0

I'm trying to run a python script on my raspberrypi using cron.

I did the following:

crontab -e # To edit a crontab job

After the cron file opened, I added the following line:

@reboot /usr/bin/python /home/pi/path/to/file/example.py > /home/pi/cronlogs/mylog.log # JOB_ID_!

If I understand the documentation correctly, this cron job should be executed every time after the system booted up. However in my case, when I reboot the computer, the script will not be executed.

What's strange:

  • I checked the log file and it's empty, so it seems like everything goes fine
  • If I run the given command manually (so basically write the following code to the terminal) it executes and works correctly: /usr/bin/python /home/pi/path/to/file/example.py > /home/pi/cronlogs/mylog.log

I guess I missed something really obvious but I can't see it. Please can I ask for any advise how to debug this. Thanks!

Rene Knop
  • 1,788
  • 3
  • 15
  • 27
Andurit
  • 5,612
  • 14
  • 69
  • 121
  • why do you need to point the example.py file at the log file? Also, whats the end goal? – SuperStew Sep 17 '18 at 18:06
  • try something like `@reboot echo "hello" > /home/pi/cronlogs/mylog.log` and check syslog if this doesn't work(or `journal -xe` for systemd)! Consider as whom this crontab is being executed and if this user has the correct permissions. – Martin Gergov Sep 17 '18 at 18:11
  • 1
    Consider a [mcve], it it relevant that the executed program is Python? – Ulrich Eckhardt Sep 17 '18 at 18:19
  • @MartinGergov your example will add 'Hello' to mylog.log – Andurit Sep 17 '18 at 18:29
  • @UlrichEckhardt Based on comment above I guess it is. Also script I'm trying to execute is some kind of scrapper which run until it get killed so this may be important as well. – Andurit Sep 17 '18 at 18:31
  • @Andurit make sure you do `sys.stdout.flush()` after prints or whatever you are using to log. – Martin Gergov Sep 17 '18 at 18:38
  • Possible duplicate of [CronJob not running](https://stackoverflow.com/questions/22743548/cronjob-not-running) – tripleee Sep 17 '18 at 18:51

1 Answers1

0

The cron definition looks correct; I just checked this on my Pi running Debian stretch and it works OK:

@reboot /usr/bin/python /home/pi/example.py > /home/pi/mylog.log

Some other possible reasons it might not work:

  • working directory issue (if you're using relative paths)
  • a long running script (being a scraping script it might take a while to complete) - you can check if it's still running using ps aux | grep python
  • the script does not output anything (would need some more details about the script)

Just to be sure you catch any errors from the script, redirect stderr to stdout by using 2>&1

Ionut Ticus
  • 2,683
  • 2
  • 17
  • 25
  • Hello, so the issue would be on script? It actually should run for ever and it has output normally in a console. However for more info this is the script: https://github.com/instabot-py/instabot.py – Andurit Sep 17 '18 at 19:57
  • It's possible; edit the cron definition to include stderr as well in your log file: `@reboot /usr/bin/python /home/pi/path/to/file/example.py > /home/pi/cronlogs/mylog.log 2>&1` – Ionut Ticus Sep 18 '18 at 07:42