July 07, 2018
I’m all about post-it notes, to-do lists, and setting reminders and calendar events in my phone. I find that getting tasks out into a system helps clear out head space to make me better able to concentrate on what I’m working on (and, let’s face it- otherwise I’d forget stuff all the time.)
This is often the case when working on a server, as well. We want the ability to schedule tasks because it cuts down on manual effort, ensures that tasks are carried out, and because no one wants to get up at 2 AM just to trigger a regression script!
Scheduling in Linux is very straightforward, and takes advantage of the “Cron” Linux utility. Cron is a daemon, which means it will constantly run in the background once it is begun initially. The Cron program picks up tasks you assign to it - “Cron Jobs” - and carries them out at the appointed date. Think of it as your tiny, quiet, and powerful Linux butler-ninja.
The easiest way to register cron jobs is to add them to a file called “crontab”. You can open this file up for editing with a simple
crontab -e. If you just want to read your crontab file, enter
crontab -l instead. * Note: this will open up the crontab file for the currently logged in user.
Lines beginning with
# are comments; any lines beginning with a space, a tab, or
# will automatically be ignored.
Cron jobs in the
crontab file contain the following syntax:
* * * * * path/to/file/filetorun.sh
There are two main parts to this entry: the scheduling information contained in the five asterisks or numbers (also called a cron expression)
* * * * * and the command to be executed
The five asterisks here represent the following:
Asterisks simply tell the system to run on every instance of that time slot; thus, the cron expression
* * * * * will fire off the cron job every minute of every hour on every day of the month on every month, every day of the week. In order to be more specific, simply replace the
* with a number, a range (eg
1-7), a list, (eg
2,4,8), or a step (
*/5, fires every 5th time).
Here are some examples:
0 1,13 * * * path/to/command
Fires the command at 1 AM and 1 PM every day
*/10 * * * * path/to/command
Fires the command every 5 Minutes
0 6 20-24 1,7 *
Fires the command at 6 AM every day from the 20th-24th in January and July.
0 19 * * 1,3,5
Fires the command at 7 PM every Monday, Wednesday, and Friday.
In addition to this syntax, some implementations support predefined definitions like
@weekly in place of a cron expression.
If you want a cron job to trigger multiple commands, you can separate the commands with a semi-colon. Commands that must be executed in a specific sequence, however, should be separated by a double ampersand (&&) and listed in their desired order.
Environment variables can also be set inside a crontab file; these are declared directly :
[VARIABLE_NAME] = VARIABLE.
I hope this article has helped you gain a good overall idea of how to start implementing cron jobs on your server. For more information on setting up cron jobs, you can check out a more detailed article here: www.unixgeeks.org/security/newbie/unix/cron-1.html