Linux/cron
cron
"The software utility Cron is a time-based job scheduler in Unix-like computer operating systems. People who set up and maintain software environments use cron to schedule jobs (commands or shell scripts) to run periodically at fixed times, dates, or intervals. It typically automates system maintenance or administration—though its general-purpose nature makes it useful for things like connecting to the Internet and downloading email at regular intervals.[1] The name cron comes from the Greek word for time, χρόνος chronos." [1]
Installation
yum install vixie-cron crontabs service crond restart
crontabs
Commands:
export EDITOR=vi # specify a editor to open crontab file. crontab -e Edit your crontab file, or create one if it doesn't already exist. rontab -l Display your crontab file. crontab -r Remove your crontab file. crontab -v Display the last time you edited your crontab file. (This option is only available on a few systems.)
crontab layout:
# min hr day mon wkday cmd * * * * * command to be executed - - - - - | | | | | | | | | +----- day of week (0 - 6) (Sunday=0) | | | +------- month (1 - 12) | | +--------- day of month (1 - 31) | +----------- hour (0 - 23) +------------- min (0 - 59)
What are cron and crontab, and how do I use them?:
Field Value Description minute 0-59 The exact minute that the command sequence executes hour 0-23 The hour of the day that the command sequence executes day 1-31 The day of the month that the command sequence executes month 1-12 The month of the year that the command sequence executes weekday 0-6 The day of the week that the command sequence executes (Sunday = 0, Monday = 1, Tuesday = 2, and so forth)
All marker: Use '*' to indicate this should happen for all time increments for the selected column
Step:
*/10 * * * * [sometask] # do every 10 minutes
Lists:
0,15,30,45 * * * * [sometask] # list of minutes 0-4,8-12 * * * * [sometask] # range and list of minutes 0 0-23/2 * * * * [sometask] # range with step (run ever other hour)
New line character '%'
0 22 * * 1-5 mail -s "It’s 10pm" joe%Joe,%%Where are your kids?%
Cron Environment Variables:
# use /bin/sh to run commands, no matter what /etc/passwd says SHELL=/bin/sh # mail any output to ‘paul’, no matter whose crontab this is MAILTO=paul
Special strings:
string meaning ------ ------- @reboot Run once, at startup. @yearly Run once a year, "0 0 1 1 *". @annually (same as @yearly) @monthly Run once a month, "0 0 1 * *". @weekly Run once a week, "0 0 * * 0". @daily Run once a day, "0 0 * * *". @midnight (same as @daily) @hourly Run once an hour, "0 * * * *".
Help:
man 5 crontab
Run script on reboot: [2]
@reboot python /home/pi/MyScript.py &
See Also
keywords
linux cron