cron
Warning
If a cron script in /etc/cron.d
has a .
in the file name,
then it will not run!
ref configs with dots in file name not working in /etc/cron.d
Links
Configuration (System)
/etc/crontab
email
To email the output of your cron job to a specific user, add the following at the top of your cron script:
MAILTO=patrick@mycompany.com
To stop emails:
MAILTO=
…or pipe the output of your command to a file (see Logging below)…
Logging
To write output to a log file, add the following after your command:
>> /home/my-user-name/logs/update_index.log 2>&1
Note: This re-directs stdout
AND stderr
which isn’t much good if you
are doing a SQL dump to stdout
. In this case, try something like this:
mysqldump --host=localhost --user=patrick --password=mypassword redmine > $DUMP_FILE 2>> $LOG_FILE
System
System cron tasks are configured from the /etc/crontab
file. The
/etc/crontab
file is set-up to run commands hourly, daily, monthly and
weekly:
/etc/crontab
To set-up a task to run at these times, just add your shell script to the relevant folder:
/etc/cron.daily
/etc/cron.hourly
/etc/cron.monthly
/etc/cron.weekly
Note
Check your shell script file name does NOT have an extension. For details, see run-parts doesn’t run script with sh extension
Note
To run a command at a different time, setup your file in the
/etc/cron.d
folder. Note: The files in this folder are in
crontab
format with the user inserted before the command (see
Sample, System below):
User
Tip
It is probably less confusing to use the global cron system for jobs on a server. It Will be easier to work out which processes are being run…
To view your own crontab
file:
crontab -l
To edit your own crontab
file:
crontab -e
To edit the crontab
file of another user:
crontab -e -u username
Sample
System
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=me@mail.com
HOME=/home/patrick/
# minute (0-59),
# | hour (0-23),
# | | day of the month (1-31),
# | | | month of the year (1-12),
# | | | | day of the week (0-6 with 0=Sunday).
# | | | | | user commands
0 0 * * * myusername /usr/bin/python2.4 /home/USERNAME/scripts/svn-hot-backup.py --archive-type=zip --num-backups=10 /home/USERNAME/svn/REPOSITORY_NAME/ /home/USERNAME/backup/svn/
User
A nice looking cron script layout (from the link below):
MAILTO=me@mail.com
# minute (0-59),
# | hour (0-23),
# | | day of the month (1-31),
# | | | month of the year (1-12),
# | | | | day of the week (0-6 with 0=Sunday).
# | | | | | commands
0 0 * * * /usr/bin/python2.4 /home/USERNAME/scripts/svn-hot-backup.py --archive-type=zip --num-backups=10 /home/USERNAME/svn/REPOSITORY_NAME/ /home/USERNAME/backup/svn/
Backing up your Subversion (SVN) repository on Dreamhost with cron
Note
This sample does not include the user
parameter. The user
parameter is required for scripts in cron.d
.
Working Hours
Cron Job every weekday during working hours. This example checks the status of the database every weekday (i.e. excluding Sat and Sun) during the working hours 9 a.m – 6 p.m:
00 09-18 * * 1-5 /home/ramesh/bin/check-db-status
00 0th Minute (Top of the hour)
09-18 9am, 10am, 11am, 12am, 1pm, 2pm, 3pm, 4pm, 5pm, 6pm
* Every day
* Every month
1-5 Mon, Tue, Wed, Thu and Fri (Every Weekday).