Article directory
- 1 What is Cron?
- 2 How is Crontab used?
- 3 Crontab Execute Scheduled Task Command Example
- 3.1 Example 1: Execute myCommand every 1 minute
- 3.2 Example 2: Execution on the 3rd and 15th minutes of every hour
- 3.3 实例3:在上午8点到11点的第3和第15分钟执行
- 3.4 实例4:每隔两天的上午8点到11点的第3和第15分钟执行
- 3.5 实例5:每周一上午8点到11点的第3和第15分钟执行
- 3.6 Example 6: Restart smb at 21:30 every night
- 3.7 实例7:每月1、10、22日的4 : 45重启smb
- 3.8 Example 8: Restart smb at 1:10 every Saturday and Sunday
- 3.9 Example 9: Restart smb every 18 minutes between 00:23 and 00:30 every day
- 3.10 Example 10: Restart smb every Saturday at 11:00 pm
- 3.11 Example 11: Restart smb every hour
- 3.12 Example 12: Restart smb every hour between 11pm and 7am
- 4 How to delete specified Crontab task?
LinuxThe built-in cron process can help us meet the needs of executing scheduled tasks. By using cron and shell scripts, there is no problem in regularly executing very complex task commands.
What is Cron?
What we often use iscrontabcommand, which is short for cron table.
It is the configuration file for cron, which can also be called the job list.
We can find the relevant 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 scripts in /etc/cron.hourly, /etc/cron.daily, /etc/cron.weekly, /etc/cron.monthly directories, let it execute every hour/day/week, month.
How is Crontab used?
Our commonly used commands are as follows:
crontab [-u username] //省略用户名表示操作当前用户的crontab -e (编辑工作表) -l (列出工作表里的命令) -r (删除工作表)
we usecrontab -e
Enter the worksheet editing of the current user, which is a common vim interface.Each line is a command.
Edit Worksheet▼
crontab -e
List worksheets▼
crontab -l
Delete worksheet ▼
crontab -r
The command of crontab is composed of time + action, and its time isminute, hour, day, month, weekFive, the operator has
- * all numbers in the range
- / how many numbers
- - from X to Z
- .hash numbers
Crontab Execute Scheduled Task Command Example
Example 1: Execute myCommand every 1 minute
* * * * * myCommand
Example 2: Execution on the 3rd and 15th minutes of every hour
3,15 * * * * myCommand
实例3:在上午8点到11点的第3和第15分钟执行
3,15 8-11 * * * myCommand
实例4:每隔两天的上午8点到11点的第3和第15分钟执行
3,15 8-11 */2 * * myCommand
实例5:每周一上午8点到11点的第3和第15分钟执行
3,15 8-11 * * 1 myCommand
Example 6: Restart smb at 21:30 every night
30 21 * * * /etc/init.d/smb restart
实例7:每月1、10、22日的4 : 45重启smb
45 4 1,10,22 * * /etc/init.d/smb restart
Example 8: Restart smb at 1:10 every Saturday and Sunday
10 1 * * 6,0 /etc/init.d/smb restart
Example 9: Restart smb every 18 minutes between 00:23 and 00:30 every day
0,30 18-23 * * * /etc/init.d/smb restart
Example 10: Restart smb every Saturday at 11:00 pm
0 23 * * 6 /etc/init.d/smb restart
Example 11: Restart smb every hour
* */1 * * * /etc/init.d/smb restart
Example 12: Restart smb every hour between 11pm and 7am
* 23-7/1 * * * /etc/init.d/smb restart
How to delete specified Crontab task?
SSH enter the following crontab command ▼
crontab -e
- Assuming multiple tasks, delete the specified scheduled task in vim (move the cursor to the configuration line to be deleted, press the delete key to delete)
按 :wq
save and exit
Check to see if the Crontab task was deleted?
crontab -l
- It is found that the crontab scheduled task that has just been deleted does not exist, which means that the deletion is successful.
CWP Control PanelHow to Set Crontab Scheduled Tasks
- In the scheduled task, add a synchronization command to automatically synchronize the backup files of the CWP control panel to GDrive.
If using the CWP Control Panel, log into the CWP Control PanelOf Server Setting
→ Crontab for root
▼
In "Add Full Custom Cron Jobs", enter the following fully custom cron command ▼
00 7 * * * rclone sync /backup2 gdrive:cwp-backup2 55 7 * * * rclone sync /newbackup gdrive:cwp-newbackup
- (Automatically sync local directory every morning at 7:00 am
/backup2
to the network disk with the configuration name gdrivebackup2
Table of contents) - (Automatically sync local directory every morning at 7:55 am
/newbackup
to the network disk with the configuration name gdrivecwp-newbackup
Table of contents) - SynchronizeWordPressFor website files, it is recommended not to back up incrementally, because the test found that if the file names are the same, but the contents of the files are different, they will not be synchronized.
due to timing startcloneAfter the automatic synchronization is completed, the rclone process may still run in the background, which will occupy up to 20% of CPU resources, resulting in a waste of server resources.
Therefore, it is necessary to add a fully customized scheduled task command to force the rclone process to close ▼
00 09 * * * killall rclone
- (Automatically forcibly close the rclone process at 7:00 every morning)
If the CWP control panel cannot edit Crontab scheduled tasks, you can use FTPsoftwareopen a file /var/spool/cron/
Edit Crontab timed tasks directly.
How to use rclone backup for VPS?Please click the link below to viewCentOSUsing GDrive Automatic Sync Tutorial▼
Hope Chen Weiliang Blog ( https://www.chenweiliang.com/ ) shared "Linux Crontab Scheduled Script Task Command & Setting Configuration File Usage", which is helpful to you.
Welcome to share the link of this article:https://www.chenweiliang.com/cwl-1429.html
Welcome to the Telegram channel of Chen Weiliang's blog to get the latest updates!
📚 This guide contains huge value, 🌟This is a rare opportunity, don’t miss it! ⏰⌛💨
Share and like if you like!
Your sharing and likes are our continuous motivation!