1、使用sleep
在crontab中加入
* * * * * sleep 10; /bin/date >>/tmp/date.txt
* * * * * sleep 20; /bin/date >>/tmp/date.txt
* * * * * sleep 30; /bin/date >>/tmp/date.txt
* * * * * sleep 40; /bin/date >>/tmp/date.txt
* * * * * sleep 50; /bin/date >>/tmp/date.txt
跟踪日志,可以看到是每10秒钟执行一次。
tail -f date.txt
Thu Mar 9 16:00:12 CST 2017
Thu Mar 9 16:00:22 CST 2017
Thu Mar 9 16:00:32 CST 2017
Thu Mar 9 16:00:42 CST 2017
Thu Mar 9 16:00:52 CST 2017
Thu Mar 9 16:01:11 CST 2017
Thu Mar 9 16:01:21 CST 2017
Thu Mar 9 16:01:31 CST 2017
每秒钟执行,
* * * * * sleep 1 ; /bin/date >>/tmp/date.txt
* * * * * sleep 2 ; /bin/date >>/tmp/date.txt
* * * * * sleep 3 ; /bin/date >>/tmp/date.txt
* * * * * sleep 4 ; /bin/date >>/tmp/date.txt
* * * * * sleep 5 ; /bin/date >>/tmp/date.txt
* * * * * sleep 6 ; /bin/date >>/tmp/date.txt
* * * * * sleep 7 ; /bin/date >>/tmp/date.txt
* * * * * sleep 8 ; /bin/date >>/tmp/date.txt
* * * * * sleep 9 ; /bin/date >>/tmp/date.txt
* * * * * sleep 10 ; /bin/date >>/tmp/date.txt
* * * * * sleep 11 ; /bin/date >>/tmp/date.txt
* * * * * sleep 12 ; /bin/date >>/tmp/date.txt
* * * * * sleep 13 ; /bin/date >>/tmp/date.txt
* * * * * sleep 14 ; /bin/date >>/tmp/date.txt
* * * * * sleep 15 ; /bin/date >>/tmp/date.txt
* * * * * sleep 16 ; /bin/date >>/tmp/date.txt
* * * * * sleep 17 ; /bin/date >>/tmp/date.txt
* * * * * sleep 18 ; /bin/date >>/tmp/date.txt
* * * * * sleep 19 ; /bin/date >>/tmp/date.txt
* * * * * sleep 20 ; /bin/date >>/tmp/date.txt
* * * * * sleep 21 ; /bin/date >>/tmp/date.txt
* * * * * sleep 22 ; /bin/date >>/tmp/date.txt
* * * * * sleep 23 ; /bin/date >>/tmp/date.txt
* * * * * sleep 24 ; /bin/date >>/tmp/date.txt
* * * * * sleep 25 ; /bin/date >>/tmp/date.txt
* * * * * sleep 26 ; /bin/date >>/tmp/date.txt
* * * * * sleep 27 ; /bin/date >>/tmp/date.txt
* * * * * sleep 28 ; /bin/date >>/tmp/date.txt
* * * * * sleep 29 ; /bin/date >>/tmp/date.txt
* * * * * sleep 30 ; /bin/date >>/tmp/date.txt
* * * * * sleep 31 ; /bin/date >>/tmp/date.txt
* * * * * sleep 32 ; /bin/date >>/tmp/date.txt
* * * * * sleep 33 ; /bin/date >>/tmp/date.txt
* * * * * sleep 34 ; /bin/date >>/tmp/date.txt
* * * * * sleep 35 ; /bin/date >>/tmp/date.txt
* * * * * sleep 36 ; /bin/date >>/tmp/date.txt
* * * * * sleep 37 ; /bin/date >>/tmp/date.txt
* * * * * sleep 38 ; /bin/date >>/tmp/date.txt
* * * * * sleep 39 ; /bin/date >>/tmp/date.txt
* * * * * sleep 40 ; /bin/date >>/tmp/date.txt
* * * * * sleep 41 ; /bin/date >>/tmp/date.txt
* * * * * sleep 42 ; /bin/date >>/tmp/date.txt
* * * * * sleep 43 ; /bin/date >>/tmp/date.txt
* * * * * sleep 44 ; /bin/date >>/tmp/date.txt
* * * * * sleep 45 ; /bin/date >>/tmp/date.txt
* * * * * sleep 46 ; /bin/date >>/tmp/date.txt
* * * * * sleep 47 ; /bin/date >>/tmp/date.txt
* * * * * sleep 48 ; /bin/date >>/tmp/date.txt
* * * * * sleep 49 ; /bin/date >>/tmp/date.txt
* * * * * sleep 50 ; /bin/date >>/tmp/date.txt
* * * * * sleep 51 ; /bin/date >>/tmp/date.txt
* * * * * sleep 52 ; /bin/date >>/tmp/date.txt
* * * * * sleep 53 ; /bin/date >>/tmp/date.txt
* * * * * sleep 54 ; /bin/date >>/tmp/date.txt
* * * * * sleep 55 ; /bin/date >>/tmp/date.txt
* * * * * sleep 56 ; /bin/date >>/tmp/date.txt
* * * * * sleep 57 ; /bin/date >>/tmp/date.txt
* * * * * sleep 58 ; /bin/date >>/tmp/date.txt
* * * * * sleep 59 ; /bin/date >>/tmp/date.txt
2、使用脚本运行
#!/bin/bash
step=2 #间隔的秒数,不能大于60
for (( i=0; i<60; i=(i+step) )); do
date
sleep $step
done
exit 0
添加到crontab中
crontab -l
* * * * * sh /root/crontab.sh >> /tmp/crontab.log
查看日志
tail -f tail -f /tmp/crontab.log