Linux 定时任务crontab

crontab定时任务格式

1 * * * * * command
2 第1列表示分钟1~59 每分钟用*或者 */1表示
3 第2列表示小时1~23(0表示0点)
4 第3列表示日期1~31
5 第4列表示月份1~12
6 第5列标识号星期0~6(0表示星期天)
7 第6列要运行的命令

crontab的时间颗粒度为分钟,要想实现秒级任务只需使用sleep命令。如每个30秒一次:

1 * * * * * command
2 * * * * * sleep 10; command
3 * * * * * sleep 20; command
4 * * * * * sleep 30; command
5 * * * * * sleep 40; command
6 * * * * * sleep 50; command
7  

crontab调试

有时我们可能在加入了一条crontab定时指令,但是这条指令死活不能执行成功,此时你需要使用下面这条指令来窥探执行的细节

tail -f /var/log/cron

tail其实是一条Linux指令。有些时候我们只需通过查看日志文件的开头或者结尾几行,就能了解到其最近的工作状态,因此Linux中提供了这样的工具:headtail

head指令可以查看文件的开头几行,通过参数-n 来设置。比如我们需要打印出前8行可以通过

head -n 8 lnmp-install.log

结果为


Press any key to install...or Press Ctrl+c to cancel
 You will install lnmp stack.
nginx-1.10.0
mysql-5.5.48
php-5.4.45
Enable InnoDB: y
Print lnmp.conf infomation...
Download Mirror: http://soft.vpser.net

tail指令可以查看文件的后几行,通过参数-n 来设置。比如打印后几行的时候可以使用

tail -n 8 lnmp-install.log

结果为

nginx (pid 19581 19580 19579) is running...
php-fpm is runing!
 SUCCESS! MySQL running (20099)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN
Install lnmp V1.3 completed! enjoy it.

如果我们需要见识某个文件的时候,可以使用-f 选项。

tail -f /var/log/cron
posted @ 2016-09-26 22:34  WingPig99  阅读(493)  评论(0编辑  收藏  举报