“每天一命令之crontab”的版本间的差异

来自linux中国网wiki
跳到导航 跳到搜索
第48行: 第48行:
  
 
[https://www.cnblogs.com/handle/p/9246197.html  Linux crontab 实现秒级定时任务 ]
 
[https://www.cnblogs.com/handle/p/9246197.html  Linux crontab 实现秒级定时任务 ]
 +
 +
[https://blog.csdn.net/xinyflove/article/details/83178876  crontab的语法规则格式(每分钟、每小时、每天、每周、每月、每年定时执行 规则)]
  
 
[[category:ops]]
 
[[category:ops]]

2020年5月18日 (一) 08:54的版本

秒级定时任务

#5s  执行一次 
* * * * * /usr/bin/curl http://www.test.com
* * * * * sleep 5; /usr/bin/curl http://www.test.com
* * * * * sleep 10; /usr/bin/curl http://www.test.com
* * * * * sleep 15; /usr/bin/curl http://www.test.com
* * * * * sleep 20; /usr/bin/curl http://www.test.com
* * * * * sleep 25; /usr/bin/curl http://www.test.com
* * * * * sleep 30; /usr/bin/curl http://www.test.com
* * * * * sleep 35; /usr/bin/curl http://www.test.com
* * * * * sleep 40; /usr/bin/curl http://www.test.com
* * * * * sleep 45; /usr/bin/curl http://www.test.com
* * * * * sleep 50; /usr/bin/curl http://www.test.com
* * * * * sleep 55; /usr/bin/curl http://www.test.com


这样写就实现了每10秒执行一次,用了5个计划任务。如果要实现每5秒,甚至是每1秒执行一次,就要写59行计划任务了,显然太繁琐,可以用一个脚本的方式实现:

#!/bin/bash
step=1  #间隔的秒数,不能大于60
for ((i=0;i<60;i=(i+step)));do
    $(date>>/mnt/file) #here you command
    sleep $step
done
exit 0

知识点

man 5  crontab 

   field          allowed values
    -----          --------------
    minute         0-59
    hour           0-23
    day of month   1-31
    month          1-12 (or names, see below)
    day of week    0-7 (0 or 7 is Sun, or use names)


see also

每天一个linux命令(50):crontab命令

crontab实现秒级的计划任务

Linux crontab 实现秒级定时任务

crontab的语法规则格式(每分钟、每小时、每天、每周、每月、每年定时执行 规则)