Cron Jobs: How to Get Started With Automation
If you are coming from Windows, Cron may seem complex at first, but if you understand how to input the time the way Cron expects, it becomes considerably easier to use to implement your scheduled tasks.
Join the DZone community and get the full member experience.
Join For FreeWhen using a Linux operating system, you have a powerful and helpful tool available to you called "Cron." Using this tool, you can schedule what are called Cron jobs, which can help a system administrator automate a number of tasks such a backups, log file rotation, system maintenance, or other tasks that can get repetitive and tedious to perform manually over time. By using Cron jobs, an administrator can save quite a bit of time and work more efficiently on other jobs.
What Is Cron?
Cron is a daemon that executes commands on the system using a specified schedule. Where Windows uses a program named Task Scheduler, Linux has Cron. If you are coming from Windows, Cron may seem complex at first, but if you understand how to input the time the way Cron expects, it becomes considerably easier to use to implement your scheduled tasks.
Linux operating systems come with some flavor of the Cron daemon, so installation should not be necessary, which allows you to make use of this utility from the start if you like.
Basics of Cron Jobs
Cron jobs are created by editing a user's crontab file (the root user can have a crontab file as well in order to run administrative tasks). The crontab file is accessed using the crontab command, which can be called using one of three different flags:
-l list the file contents
-e edit the file contents
-r remove the file
For example, to edit the crontab file for the current user, you simply type the command:
crontab -e
This will either open the crontab file using the default text editor or allow you to choose the text editor that it will use to open crontab files and open it using that editor.
Creating Cron Jobs
With the crontab file open for editing, you can now create a Cron job by specifying the time a task should run as well as pointing to what task should be run at that time. The time entry can be a little tricky, as it is a series of numbers or asterisks separated by spaces. The order and possible values are shown below:
Minute | Hour | Day | Month | Day of Week |
0-59 | 0-23 | 1-31 | 1-12 | 0-6 |
There are a few things you should note about this:
The hour is based on a 24-hour clock, so 0 is midnight, 13 is 1:00 pm, and so on.
The day of the week is zero-based, so 0 is Sunday, 1 is Monday, and so on.
If an asterisk is put in place of any of these numbers, then it is a wild card character, which represents every possible value that can be in that place. This allows you to schedule tasks every day, every hour, and so on.
Crontab Examples
To understand how this works, here are some examples:
0 * * * * backup.sh
This runs the backup.sh command every hour, right at the beginning of the hour (for example, 1:00, 2:00, and so on). Using the 0 minute and leaving the hour (and the rest) as wild cards, the command gets run every hour.
30 20 * * * backup.sh
This runs the program backup.sh every day at 8:30 pm (20:30). Since the minute and hour are filled in, and the remaining slots are wild cards, the program runs daily at 20:30.
There are also a number of other configurations you can use to set up the type of automation you need.
Published at DZone with permission of Darren Perucci, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments