Fork me on GitHub

Linux crontab 每5秒钟执行一次 shell 脚本 的方法

Linux crontab 每5秒钟执行一次 shell 脚本 的方法


 

由于 Linux 的 crontab 的定时命令格式如下:

 minute hour day-of-month month-of-year day-of-week commands

意味着标椎定时任务中,最小定时周期是分钟。

但是,由于实际应用中,可能需要每5秒就要求执行某个shell脚本。

该如何实现呢?


 

本文中提供如下方式;

间隔调用shell命令

使用 crontab -e 命令编辑定时任务

实现每5秒定时执行脚本,crontab 定时脚本如下(根据自己的实际情况,只需要将&& 后面的部分,替换成自己需要的脚本执行命令即可):

复制代码
*/1 * * * * /bin/date >>/tmp/date.txt
*/1 * * * * sleep 5  && /bin/date >>/tmp/date.txt
*/1 * * * * sleep 10 && /bin/date >>/tmp/date.txt
*/1 * * * * sleep 15 && /bin/date >>/tmp/date.txt
*/1 * * * * sleep 20 && /bin/date >>/tmp/date.txt
*/1 * * * * sleep 25 && /bin/date >>/tmp/date.txt
*/1 * * * * sleep 30 && /bin/date >>/tmp/date.txt
*/1 * * * * sleep 35 && /bin/date >>/tmp/date.txt
*/1 * * * * sleep 40 && /bin/date >>/tmp/date.txt
*/1 * * * * sleep 45 && /bin/date >>/tmp/date.txt
*/1 * * * * sleep 50 && /bin/date >>/tmp/date.txt
*/1 * * * * sleep 55 && /bin/date >>/tmp/date.txt
复制代码

 

执行效果如下:


 

额外补充

crontab -l : 是查看当前 Linux 机器上的定时任务列表(llist 的首字母)

crontab -e : 是编辑定时任务(eedit 的首字母)

crontab 命令的具体使用说明如下:

复制代码
Usage:
 crontab [options] file
 crontab [options]
 crontab -n [hostname]

Options:
 -u <user>  define user
 -e         edit user's crontab
 -l         list user's crontab
 -r         delete user's crontab
 -i         prompt before deleting
 -n <host>  set host in cluster to run users' crontabs
 -c         get host in cluster to run users' crontabs
 -s         selinux context
 -x <mask>  enable debugging
复制代码

 

posted @   龙凌云端  阅读(16086)  评论(1编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示