您的位置:首页 > 运维架构 > Linux

linux crontab命令用法

2016-07-11 19:12 549 查看
crontab命令是linux提供的配置定时任务的工具

crontab -l:查看现有定时任务

crontab -e:编辑定时任务

格式

*  *  *  *  *  command

分  时  日  月  周  命令

第1列表示分钟1~59 每分钟用*或者 */1表示,*/10表示每10分钟

第2列表示小时1~23(0表示0点)

第3列表示日期1~31

第4列表示月份1~12

第5列标识号星期0~6(0表示星期天)

第6列要运行的命令

*:(星号)表示所有的值,如 ,* 16 5 6 *,表示6月5日16点到17点之间的每一分钟执行一次

,:(逗号)表示可选的值,如,3,4,5 16 5 6 *,表示6月5日16点3分,4分,5分各执行一次

-:(中划线)表示范围,如5-10 16 5 6 *,表示6月5日16点5分到10分,每分钟执行异常

/:(正斜杠)表示频率,如*/10 * * * *,表示每十分钟执行一次

crontab用户权限问题

Linux 下可以通过创建文件 /etc/cron.allow 或者 /etc/cron.deny 来控制权限:

/etc/cron.allow 文件存在,那么只有这个文件中列出的用户可以使用 crontab, 同时 /etc/cron.deny文件被忽略;

/etc/cron.allow 文件不存在,那么文件 /cron.deny 中列出的用户

将不能用使用 crontab

下面这种情况用户lxm没有crontab权限

[root@localhost etc]# cat /etc/cron.deny
lxm
[root@localhost etc]# cat /etc/cron.allow
[root@localhost etc]#
[lxm@localhost ~]$ crontab -l
You (lxm) are not allowed to use this program (crontab)
See crontab(1) for more information


将用户加入cron.allow后,用户lxm有了crontab权限

[root@localhost etc]# cat /etc/cron.allow
lxm
[lxm@localhost ~]$ crontab -l
no crontab for lxm


crontab服务启动与停止

[root@localhost etc]# service crond status
crond (pid  1343) is running...
[root@localhost etc]# service crond stop
Stopping crond: [  OK  ]
[root@localhost etc]# service crond start
Starting crond: [  OK  ]
[root@localhost etc]# service crond restart
Stopping crond: [  OK  ]
Starting crond: [  OK  ]


crontab用户存放目录/var/spool/cron

[root@localhost etc]# ll /var/spool/cron
total 4
-rw-------. 1 lxm lxm 72 Jul 11 18:54 lxm
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  linux