Crontab didn’t work as intended

cron

I set crontab but nothing happened,

crontab -l
03 04-22 * * * python /me/radio_alarm.py

What's the reason, the command works as intended in Linux.

Best Answer

Your crontab suggests that at 3 minutes past every hour from 04:00 through 22:00, invoke python to run /me/radio_alarm.py

If that's correct, and there are no other problems that we can't see*, the following correction should work:

03 04-22 * * * /usr/bin/python /me/radio_alarm.py

It is necessary to specify the complete path to python because the "cron user" does not have the same $PATH environment that you do under your user name.

* I assume that you have successfully run your script from the command line. If that is the case, you've probably cleared most of the potential errors below, but just in case here are the "usual suspects":

  • is your script marked as executable? (e.g. chmod 755 /me/radio_alarm.py)
  • does your script begin with an appropriate shebang entry? (e.g. #!/usr/bin/python)
  • is your script written for the same version of Python that you're invoking? (e.g. mac os has only Python2, unless you've installed Python3 as part of homebrew, etc.)

Finally, it never hurts to capture any stderr output while you're testing a new script. You can easily add an "error log" to your script as follows:

03 04-22 * * * /usr/bin/python /me/radio_alarm.py  > ~/cronjoblog 2>&1  

This will redirect any error output from your script to the file cronjoblog in your user's home directory.

Hope that helps. Let us know if you have further issues or questions.