Linux Crontab Reference


Crontab basics

1. Crontab usage:
[user@linux1 ~] crontab --help
crontab:
usage:  crontab [-u user] file
        crontab [-u user] [ -e | -l | -r ]
                (default operation is replace, per 1003.2)
        -e      (edit user's crontab)
        -l      (list user's crontab)
        -r      (delete user's crontab)
        -i      (prompt before deleting user's crontab)
        -s      (selinux context)

2. Edit/View crontab in default editor
[user@linux1 ~] crontab -e

3. Crontab syntax
*    *    *     *    *  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)

4. Crontab examples
0       *      *     *     *         rm /home/user/tmp/*   # Will run every hour
0     18      *     *     *         rm /home/user/tmp/*   # Will run every day at 6pm
0     18      2     *     *         rm /home/user/tmp/*   # Will run on the 2nd day each month at 6pm
0     18      2     1     *         rm /home/user/tmp/*   # Will run every jan 2nd at 6pm
45     2      *     *     6         /home/user/backup.sh  # Will run every saturday at 2:45am
0     23      *     *     1-5      /home/user/backup.sh  # Will run at 11pm every monday to friday (no sat/sun)
0     12      *     *     0,2,4   /home/user/backup.sh  # Will run at noon every sun,tues,thurs
0     12      *     1     *         /home/user/backup.sh  # Will run at noon every day, only in month of january



Crontab Tweaks

1. Change default editor for cron
[user@linux1 ~] crontab -e
This will set the crontab to open in your default editor
[user@linux1 ~] export EDITOR=nano;
This will set the crontab to open in nano
[user@linux1 ~] export EDITOR=vi;
This will set the crontab to open in vi

2. Check if crond is running
[user@linux1 ~] ps aux|grep crond
root    701  0.0  0.1  5092  1076 ?     Ss  08:23  0:00 crond
root    724  0.0  0.0  4044   668 pts/8 S+  08:24  0:00 grep crond
The first entry shows that your cron daemon is running.

3. Restart crond if needed.
To restart crond, if needed become root, then run crond (no & required)
[user@linux1 ~] su
[root@linux1 ~] crond

4. Launch a GUI App from cron
30 8 * * 1-5 export DISPLAY=:0 && pidgin

5. Kill a process by name from cron
30 17 * * 1-5 killall pidgin

6. Disable email
30 18 * * * 7z a /home/user/nightly_backup.zip /home/user/files/ > /dev/null 2>&1

7. Pipe to log
30 18 * * * 7z a /home/user/nightly_backup.zip /home/user/files/ > /home/user/cronlogs/backup_7z.log

8. Append to log
30 18 * * * 7z a /home/user/nightly_backup.zip /home/user/files/ >> /home/user/cronlogs/backup_7z.log 2>&1

9. Comment out a line
#30 18 * * * 7z a /home/user/nightly_backup.zip /home/user/files/ >> /home/user/cronlogs/backup_7z.log 2>&1
code snippets are licensed under Creative Commons CC-By-SA 3.0 (unless otherwise specified)

user on 2008-12-12 16:36:48
to make the default editor permanent, add:

export EDITOR=nano

to your /home/user/.bashrc and restart the shell.