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