linux下crontab命令可以执行定时任务,但最小粒度为1min,可对其合理运用,以实现秒级别的粒度。
以下代码是实现周期为20s的定时任务,两种方式均可,但稍有不同。
修改用户的日常任务:crontab -e
* * * * * python test.py
* * * * * sleep 20; python test.py
* * * * * sleep 40; python test.py
修改系统的定时任务:vim /etc/crontab
* * * * * root python test.py
* * * * * root sleep 20 && python test.py
* * * * * root sleep 40 && python test.py
方法二:自定义定时执行的脚本
写个后台运行的脚本一直循环运行,然后每次循环sleep 20s。
while true ;do
command
sleep 20 //间隔秒数
请您注册登录超级码客,加载全部码客文章内容... |