Using and configuring CRON

Cron — task scheduler in UNIX-like operating systems, used for periodic execution of tasks at a certain time.

The file is located at the /etc/crontab address, it adds the lines with the commands that need to be executed automatically, and the commands can be placed in a separate file in the /etc/cron.d/ directory, /etc/cron.daily/, /etc/cron.hourly/, /etc/cron.monthly/, /etc/cron.weekly/.

Example of an added line in cron:

* * * * * command

Startup time table:
– – – – –
| | | | |
| | | | —– Day of the week (0 – 7) (Sunday =0 или =7)
| | | ——- Month (1 – 12)
| | ——— Day (1 – 31)
| ———– Hour (0 – 23)
————- Minute (0 – 59)

I’ll give a few examples of the execution time of the commands:

* * * * *
Every minute

*/5 * * * * 
Every 5 minutes

*/30 * * * *
Every 30 minutes

0 * * * *
Every hour

30 * * * *
Every hour in 30 minutes

0 */2 * * *
Every 2 hours

30 */2 * * *
Every 2 hours in 30 minutes

59 23 31 12 5
One minute before the end of the year, if the last day of the year is Friday

59 23 31 Dec Fri
A minute before the end of the year, if the last day of the year is Friday (another version of the entry)

45 17 7 6 *
Every year on the 7th of June at 17:45

0,15,30,45 0,6,12,18 1,15,31 * 1-5
At 00:00, 00:15, 00:30, 00:45, 06:00, 06:15, 06:30, 06:45, 12:00, 12:15, 12:30, 12:45, 18:00, 18:15, 18:30, 18:45, if now the 1st, 15th or 31st day of any month and only on weekdays of the week

*/15 */6 1,15,31 * 1-5
В 00:00, 00:15, 00:30, 00:45, 06:00, 06:15, 06:30, 06:45, 12:00, 12:15, 12:30, 12:45, 18:00, 18:15, 18:30, 18:45, if now the 1st, 15th or 31st day of any month and only on the working days of the week (another version of the record)

0 12 * * 1-5 (0 12 * * Mon-Fri)
At noon on workdays

* * * 1,3,5,7,9,11 *
Every minute in January, March, May, July, September and November

1,2,3,5,20-25,30-35,59 23 31 12 *
On the last day of the year at 23:01, 23:02, 23:03, 23:05, 23:20, 23:21, 23:22, 23:23, 23:24, 23:25, 23:30, 23:31, 23:32, 23:33, 23:34, 23:35, 23:59

0 9 1-7 * 1
The first Monday of every month, at 9 am

0 9 * * 1 
Every Monday at 9 am

0 0 1 * *
At midnight, the first day, every month

* 0-11 * *
Every minute before noon

30 9 1 * *
On the 1st of every month at 9:30

* * * 1,2,3 *
Every minute in January, February and March

* * * Jan,Feb,Mar *
Every minute in January, February and March

0 0 * * *
Every day at midnight

0 0 * * 3
Every Wednesday at midnight

To ensure that the result of the command does not come to the e-mail at the end of the command, you can add:
>/dev/null 2>&1

Here is an example of the line for adding the script /home/user/scrips.sh to Cron for autorun every day at midnight (the file must be executable):
0 0 * * * /home/user/script.sh >/dev/null 2>&1

To receive notifications by email in case of errors, we specify (by default, messages are sent to the root user):

MAILTO=example@ixnfo.com

You can also execute a script or command only at system startup by specifying @reboot at the beginning of the line, but on some Linux this may not work, so I recommend using rc.local, init.d, etc., or check if @reboot works:

crontab -l
@reboot echo "test" > /home/ixnfo.com/test.txt 2>&1
sudo reboot
cat /home/ixnfo.com/test.txt

See also my articles:
Redirecting mail for the root user
How to write CRON logs to a separate file
Configuring Cron Jobs in cPanel and WHM

Leave a comment

Leave a Reply