Cron configuration occurs through the unix shell. First of all, you need to determine which programs you want to run and find out the full path to them on the server disk. To do this, cd into the directory where the program you are running is located, and find out the full path to that directory using the pwd command. The path might look like /home/u12345/scripts/script.pl, for example. Make sure the file you are running has read+execute (r+x) permissions for the file’s owner.

You can change the permissions to the ones you need with the command:

chmod 750 script.pl

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

to insert text, press i, then enter 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

Cron jobs 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.

A cron job looks like a line, at the beginning there are five required fields to indicate the frequency of the job, and then there is a command that needs to be run:

field1 field2 field3 field4 field5 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. number of the month in the year – 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)

For each specific parameter, you can specify several values separated by commas. For example, if you write 1,4,22 in the “hours” field, then the task will be launched at 1 a.m., at 4 a.m., and at 10 p.m. You can set an interval – 4-9 will mean that the program needs to be launched every hour in the period from 4 to 9 hours inclusive. The ‘*’ symbol means “all possible values”. For example, specifying ‘*’ in the “hours” field would mean “run every hour”. The ‘/’ character is used to indicate additional frequency of the job. For example, ‘*/3’ in the ‘hours’ field means ‘every three hours’.

So, this is what a simple cron script looks like:

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

The script /home/u12345/script.pl will automatically run every three hours on Tuesday and Friday. Having entered such a script in the vi editor, exit with saving the editing results and, if you have not made any mistakes, the task will be executed at the specified frequency. If errors were made during editing, cron will notify you about them.

Example:

/tmp/crontab.xxxxxxx: 1 line, 9 characters.

crontab: installing a new crontab

"/tmp/crontab.xxxxxxx":1: bad minute

crontab: errors in crontab file, unable to install

Want to repeat the same edit?

Correct the errors and try saving 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 run, run this task at night, from 2 to 8 a.m.—the load on the servers at this time is minimal. Below are examples of cron jobs. We hope this information will help you better understand how this program works.

# perform the task once per hour for 0 minutes

0 */1 * * * /home/u12345/script.pl

#perform the task every three hours at 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 o'clock 1 minute every year

1 0 5 4 * /home/u12345/script.pl

# complete the task on Friday the 13th at 13:13

13 13 13 * 5 /home/u12345/script.pl

#complete the task every month on the 1st at 6:10 am

10 6 1 * * /home/u12345/script.pl