3

On Raspbian, I created a crontab file using the following command:

sudo crontab -e Added the following lines to cron

@reboot /usr/bin/python3 /home/pi/dev/bootscripts/ipchecker/ipchecker.py
0 * * * *  /usr/bin/python3 /home/pi/dev/bootscripts/ipchecker/ipchecker.py

I tested my script and it does the work successfully when run via command-line:

python3 /home/pi/dev/bootscripts/ipchecker/ipchecker.py 

OR

/usr/bin/python3 /home/pi/dev/bootscripts/ipchecker/ipchecker.py

So with my cron jobs I would expect to get the python script running on every reboot and every hour, but seems not. What could be wrong here, and how can one debug what's going on silently on cron jobs? Should I check in the Logs?

  • 1
    Hello, could you check a few of the existing posts on the topic some might contain relevant insight. Using full paths is a good thing with cron for starters. As for debugging, check journalctl for cron outputs and try adding a simple logging of your own (e.g. write to file) to the python script itself. That should get you at least started. – Ghanima Jul 26 '16 at 7:29
  • As Ghanima says check your paths are absolute which also includes the paths in the scripts you call up (until you have explicitly set a current directory). – joan Jul 26 '16 at 8:05
  • @joan im confuse with your comment , as i described on the first block of code i have the full paths both for pyhton3 and for the script. So what do you meant by '(until you have explicitly set a current directory)' – Jorge Vidinha Jul 26 '16 at 8:19
  • @JorgeVidinha You need the full paths in the scripts you call up. Your question doesn't show the content of the script(s) you are calling up. – joan Jul 26 '16 at 8:22
2

I believe I have answered this question in a thread that took me quite a while to solve which was answered on the Unix/Linux Forum concerning running a script on boot for the RPi-2. The thread is triggering tmux on boot

however I could summarize the steps for you briefly:

  1. Set your PATH variable. You can firstly check if your defined folder containing your script is already in the PATH environment or not using:

    $ $PATH # this should print out different paths
    

    If you do not see your path then add it by doing the following:

    $ export PATH=$PATH:/path/to/dir/
    
  2. Now once you are done add your crontab -e with a little bit of tweak:

    $ crontab -e
    ## inside your crontab 
    
    @reboot sleep 10; /usr/bin/python3 /home/pi/dev/bootscripts/ipchecker/ipchecker.py
    

the sleep on boot up is actually very useful because depending on your system the @reboot job might be triggered before the PATH variables are available. This might look hacky but this is a plausible explanation and I have traversed through a lot of threads on StackOverflow which suggest that sleep or wait is a great way for boot scripts. This works for me and it should definitely work for your case too.

Inference

I suggest to add a little sleep before some scripts, especially ones which are used at bootup.

  • 2
    Note that if it is necessary, where and when you set $PATH is significant. Simply calling export from the command line will not make the change persistent across reboots, nor will it affect currently running processes (including crond) or their future children. – goldilocks Jul 26 '16 at 11:15
  • Do you mean adding the path to the bash_profile should also be important? – Shan-Desai Jul 26 '16 at 11:17
  • 1
    I mean for it to be effective, it must be done at an appropriate point. You probably don't want to willy-nilly pile things onto the default system wide path (if I did, I'd do it in a new file in /etc/profile.d -- but note this won't necessarily apply to stuff started by init, you'd have to test). If this is just something that is important for this event, then you should use a wrapper script which exports the path. This will then apply to anything started by that script and so on, but not the system as a whole. That's probably the ideal mechanism. – goldilocks Jul 26 '16 at 11:37
0

To close this one and after some battle i solved the issue finding that the problem was related to the usage os.getcwd() to read data from a local file inside my script - issue seems be fixed for now after adding full paths to locate the needed files. using os.getcwd() was always referencing to /root/ and the needed file that could never be found. Probably there is a way to tell cron to be relative to where the script is running from ...

Found my issue this piping the output log of my script like below :

@hourly /usr/bin/python3 /home/pi/dev/bootscripts/ipchecker/ipchecker.py >> /home/pi/dev/bootscripts/ipchecker/ipchecker_hourly.log 2>&1

Thanks all for the multi learning i received from all of you . Regards

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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