Introduction to cron
What we often use is the crontab command, which is short for cron table, which is the configuration file of cron, and it can also be called the job list. We can find related configuration files in the following folders.
- 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 administrative 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 script in the /etc/cron.hourly, /etc/cron.daily, /etc/cron.weekly, /etc/cron.monthly directories and let it execute every hour/day/week, month.
Use of crontab
Commonly used commands are as follows:
crontab [-u username] // Omitting the user table means operating the current user's crontab - e (edit worksheet) -l (list commands in worksheet) -r (delete work)
We use crontab -e to enter the current user's worksheet editing, which is a common vim interface. Each line is a command.
The command of crontab is composed of time + action, and its time is divided into , hours, days, months, and Fridays, and the operators are
- * all numbers in the range
- / every number of digits passed
- - from X to Z
- , the hashed number
example
Example 1: Execute myCommand every 1 minute
* * * * * myCommand
Example 2: Execution on the 3rd and 15th minutes of every hour
3,15 * * * * myCommand
Example 3: Execute at the 3rd and 15th minutes from 8am to 11am
3,15 8-11 * * * myCommand
Example 4: Execute every two days at the 3rd and 15th minutes from 8:00 am to 11:00 am
3,15 8-11 */2 * * myCommand
Example 5: Execute every Monday at the 3rd and 15th minutes from 8:00 am to 11:00 am
3,15 8-11 * * 1 myCommand
Example 6: Restart smb at 21:30 every night
30 21 * * * /etc/init.d/smb restart
Example 7: Restart smb at 4:45 on the 1st, 10th, and 22nd of each month
45 4 1,10,22 * * /etc/init.d/smb restart
View and modify system time under Linux
View current time
Modify system time
Automatically sync time
install ntpdate
wich ntpdate shows the absolute path of the command
find view path
Use timed tasks