In order to automate the processes, we need to execute some commands or tasks periodically like the backups taken, downloading mails to Billing or ticketing system etc. We can accomplish these tasks by adding a cronjob. Cron is the name of program that enables unix users to execute commands or
scripts (groups of commands) automatically at a specified time/date. In cron the minimum unit you can specify is minute, so you cannot schedule a every-second cronjob. In a typical scenario, there is no reason for most of us to run any job every second in the system.
crond is the daemon for the cronjob and it needs to be run in order to work the crons. Make sure the daemon is started at startup, if any issues with the cron occurs.
Common commands used for viewing and editing cronjobs are;
crontab -l # Viewing the cronjobs as currently logged in user crontab -e # Edit the cronjob for currently logged in user crontab -l -u $USER # View the cronjob for the specified user crontab -e -u $USER $ Edit the cronjob for the specified user
Example of a cronjob at 6 AM at every day can be like following
0 6 * * * /home/eazylinux/script.sh
Example of a cronjob set every 5 minutes
*/5 * * * * /home/eazylinux/script.sh
If you want a job to be executed on the first minute of every year, then you can use the @yearly cron keyword as shown below. Like that you can set monthly and daily.
@yearly will run at 00:00 on Jan 1st for every year.
@monthly will run at 00:00 on 1st of every month.
@weekly will run at 00:00 on starting of every week.
@daily will run at 00:00 on every day.
@yearly /home/eazylinux/script.sh @monthly /home/eazylinux/script.sh @weekly /home/eazylinux/script.sh @daily /home/eazylinux/script.sh
Using the @reboot cron keyword, this will execute the specified command once after the machine got booted every time.
We can set some variable in addition to the environment variables set by cron daemon to customize it.
MAILTO : If we set the variable, the cron will send the comamnd output to the mail account set in MAILTO variable. if MAILTO=”” ( empty ), no mail will be sent. Otherwise the mail will sent to the owner of the cronjob.
Several environment variables are set up automatically by the cron daemon. SHELL is set to /bin/sh, and LOGNAME and HOME are set from the /etc/passwd line of the crontab’s owner. HOME and SHELL may be overridden by settings in the crontab; LOGNAME may not.
Installing Crontab From a Cron File
Instead of directly editing the crontab file, you can also add all the entries to a cron-file first. Once you have all these entries in the file, you can upload or install them to the cron as shown below. This can be used for migrating cron from one server to another.
$ crontab -l no crontab for eazylinux $ cat cron-archive.txt @yearly /home/eazylinux/annual-maintenance */10 * * * * /home/eazylinux/check-load $ crontab cron-archive.txt $ crontab -l @yearly /home/eazylinux/annual-maintenance */10 * * * * /home/eazylinux/check-load
Note: This will install the crons from the file after deleting the current cron’s. So do not attempt, if there is any conrjobs present.
System Wide Cron Schedule
System administrator can use predefine cron directory as shown below.
You can change the time for the crons mentioned in the above files in “/etc/crontab”. Refer manual pages for more details.
Content of /etc/crontab;
17 * * * * root cd / && run-parts --report /etc/cron.hourly 25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily ) 47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly ) 52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
Disable email notification
You can use the following entry at the end of cron command to disable email notifications.