I have a shell script main.sh
which runs a python script main.py
. When i run the main.sh
script manually then the execution is fine (Note: i am running it manually with root user) and the python script works as expected. However when i try the same from crontab which runs the main.sh script every 30 mins then python script is not getting executed.(i have already done chmod u+x main.sh to make it executable)
The crontab given below is also of the root user
my crontab file:
*/30 * * * * /home/opc/python_scripts/main.sh >> /home/opc/cron.log
.
my main.sh file:
#!/bin/bash
output=$(sh /home/opc/python_scripts/url.sh)
echo "$output"
searchstring="url is up, login service is up and oc console is up"
if [[ "$output" == *"$searchstring"* ]] ; then
echo "URL is up"
sudo python3 /home/opc/python_scripts/main.py >> /home/opc/pyfile.log
else
echo "URL is Down"
sudo python3 /home/opc/python_scripts/disable_crr.py
fi
I know that it is entering the first if condition and it is also printing the "URL is up" statement, i have another log file which records this. When i run it manually then i can see that even python script is running properly but with crontab it gets stuck at echo "URL is up"
, after that no execution happens
Things i have done to solve this:
i have seen solutions of similar questions like-Calling a python script from shell script cron , Shell Script: Execute a python program from within a shell script, but in all of these questions the user was not able to run the python script from the shell script manually. I am able to run it manually but if i use a cronjob then i am having a problem.
when i ran ls -la in the directory where these scripts are located the ownership of these files was like:
main.sh root root
main.py opc opc
Also i added #!/usr/bin/env python3
at the top of my python script as i had seen this as a solution in a similiar question but did not work, only manually works, not with crontab.
How do i run it with crontab?