Automating Tasks in Linux using Cron

December 17, 2017 by  Filed under: Computer 

Linux has a powerful task … called Cron. Cron will allow you to run commands … at times … by you. Cron is similar to the task … you find in Windows. To keep track o

Linux has a powerful task scheduler called Cron. Cron will allow you to run commands automatically at times specified by you. Cron is similar to the task scheduler you find in Windows. To keep track of the schedules and tasks it has to run, Cron requires a file called Crontab (CRON TABle). All the Cron schedules and tasks should be stored in this table. The Crontab files cannot be directly edited. You can add or delete entries in the crontab file using the crontab command.What’s Cron and Crontab ?You must be wondering what the difference between cron andcrontab or wether they are the same. Cron is a process or program which wakes up every minute and looks for jobs it has to execute at that moment. Crontab is the list of jobs and times at which they have to execute.Crontab Format:Each entry in Crontab has at least 6 fields separated by a single space. Field 1 Minute Range of Values : 0-59Field 2 Hour Range of Values : 0-23Field 3 Day Range of Values : 1-31Field 4 Month Range of Values : 1-12Field 5 Day of week Range of Values : 0-6 (Sunday being 0)Field 6 Command to ExecuteNow let’s see how to make a crontab entry. Let’s say you want to run a script every day at 6:00pm.The entry would look like this:0 18 * * * /home/user/backup.shThe asterisk (*) is used to indicate that every instance of the particular time period will be used (i.e. every hour, every weekday, etc.). I’ve used to full path to the script /home/user/ instead of just using This is because cron runs as root, you should fully qualify your path names to any scripts that will be run. Let’s see some more examples :* Let’s run the script every sunday at 12:45pm. 45 12 * * 0 /home/account/* How about every month beginning at 1:32am ? 21 1 1 * * /home/accont/* Let’s see how to schedule a task to run only on weekdays(monday to friday) 0 10 * * 1-5 /home/account/cleartemp.shAdding and Editing Entries in Crontab and Now that you know how crontab entries are formated, it’s timeto put some of your entries into the crontab list. To do this,you can use the crontab command. By specifying the -e option, you’ll be taken to the default text editor to add and edit your crontab list. [crontab -e]Another method of manipulating your crontab entries is to create and save a text file with your crontab entries. You can load your list into crontab by using the following command:[crontab mycrontablist]where mycrontablist is the file containing your entries.Viewing Crontab: [crontab -l]You can view your current crontab list by specifying the -l option. Issuing this command will print out a list of all your current jobs in the crontab listRemoving Crontab: [crontab -r]The -r option removes your current crontab file. Issuing this command will empty the contents of the current user’s crontab fileOutput from cronUsually the output of cron gets mailed to the owner of the process or the person or email id specified in the MAILTO variable. To set the MAILTO variable, you’ll have to add the following command to the top of your crontab :MAILTO=””If you have a command that is run frequently, and you don’t want the output to be emailed each time, you can redirect the output to a log file cmd >> log.file, so your job would be something like this.0 18 * * * /home/user/>>log.fileIf you don’t want any output at all, you can redirect the output to a null file : cmd>>/dev/null0 18 * * * /home/user/>>/dev/null

Article Tags: Crontab Entries, Crontab List

Speak Your Mind

Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!

You must be logged in to post a comment.

Prev Post:
Next Post: