Windows comes with a tool to execute tasks regularly called "Scheduled Tasks". Under Linux, we use Cron to achieve this function.
apt-get install cron
pgrep cron
service cron start
crontab -e
To open the crontab file that your user belongs to. The first time you use this command, you will be asked to choose a text editor. I chose vim. The selected editor is also available
You can also change select-editor
by yourself every time
After each crontab is saved, we also need to restart cron to apply this scheduled task. Use the command: sudo service cron restart
All cron task plans are recorded in the crontab task plan file, and the task file is managed through the crontab command.
$ crontab -u root -e #Edit the scheduled task file of user root
$ crontab -e #Edit the current user's scheduled task file
$ crontab -u root -l #Display the scheduled task file of user root
$ crontab -l #Display the current user's scheduled task file
$ crontab -r #Delete the current user's scheduled task file
m h dom mon dow command
0- 590- 231- 311- 120- 7 command
In addition, you need to use some special symbols to achieve flexible configuration:
* Represents all values
/ Stands for "every"
- Representative range
, Split number
## Specify specific execution time
2**** ls #Execute the ls command once every second minute of the hour
307*** ls #Execute the ls command once every day at 7:30
3020**2 ls #Every Tuesday, execute the ls command at 20:30 (0 and 7 indicate Sunday)
## Specify interval time
* /2**** ls #Execute the ls command every 2 minutes
## Specify time period
3073- 6** ls #Execute the ls command at 7:30 on the 3, 4, 5, and 6th of each month
## Specify multiple times
3073,6** ls #Execute the ls command at 7:30 on the 3rd and 6th of each month
In addition, use run-parts to run all scripts in the specified directory (note that the script must be added "#!/bin/bash", otherwise run-parts will fail to call)
307*** run-parts /home #Run every day at 7:30/All scripts in the home directory
Recommended Posts