Score:0

Bash script in Cron not running Python Script on Ubuntu 20.04.2 LTS

cl flag

I have an Ubuntu 20.04.2 LTS server which I run few python scripts.

I created a cronjob to run a bash script to check and see if my python script is running:

#!/bin/sh
#!/bin/python2.7


SERVICE="start_server.py"
if ps -ef | grep "$SERVICE" | grep -v grep >/dev/null
then
    echo "start_server.py is running" >> /home/ubuntu/mislaka/script_log.log
else
        python2.7 /home/ubuntu/mislaka/start_server.py &
    echo "start_server.py stopped" >> /home/ubuntu/mislaka/script_log.log
fi

This is the cronjob configuration:

* * * * * /home/ubuntu/mislaka/check_process.sh

When checking the log file I can see it is adding the echo commands to the file but it does not start the python script.

If I run my script outside of the cronjob it is working as expected.

Will appreciate any help.

Thank you!

Score:0
ng flag

There are some differences between bash and sh shells. If you are using bash you shoud use:

#!/bin/bash
Score:0
cl flag

Found out what was my issue. Instead of writing my command under the else statement in one line, I wrote it in two:

echo "start_server.py stopped" >> /home/ubuntu/mislaka/script_log.log && python2.7 start_server.py &

This fixed my issue.

bac0n avatar
cn flag
That's unlikely, the only thing changed is the order in which the line is executed, I suspect you are missing the path to `start_server.py`
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.