2

I'm running a program on one of my PIs that has a tendency to error out. I'm still trying to figure out the error but would like a cron entry (or maybe a python script, or most likely both) that will check on an hourly basis to see if the particular job is running, if so, do nothing, if not, then restart it. It's been YEARS since I used *nix and I'm not quite sure where to start.

Thanks for any assistance

5

The following is an excerpt from the 'Raspberry Pi: Measure, Record Explore' book. It doesn't have the script for the restarting, but at least it will give you a going over of setting crontab.

Consider that we wish to run a Python script every day at 6am. The following command will let us edit the crontab;

crontab -e

Once run it will open the crontab in the nano editor. The file will look as follows;

# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command

The default file obviously includes some explanation of how to format an entry in the crontab. In our case we wish to add in an entry that told the script to start at 6 hours and 0 minutes each day. The crontab accepts six pieces of information that will allow that action to be performed. each of those pieces is separated by a space.

A number (or range of numbers), m, that represents the minute of the hour; A number (or range of numbers), h, that represents the hour of the day; A number (or range of numbers), dom, that represents the day of the month; A number (or list, or range), or name (or list of names), mon, that represents the month of the year; A number (or list, or range), or name (or list of names), dow, that represents the day of the week; and command, which is the command to be run, exactly as it would appear on the command line. Assuming that we want to run a Python script called ‘m_temp.py` which was in the ‘pi’ home directory the line that we would want to add would be as follows;

0 6 * * * /usr/bin/python /home/pi/m_temp.py

So at minute 0, hour 6, every day of the month, every month, every day of the week we run the command /usr/bin/python /home/pi/m_temp.py (which if we were at the command line in the pi home directory we would run as python m_temp.py, but since we can’t guarantee where we will be when running the script, we are supplying the full path to the python command and the m_temp.py script.

If we want to run the same command every 1 hours we can use the */1 notation, so that our line in the crontab would look like the following;

* */1 * * * /usr/bin/python /home/pi/m_temp.py
2

I would start with man crontab It will tell you everything you could possibly need to know about cron.

Once you've familiarized yourself a little bit, the command crontab -e will open your user's crontab configuration for editing. At the very bottom of the file will be a commented out line reminding you of the order for years, months, days, hours, etc.

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.