cheatsheet on cron and backups
1 Cron files:
Every user has a set of cron tables (files) that are stored in /var/spool/cron /var/log/cron # for CentOS
You do not edit them directly (but you can view them).
To edit them use the command crontab -e
To list them use crontab -l
To remove the table use =crontab -r=** Syntax. There are five numeric fields (or special keyword shortcuts like @yearly) followed by the command you want to run.
- * * * * command to execute
- minute (0 - 59)
- hour (0 - 23)
- day of month (1 - 31)
- monthly (1 - 12)
- day of week (0 - 6) Sunday = 0 or 7
* * * * * <command to be executed> - - - - - | | | | | | | | | +------day of the week (0 - 6) (Sunday=0 | | | | | | | +-------month (1 - 12) | | | | | +------ day of month ( 1 - 31 ) | | | +------hour (0 -23) | +----+ min (0 - 59 )
1.0.1 Each time field can be:
- * for all possible times for that field
- - a separated range (e.g. 2-5)
- , a separated list (e.g. 1,3,5)
1.0.2 Special keyword shortcuts:
- @reboot
- @yearly
- @monthly
- @weekly
- @daily
- @hourly
To remotely sync backups every hour:
0 * * * *
2 My cron examples
2.1 Every night, send an email with a disk free report to an admin.
15 23 * * * df -h --total | mail -s "Disk Free Report" admin@earth.continents.ops
2.2 Every Sunday at 11:30 pm
30 23 * * 0 my-start-of-the-week-backup-job
2.3 Every 15th of the month at 3 am
0 3 15 * * my-montly-maint-job
2.4 Every hour on the hour, on the quarter hour and on the half hour
cat /var/spool/cron root@c8host /var/spool/cron [196]$ cat root
0 * * * * rsync -avz 192.168.111.11:/etc /backup/incremental/vm1/ 15 * * * * rsync -avz 192.168.111.12:/etc /backup/incremental/vm2/ 30 * * * * rsync -avz 192.168.111.13:/etc /backup/incremental/vm3/ root@c8host /var/spool/cron
2.4.1 Reminder of rsync syntax
rsync -vz from to
rsync -vz fromdirectory todirectory
rsync -vz source destination
if [destination]
is omitted, rsync acts like ls -l
on the source.
The source
or destination
may be local or remote, i.e. rsync works both ways.
So source or destination can be:
- user@10.1.1.1:/home/zintis/folder
2.5 As a user, run date every 10 minutes (with output to a file)
0,10,20,30,40,50 * * * * date 1>> /home/zintis/cron-output.txt 2>> /home/zintis/cron-output.txt That is becuase 1 is stdout and 2 is stderr But I can combine that with & which is used for BOTH stdout and stderr so:
0, 10, 20, 30, 40, 50 * * * * date &>> /home/zintis/cron-output.txt
You could also specify the minutes as:
*/5
for every 5 minutes*/10
for every 10 minutes etc.
Or every 3 hours on the 15 minutes past the hour:
15 */3 * * *
could also just add >> /home/myuser/myscript.log 2>&1
to the end of your
crontab entry.
2.6 Another issue if running as a bash or as a script
I solved the problem. There are two ways:
M1
Change the redirection from &>>
to 2>&1.
So now crontab -e looks like
*/1 * * * * /home/ranveer/vimbackup.sh >> /home/ranveer/vimbackup.log 2>&1
I believe the above works because by default cron is using sh
to run the task
instead of bash so &>>
is not supported by sh.
M2
Change the default shell by adding SHELL=/bin/bash
in the crontab -e file.
2.7 Also, consistency with /bin/sh
Make sure your shell redirection syntax conforms to /bin/sh
. If you try to
use shell redirection syntax that is not valid with /bin/sh
then your
command will fail and your cron job will not ever run.
In your /etc/cron.d/example1
config files if you specify a user other than
root
and that user's login shell is not /bin/bash
… you must still use
/bin/sh
syntax in /etc/cron.d/example1
command.
For example
If your user has shell csh
or zsh
or ksh
set for his login shell. In your
/etc/cron.d/example1
config file, the command must use /bin/sh
syntax.
Specifically any shell redirection must be /bin/sh syntax.
If you try to use for example csh
shell redirect syntax in your
/etc/cron.d/example1
, then your cron job will never run. The logfile
for crond located at /var/log/cron
shall say that the command is run but
the command will error out with a syntax error before your command gets run.
Good discussion of ubuntu options for cron are here