cron scheduled task
Introduction
cron is a service provided by the linux system to perform scheduled tasks. It runs in the background and schedules the execution of specified tasks according to the time through the configuration file "crontab". These timing tasks can be divided into two types, one is system tasks and the other is user tasks.
Timing execution principle
When cron starts, the configuration files in the /var/spool/cron/crontabs directory and the /etc/crontab configuration file are loaded into the memory. Then, the cron service process checks these crontab s every minute to see which commands need to be executed in this minute. If there is something that needs to be executed, execute the relevant command. In addition, cron also detects the modification time of the configuration file and reloads all crontabs if it changes, so we don't have to restart the service after modifying the crontab to activate these scheduled tasks.
cron configuration file
- The /var/spool/cron/ directory stores crontab tasks for each user including root, and each task is named after the creator
- /etc/crontab This file is responsible for scheduling various system administration and maintenance tasks.
- /etc/cron.d/ This directory is used to store any crontab files or scripts to be executed.
- We can also put the configuration file in
/etc/cron.hourly, /etc/cron.daily, /etc/cron.weekly, /etc/cron.monthly directory, let it execute every hour/day/week/month.
cron syntax
* * * * * command to be executed - - - - - - | | | | | | | | | | | --- pre-executed command | | | | ----- means week 0~7(where Sunday can be represented by 0 or 7) | | | ------- Indicates month 1~12 | | --------- show date 1~31 | ----------- Indicates hour 1~23(0 means 0 points) ------------- Indicates minute 1~59 per minute*or */1 express
System tasks
These cron tasks are used by system services and critical tasks and require root-level privileges to execute. You can view system-level cron jobs in the /etc/crontab file
[root@localhost etc]# cat /etc/crontab SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root # For details see man 4 crontabs # 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 # | | | | | # * * * * * user-name command to be executed * * * * * root /root/test.sh
User task
User-level cron tasks are separate for each user. So each user can create their own cron jobs using the crontab command and also edit or view their own cron jobs using the following commands.
Use the crontab -e command to edit user-defined cron tasks.
# use /bin/bash to run commands, instead of the default /bin/sh SHELL=/bin/bash # mail any output to `paul', no matter whose crontab this is MAILTO=paul # # run five minutes after midnight, every day 5 0 * * * $HOME/bin/daily.job >> $HOME/tmp/out 2>&1 # run at 2:15pm on the first of every month — output mailed to paul 15 14 1 * * $HOME/bin/monthly # run at 10 pm on weekdays, annoy Joe 0 22 * * 1–5 mail -s "It's 10pm" joe%Joe,%%Where are your kids?% 23 0–23/2 * * * echo "run 23 minutes after midn, 2am, 4am ..., everyday" 5 4 * * sun echo "run at 5 after 4 every Sunday" 0 */4 1 * mon echo "run every 4th hour on the 1st and on every Monday" 0 0 */2 * sun echo "run at midn on every Sunday that's an uneven date" # Run on every second Saturday of the month 0 4 8–14 * * test $(date +\%u) -eq 6 && echo "2nd Saturday" All the above examples run non-interactive programs. If you wish to run a pro‐ gram that interacts with the user's desktop you have to make sure the proper environment variable DISPLAY is set. # Execute a program and run a notification every day at 10:00 am 0 10 * * * $HOME/bin/program | DISPLAY=:0 notify-send "Program run" "$(cat)"