2

I'm trying to automatically run a script once every minute. But it might take more than 1 minutes sometimes & hence I have to have a lock on the script while it is running so that only one instance of the script can run at any given time.

My cronjob is as follows,

* * * * * root /usr/bin/flock -w 0 /var/cron.lock /pythonScripts/readPushData.py

My python script is set an executable using,

chmod a+x readPushData.py

The python compiler is set at the top of the script like,

#!/usr/bin/python3.5

The script can be run manually using './readPushData.py', but when I try to run it using a cronjob it doesn't work. Why is this happenning?

7
  • cron will only run proper commands. Have you tried running root /usr/bin/flock -w 0 /var/cron.lock /pythonScripts/readPushData.py?
    – Milliways
    Mar 26, 2019 at 7:42
  • you mean run this on shell? Mar 26, 2019 at 8:04
  • You have taken this out of context, and failed to specify what you actually did to set cron up.
    – Milliways
    Mar 26, 2019 at 9:38
  • 1
    This likely has nothing to do with the lock. If the lock matters at all, it is probably a secondary problem. Python scripts (or scripts and programs generally) that run from the command line will not necessarily run in cron due to having a different environment setup. There are multiple posts on that here, and the question really has nothing to do with Raspberry Pi. It's general Linux. As regards timing, you should probably consider having cron launch this once and having the script itself sleep between times that you want it to work.
    – Brick
    Mar 26, 2019 at 15:33

1 Answer 1

1

I found the solution for running one instance of my script at a time using this [https://raspberrypituts.com/raspberry-pi-simple-cron-jobs-explanation/][1]

Then I used the following in /etc/cron.d/python to setup the cronjob

* * * * * root /usr/bin/python3.5 /pythonScripts/readPushData.py

Then I rebooted the system. It works like a charm. Thank you all for your comments!

1
  • Please accept your own answer with a click on the tick on its left side. Only this will finish the question and it will not pop up again year for year.
    – Ingo
    Dec 28, 2019 at 21:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.