The cron service (daemon) runs in the background and constantly checks the /etc/crontab file, and /etc/cron.*/ directories. It also checks the /var/spool/cron/ directory.
/etc/crontab is system crontabs file. Usually only used by root user or daemons to configure system wide jobs.
Typical /etc/crontab file entries:
SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root HOME=/ # run-parts 01 * * * * root run-parts /etc/cron.hourly 02 4 * * * root run-parts /etc/cron.daily 22 4 * * 0 root run-parts /etc/cron.weekly 42 4 1 * * root run-parts /etc/cron.monthly
First, the environment must be defined. If the
SHELL line is omitted, cron will use the default, which is /bin/sh. If the
PATH variable is omitted, no default will be used and file locations will need to be absolute. If
HOME is omitted, cron will use the invoking users home directory.
Scripts in /etc/cron.d/ is much equivalent to /etc/crontab file. You can specify different run time and user ID used to run each script.
Usually system-wide daemon such as sysstat places their cronjob here.
Other cron job folders
As a root user or superuser you can use following directories to configure cron jobs. You can directly drop your scripts here. The run-parts command from /etc/crontab file runs scripts or programs in below directories :
|/etc/cron.daily/||Run all scripts once a day, run time scheduled in /etc/crontab|
|/etc/cron.hourly/||Run all scripts once an hour, run time scheduled in /etc/crontab|
|/etc/cron.monthly/||Run all scripts once a month, run time scheduled in /etc/crontab|
|/etc/cron.weekly/||Run all scripts once a week, run time scheduled in /etc/crontab|
Usually, used by system services and critical jobs that requires root like privileges. The sixth field is the name of a user for the command to run as. This gives the system crontab the ability to run commands as any user.
> cat /etc/cron.d/novell.com-suse_register 06 02 06 * * root /usr/lib/suseRegister/bin/cron_suse_register
User can install their own cron jobs using the crontab command. The sixth field is the command to run, and all commands run as the user who created the crontab.
Disable Email Alert
By default, Command output is mailed to the crontab owner.
To disable this email notification:
- Add any one of the following at the end of the line for each cron job to redirect output to /dev/null.
0 1 5 10 * /path/to/script.sh >/dev/null 2>&1 #OR 0 1 5 10 * /path/to/script.sh > /dev/null #OR 0 1 5 10 * /path/to/script.sh > /dev/null 2>&1 || true
- You can set MAILTO=”” variable at the start of your crontab file.
MAILTO="" #This will also disable email alert. 0 3 * * * /path/to/script.sh MAILTO="userX" #This will send email to userX 0 3 * * * /path/to/script.sh
Special time keywords
Instead of the first five time fields, you can use any one of eight special keywords. It will not just save your time but it will improve readability:
|Special Time 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 * * * *”|