What Is Crontab And How To Automate Tasks In Linux With Crontab
Table of Contents
Cron is a utility that manages background processes or daemons from time to time (for example each minute, day, week or month). Cron works like a scheduler, for example, it can download files from the internet or download emails at regular intervals, also you can use it for more complex tasks like install updates. Originally cron was created for UNIX by Ken Thompson, but now you can use it in all UNIX-like systems, even Linux! UNIX has other utility for tasks scheduling called “at” but cron is much better. There are two ways to use cron, the first is through the cron directories located in /etc if you open a terminal and type the following command: $ ls /etc/cron* You will get the following output:
Now, you can see the following directories in the image: cron. daily, cron. hourly, cron.monthly, cron.weekly and cron.yearly.These directories store the scripts that must be executed by cron, according to the name of each directory is the frequency of the execution. For example, the scripts stored in cron.hourly will be executed each hour.
Now, I’m going to write a simple script to make a backup of a directory, and I’ll put it on the cron. hourly directory to execute the script each hour. One important thing that you must consider, is to provide the execution permissions to the script, ‘cause if you don’t, cron won’t execute the file.
The example of the process to add a script to cron tasks using the global cron directories:
I have a directory and I want to make and automatically backup each hour:
I write a script to make the backup, I used nano but you can use your favorite editor:
I test the script:
I copy the script to /etc/cron.hourly:
I give the execution permissions to the script:
All the scripts must start with line #!/bin/sh because it indicates the shell to use. Use cron through the /etc/cron.* directories aren’t very useful, so I’m going to explain the second-way using crontab.
If you remember the output when I listed the content of /etc/cron* you’ll see the following file: /etc/crontab
You can schedule tasks in the file crontab, but first, you must learn the following:
If you analyze the content of crontab file, you can identify 7 fields in the lines after “# run-parts”, I mean the following ( the first line after “# run parts”):
Field1 | Field2 | Field3 | Field4 | Field5 | Field6 | Field7 |
01 | * | * | * | * | root | nice … |
The first five fields indicate time values, the sixth field indicates the user and the seventh indicates the command to execute, this last can be a script or a command (uname, ls, apt-get, etc).Time fields are following:
minute | hour | day of month | month | day of week |
01 | * | * | * | * |
The accepted value for each field is:
minute | 00 – 59 |
hour | 00 – 23 |
day of month | 1 – 31 |
month | 1 – 12 |
day of week | 1 – 7 (Monday: 1, Sunday: 7) |
If the value of one of these fields is “*”, it means all possibles values for the correspondent field, for example, * * * * * root /root/script.shThis task will be executed by root every minute, all days and all months.
Crontab uses too special strings to define ranges of frequency:
@reboot | One time after boot |
@yearly | Once a year |
@annually | Same as @yearly |
@monthly | Once a month |
@weekly | Once a week |
@daily | Daily |
@midnight | Same as @daily |
@hourly | Hourly |
You just change the time fields for one of the special strings and then complete the rest, for example, @annually root dist-upgrade. This task should be executed each year automatically.
Now you can add tasks to crontab, also you can make a .scripts directory at your /home and store your scripts in it, finally, you can add an entry for every script in the crontab file.
Crontab can manage a tasks file for each user, you can test if your user has crontab tasks using the following command:
You can schedule tasks in a file, just create it:
When you have been created your crontab file, you must load it to cron typing the command:
$ crontab your_crontab
Cron is very powerful because after that you loaded a new crontab, you don’t have to do anything, that’s all, cron load new tasks and crontab files automatically. The result of my crontab is a script that makes a file with dates generated every minute:
Script
The file generated Other options for crontab are the following:
Cron is the most powerful task scheduler that I know and it’s excellent because it isn’t just for Linux, it’s for all UNIX-like systems. If you manage a server, probably you’ll love cron, even if you’re a common user you’ll like cron, with cron you can schedule everything. If you want to know more about cron, you should see the man pages:
$ man cron $ man crontab
LinuxAndUbuntu Newsletter
Join the newsletter to receive the latest updates in your inbox.