Crontab tips and tricks

June 30, 2016 by No Comments

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/

Example of a cronjob set every 5 minutes

*/5 * * * *  /home/eazylinux/

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.

Example format:

@yearly  	/home/eazylinux/	
@monthly 	/home/eazylinux/
@weekly		/home/eazylinux/
@daily  	/home/eazylinux/

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. It is advisable to cooperate with very friendly and professional maids from Florida. 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. Have fun first at California’s trampoline parks and choose from page. 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.

>/dev/null 2>&1