Linux crontab and Windows task schedules are very similar. Crontab can be used to execute tasks on a regular basis in the system

For example: regular update of user expired members, regular WeChat official account push messages...

image.png

From left to right:

  • [minute]
  • [Hour]
    -[One day of the month]
    -[Month of each year]
    -[Day of the week]
    -[Command executed]

Note: Please pay attention to the value range of each option

Add/Edit Crontab

Add or update commands in crontab

crontab -e

By default, the system will edit the crontab command set of the currently logged in user. Need to edit the command set of other users, you need to use the following commands

crontab -u username -e

View Crontab

View the Crontab command list of the current system login user

crontab -l

View the list of Crontab commands of other users

crontab -u username -l

10 classic examples

1. Perform tasks at 02:00 every day

0 2 * * * /bin/sh elephdev.sh

2. Perform tasks at 5:00 and 17:00 every day

0 5,17 * * * /scripts/script.sh

3. Perform a task every minute

* * * * * /scripts/script.sh

4. Perform tasks every Sunday at 17:00

0 17 * * sun /scripts/script.sh

5. Perform a task every 10 minutes

*/10 * * * * /scripts/script.sh

6. Perform tasks in specific months

* * * jan,may,aug * /script/script.sh

7. Perform tasks on specific days

0 17 * * sun,fri /script/scripy.sh

8. Perform the task on the first Sunday of a month

0 2 * * sun [$(date +%d) -le 07] && /script/script.sh

9. Perform a task every four hours

0 */4 * * * /scripts/script.sh

10. Perform tasks every Monday and Sunday

0 4,17 * * sun,mon /scripts/script.sh

11. Perform a task every 30 seconds

We have no way to directly implement similar examples through appeals, because the smallest is 1min. But we can use the following method

* * * * * /scripts/script.sh
* * * * * sleep 30; /scripts/script.sh

12. Multiple tasks are configured in one command

* * * * * /scripts/script.sh; /scripts/scrit2.sh

13. Execute when the system restarts

@reboot /scripts/script.sh
点赞(0)

评论列表 共有 0 评论

暂无评论

微信服务号

微信客服

淘宝店铺

support@elephdev.com

发表
评论
Go
顶部