How To: Automate Server Scripts With Cron

Servers can automatically perform tasks that you would otherwise have to perform yourself, such as running scripts. On Linux servers, the cron utility is the preferred way to automate the running of scripts.

There are two main ways to get cron to run a script. The first is to place a script into one of the following directories:

  • /etc/cron.hourly/
  • /etc/cron.daily/
  • /etc/cron.weekly
  • /etc/cron.monthly/

Any script put into these directories will run once per hour, once per day, once per week, or once per month. These scripts will run as the root user, without any special flags.

Cron also allows for scripts to be run by users other than root. Instead of placing the scripts in a watched directory, each user (including root) has what is called a crontab file.

Editing Crontab Files

While it is tempting to directly open your user’s crontab file with the text editor of your choice, never do this. Changes you make will not be picked up by the cron daemon. Instead, run the following command:

crontab -e

This command opens a temporary cron file in your system’s default text editor (the value of the $EDITOR shell variable). If your user’s crontab already has entries, you should see lines like this:

1 0 * * * echo -n "" > /home/user/application_logs/error_log
*/5 * * * * /home/user/test.pl

Each line is commonly referred to as a cronjob. The five columns on the left of a cronjob set the schedule for the command that is run on the right. Each of the first five columns specifies a time, such as minutes, hours, day of the month, etc.:

.---------------- minute (0 - 59)
| .------------- hour (0 - 23)
| | .---------- day of month (1 - 31)
| | | .------- month (1 - 12) OR jan,feb,mar,apr ...
| | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
| | | | |
* * * * * command to be executed

A “*” in a column means that the command will run every time that unit is reached. So, if all columns are stars, the command will run once every minute.

If only a number is specified in a column, then the command will be run on that number of the unit only. For example, the following command will run once a day at 1pm server time:

0 13 * * * /home/user/example.sh

If you want the script to run more than once per day, you can specify multiple hours using commas:

* 2,13,21 * * * /home/user/example.sh

Or you can specify a range with a “-“:

* 2-6 * * * /home/user/example.sh

You can also follow a “*” with “/” and a number to specify a frequency that does not naturally fall into one of the columns. For example, the following will run the script every five minutes:

*/5 * * * * /home/user/example.sh

Tips And Tricks

  1. The letters “e” and “r” are right next to each other on most keyboards. Make sure you do not accidentally run “crontab -r”. It will delete all of that user’s cronjobs without warning.
  2. Make sure that any script you want to run has executable permissions set for the user.
  3. It is a good idea to specify the full path to a script. Scripts that start with “~/” instead of “/home/user/” may not work.

If you are having trouble getting a particular cron to run, do not hesitate to contact our Heroic SupportĀ® team for assistance.

Be Sociable, Share!
Here's $75, Launch a New VPS Today. Find out why 30,000 customers have chosen our Best-in-Class Performance & 24x7 Heroic Support.