Help Docs Server Administration Linux Server Administration Automating Server Scripts with Cron

Automating Server Scripts with Cron

Learn to automate tasks on your Linux server with cron. This time-based job scheduler can run scripts at fixed times, dates, or intervals.

Linux operating systems feature a time-based job scheduler called cron. You can use cron to schedule jobs to run at fixed times, dates, or intervals, and also to automate system maintenance or administration.   Additionally, it is also useful for downloading files from the internet or downloading email at regular intervals.

This tutorial will help you set up automated scripts on your servers.  In addition, to see how to view jobs already running on your server, please see our tutorial, Displaying All Jobs in Cron/Crontab.

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/

Each directory indicates when the script will run, hourly, daily, weekly, and monthly. The scripts will run as root user without any special flags. If you do not want the script to run as root, use the crontab file for that user, instead of placing the scripts in a watched directory.

Editing Crontab Files

Warning:

While it is tempting to directly open your user’s crontab file with the text editor of your choice, never do this. Changes made will NOT be picked up by the cron daemon.

Run the following command:

crontab -a

This command opens a temporary cron file in your systems default text editor. 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 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 specify 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

Note:

An asterisk “*” in a column means the command will run every time that unit is reached. So if all columns are asterisks, 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.

This command shows that the job will run once a day at 1pm server time:

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

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.script

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

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

You can also follow an asterisk “*” with a forward slash “/” 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.script

Helpful Tips:

  1. Note! The letters e and r are right next to each other on most keyboards. Make sure you do not accidentally run crontab -r. This 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 that 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.
Was this article helpful?