Issue
I have a crontab job, that looks like this:
*/5 * * * * /usr/bin/flock -w 0 /home/partners_perfluence_delivery/cron.lock cd /home/partners_perfluence_delivery && /usr/bin/python3 /home/partners_perfluence_delivery/main.py
When I run script main.py
manually, everything works like a charm. When I'am trying to use crontab job, lock-file is created, but when I am looking for a job with ps aux | grep main.py
I see nothing in the output as well as requests used in main.py
are not being made
What am I possibly doing wrong?
Solution
flock
is executing cd
in a subprocess, then releasing the lock when that completes. If the Python script needs to be run in that directory, it doesn't happen because the directory was only changed in the subprocess.
You should execute cd
and python
in the same shell process using flock -c
.
*/5 * * * * /usr/bin/flock -w 0 /home/partners_perfluence_delivery/cron.lock -c 'cd /home/partners_perfluence_delivery && /usr/bin/python3 /home/partners_perfluence_delivery/main.py'
Answered By - Barmar