Score:0

crontab sees bash script running while it is not

il flag

I have a python file which should ideally run at all times. However, sometimes it crashes, so I would have to start it again. Therefore, I have a cronjob that should try to run every 5 minutes (if it crashes it would restart again always within 5 minutes at least):

*/5 * * * * bash my_script.sh

that runs the following bash script called my_script.sh:

#!/bin/bash
now=$(date +"%T")
if ! pgrep -f 'filename.py'
then
    echo "$now: filename was not running, started filename" >> crontab_errors.log
    /home/ubuntu/envs/my_virtual_env/bin/python /home/ubuntu/filename.py arg1
else
    echo "$now: filename was already running" >> crontab_errors.log
fi

The bash script works great when used alone, so it starts the code if it is the first instance, and if I would start another, it would mention that the filename was already running.

However, this behavior is not seen in crontab. There, it continuously mentions that the file is already running, while easy inspecting with the top command clearly shows that there is no python process called 'filename.py' running. It is also visible in the log that I create, called crontab_errors:

17:55:01: filename was already running
18:00:01: filename was already running
18:05:01: filename was already running

What am I missing here? My goal is just to always be sure that only 1 instance of filename.py is running because otherwise the code crashes.

Thanks in advance!

vanadium avatar
cn flag
Did you wonder whether there may be a different, better approach to achieve what you want instead of the approach you think will work?
intStdu avatar
il flag
I searched a lot online for automation tasks and crontab seems to be the way to go if you want to have processes running even if you are away from keyboard. So if you have other suggestions I would really like to know! This part of programming/automation is new to me so any directions would be great!
vanadium avatar
cn flag
Then you better ask a question indicating what you want (your Problem X), indicating what you tried thus far, rather than asking about what you tried thus far (Problem Y).
intStdu avatar
il flag
Well, what I want is also summarized in the last sentence to be honest: "My goal is just to always be sure that only 1 instance of filename.py is running because otherwise the code crashes." And also in the first sentence, and I provide working code. So I do not really agree with you here...
hr flag
Are your files really named `my_script.sh` and `filename.py`? Often this kind of issue is due to a partial match with the script process
intStdu avatar
il flag
@steeldriver should i make those files absolute paths?
PonJar avatar
in flag
If you use a systemd unit file to start your script you can configure it to restart automatically should it ever fail. I suggest you research how to do that.
vanadium avatar
cn flag
@PonJar this is exactly why I hinted OP not to ask about problem Y, but about problem X. This is indeed the solution.
PonJar avatar
in flag
@vanadium Thanks for all the helpful stuff you post on here. You will not know it but I have learnt a lot from you. Being pragmatic I thought a push in the right direction would help. If this OP were to ask a question about how best to achieve his goal rather than how to do it with cron then I’d be happy to answer with an example unit file
vanadium avatar
cn flag
@PonJar, I agree very much with your viewpoint. And thanks for the complements!
I sit in a Tesla and translated this thread with Ai:

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.