How to configure cron.


Cron scripts are configured via unix shell. First of all, you need to determine which programs you want to run and find out the full paths to them on the server disk. To do this, use the cd command to navigate to the directory where the program is being run and find out the full path to this directory using the pwd command. The path may look for example like /home/u12345/scripts/script.pl . Make sure that the file you want to run has read+execute rights (r+x) for the file owner.


You can change the rights to the necessary ones with the command:


chmod 750 script.pl

Next, run the command crontab -e. You will find yourself in the vi text editor, where you can enter the script text for cron. Brief information on the vi editor:



  • to insert text, press i, then enter the text

  • to delete characters, press ESC, and then type x

  • to exit vi without saving changes, press ESC, and then type :q!

  • to save and exit, press ESC, and then type :wq


Tasks for cron are written one per line. After each line, including after the last or only one, you must press enter — otherwise the tasks will not work.


The task for cron looks like a string, at the beginning there are five mandatory fields to indicate the frequency of the task, and then follows the command to run:


field 1 field 2 field 3 field 4 field 5 command

Values of the first five fields:



  1. minutes — a number from 0 to 59

  2. hours — a number from 0 to 23

  3. day of the month — a number from 1 to 31

  4. the number of the month in the year is a number from 1 to 12

  5. day of the week — a number from 0 to 7 (0-Sun,1-Mon,2-Tue,3-Wed,4-Thu,5-Fri,6-Sat,7-Sun)


Several comma-separated values can be set for each specific parameter. For example, if you write 1,4,22 in the "hours" field, then the task will be launched at 1 a.m., 4 a.m. and 22 p.m. You can set the interval — 4-9 will mean that the program needs to be run every hour in the period from 4 to 9 hours inclusive. The symbol '*' means "all possible values". For example, specifying '*' in the "clock" field would mean "run every hour". The symbol '/' is used to indicate the additional frequency of the task. For example, '*/3' in the "hours" field means "every three hours".


So, what does the simplest cron script look like:


0 */3 * * 2,5 /home/u12345/script.pl

Script /home/u12345/script.pl it will automatically start every three hours on Tuesday and Friday. By entering such a scenario in the vi editor, exit with the editing results saved and, if you have not made any mistakes, the task will be put to execution with the specified frequency. If mistakes were made during editing, cron will inform you about them.


For example:


/tmp/crontab.xxxxxxx: 1 lines, 9 characters
crontab: installing new crontab
"/tmp/crontab.xxxxxxx":1: bad minute
crontab: errors in crontab file, can't install
Do you want to retry the same edit?

Fix the errors and try to save the task again.


You can view the list of scripts already installed in cron with the command crontab -l:


-bash-2.05b$ crontab -l
0 */3 * * 2,5 /home/u12345/script.pl

Recommendation: if you need to run a program once a day, especially if it requires large resources to perform, perform such a task at night, in the period from 2 to 8 hours - the load on the servers at this time is minimal.



The following are examples of tasks for cron. We hope this information will help you better understand the work of this program.


# perform the task once an hour in 0 minutes
0 */1 * * * /home/u12345/script.pl

# perform a task every three hours in 0 minutes
0 */3 * * * /home/u12345/script.pl

# complete the task on Mondays at 1:15 a.m.
15 1 * * 1 /home/u12345/script.pl

# complete the task on April 5 at 0:1 every year
1 0 5 4 * /home/u12345/script.pl

# complete the task on Friday the 13th numbers at 13 hours 13 minutes
13 13 13 * 5 /home/u12345/script.pl

# perform the task monthly on the 1st at 6 hours 10 minutes
10 6 1 * * /home/u12345/script.pl