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 机器上的定时任务列表(l 是 list 的首字母)
crontab -e : 是编辑定时任务(e 是 edit 的首字母)
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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)