4

I have finished a program in Python and I intend for it to be run from my RPi every n hours. This will be my first time running such program/script in this way and I would like to know if there is anything I should know/have written into the script before I add it to my crontab?

I'm also not sure how such an entry in the crontab would look. Do I write something like:

* 2 * * * pi `python /home/pi/Desktop/myProg.py`

With back-ticks around the command or would I launch myProg.py from within a shell script that I would call from the crontab?

* 2 * * * pi /home/pi/Desktop/launchMyProg.sh

Also, I had some print statements in the program that I commented out as they were mainly for debug during writing it, but on second thoughts it would be useful for me to have these for debug info but directed to the /var/log/messages or other log file which I can monitor with tail -f /var/log/messages command.

How would I do that exactly? Thanks.

uncle-junky
  • 723
  • 1
  • 8
  • 33
  • In crontab you write commands exactly as in the shell. Only & important difference: you may have different environment variables. The most common issue arises from different PATH settings. – georgepsarakis Jun 13 '13 at 20:36

2 Answers2

4

You are on the right track. To fire a python script every n hours, do this:

* */n * * * python /home/pi/Desktop/myProg.py

That's it. Make sure that you are editing the sudo level crontab, which is accessed via sudo crontab -e.

Your code would actually have the script run at 2 am every day - it technically says "run whenever the hour is equal to 2". * */n * * * says "run whenever the hour is divisible by n". A GREAT resource on crontab can be found here: Schedule tasks on Linux using crontab

Logging the output is also easy. If you had a log file on your desktop called myLog.log, you would add >> /home/pi/Desktop/myLog.log to end of the crontab entry. That would make the entry look like this:

* */2 * * * python /home/pi/Desktop/myProg.py >> /home/pi/Desktop/myLog.log

EDIT (thanks to @Iamreck): Another option for outputting to a file is with sys.stdout. This would accomplish the same goal. To do it this way, add the following to your Python script:

import sys
sys.stdout = open("myLog.log","w")    
sys.stderr = open("myLogErr.log","w")   
print "stdout test"

You would also have to change the working directory to your Desktop when doing this, and this can be done with the top answer at this SO post

Community
  • 1
  • 1
rickcnagy
  • 1,774
  • 18
  • 24
  • 1
    Use `sudo crontab -e` if and only if you want the job to run as `root`. Also, if you `chmod +x` the script (and make sure it has a proper `#!` line at the top), you can execute it directly rather than passing it as an argument to the `python` command. – Keith Thompson Jun 13 '13 at 22:53
  • 1
    Another point: I don't use `crontab -e`; it's too easy to make incorrect edits and not be able to get back to the previous version. I keep my crontab in a separate file under a source control system, and feed the file to `crontab`. – Keith Thompson Jun 13 '13 at 22:54
0

Your second bit of code is indeed correct.

You have two options to redirect the output of your program.

  1. Standard Linux IO Redirection - http://www.tuxfiles.org/linuxhelp/iodirection.html

    * 2 * * * python /home/pi/Desktop/myProg.py &>> /home/<user>/myProg.log
    

    This will redirect stdout and stderr, appending to that log file.

  2. Change the sys.stdout file object in your Python script (and probably stderr and too)

    import sys
    sys.stdout = open("myLog.log", "w")
    sys.stderr = open("myLogErr.log", "w")
    print 'testing new stdout'
    

You also may want to run it as another user, you can accomplish that with su.

RyPeck
  • 7,830
  • 3
  • 38
  • 58