因为 Linux 的 crontab 的定时命令格式以下:linux
minute hour day-of-month month-of-year day-of-week commandsshell
意味着标椎定时任务中,最小定时周期是分钟。spa
可是,因为实际应用中,可能须要每5秒就要求执行某个shell脚本。debug
该如何实现呢?code
本文中提供以下方式;blog
使用 crontab -e 命令编辑定时任务crontab
实现每5秒定时执行脚本,crontab 定时脚本以下(根据本身的实际状况,只须要将&& 后面的部分,替换成本身须要的脚本执行命令便可):get
*/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
执行效果以下:it
crontab -l : 是查看当前 Linux 机器上的定时任务列表(l 是 list 的首字母)io
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