cron
-
Configuration file /etc/crontab. Directory for config files /var/spool/cron/
-
The crond daemon calls run-parts to execute scripts from the /etc/cron.* directories.
Upon each activation, the cron daemon checks the modification date of the /etc/crontab file and the /var/spool/cron/ directory. If they have changed, it re-reads all changes and takes them into account in subsequent executions. Since the crontab utility updates the modification date of the /var/spool/cron/ folder after editing the job file, there is no need to restart the cron process after making changes to a user's schedule file; these changes will be taken into account at the next activation. This is precisely why the crontab -e command should be used to edit user schedule files, rather than editing the files directly. The modification date for the /etc/crontab file is checked separately, so it can be modified using a standard editor (root privileges are required for this).
Output generated by executed commands is, by default, forwarded to the mailbox of the schedule file's owner (usually root, whose email is specified in the /etc/aliases file). To cancel this default behavior, use the MAILTO variable. An empty value (MAILTO="") will redirect messages to /dev/null.
The crontab file format also allows the use of predefined values instead of the first five positions, which specify the job execution time:
-
@reboot (execute at operating system startup)
-
@yearly (execute annually at midnight on January 1st)
-
@monthly (execute monthly at midnight on the 1st of each month)
-
@weekly (execute at midnight every Monday)
-
@daily (execute daily at 0:00)
-
@hourly (execute at the beginning of every hour).
View the crontab file for the www-data user. The -e option allows editing the www-data user's crontab file.
# crontab -u www-data -e # редактировать содержимое файла crontab # crontab -u www-data -l # отобразить содержимое файла crontab
# Example of job definition: # .---------------- 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
Examples:
-
Run the program every two days at 10:00 and send a report to the email address specified in the MAILTO variable
MAILTO=noc@example.net 0 10 */2 * * root /usr/local/repl/restart_repl.sh
-
Run the script on the 1st and 15th of every month at 2:05, as well as on Sundays (Day and WDay operate in "OR" mode)
5 2 1,15 * Sun /home/script
