crontab 命令详情

安装crontab

yum install crontabs

查看定时任务

crontab -l

设置定时任务

crontab -e

注意:centos7下修改定时任务crontab -e的时候,控制台输出“crontab: installing new crontab”,表示任务添加成功,且之后三分钟不会执行对应的任务,所以在调试定时器的时候,要把这三分钟考虑进去,如果执行的时间在三分钟内的话,对应的任务则不会生效。原因(使用crontab -e编写需要等3分钟出现效果)

添加任务时遇到异常 Operation not permitted

1 [root@root bin]# crontab -e
2 no crontab for root - using an empty one
3 crontab: installing new crontab
4 /var/spool/cron/#tmp.root.XXXXt2axj2: Operation not permitted
5 crontab: edits left in /tmp/crontab.lnU8I7

解决方案:

1 [root@root bin]# lsattr /var/spool/cron/
2 [root@root bin]# chattr -ai /var/spool/cron

 

查看crontab状态

systemctl status crond.service

启动crontab

systemctl start crond.service

关闭crontab

systemctl stop crond.service

重启crontab

systemctl restart crond.service

定时任务时间设置

# 修改/添加定时任务
crontab -e
# 每分钟把hello world字符串写入/home/string.txt文件中
* * * * * echo "hello world" >> /home/string.txt
更多案例
# 修改/添加定时任务
crontab -e

# 每天3点执行
* 3 * * * 执行的语句

# 每天18点30分执行
30 18 * * * 执行的语句

# 每个月的1号的12点15分执行
15 12 1 * *  执行的语句

# 每年的3月1号的11点45分执行
45 11 1 3 * 执行的语句

# 每周三的16点30分执行
30 16 * * 3 执行的语句


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

摘自:https://blog.csdn.net/CRMEB/article/details/124117583

posted @ 2023-01-05 15:16  RyderWang  阅读(118)  评论(0编辑  收藏  举报