天天看点

centOS7中crontab的使用及错误/bin/sh: root: 未找到命令

方法一

crontab -l #查看任务
crontab -e  #便捷定时编辑任务
systemctl reload crond.service #重启crontab
systemctl start crond.service 
systemctl stop crond.service
systemctl restart crond.service
           

无systemctl

service crond status
service crond restart
service crond start
/sbin/service crond start //启动服务
/sbin/service crond stop //关闭服务
/sbin/service crond restart //重启服务
/sbin/service crond reload //重新载入配置
           

开机启动

vim /etc/rc.d/rc.local 中增加

systemctl start crond.service
           

方法二

vim /etc/crontab

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed
47  14  *  *  * root /home/local/mongodel.sh > /home/local/log/mongodel.log 2>&1
           

重新装载生效

crontab /etc/crontab 
           

错误

/bin/sh: root: 未找到命令
           
crontab -e
*/5  *  *  *  * /home/word2pdf.sh > /home/log/word2pdf.log 2>&1 #正确
*/5  *  *  *  * root /home/word2pdf.sh > /home/log/word2pdf.log 2>&1 #错误 报错/bin/sh: root: 未找到命令
           
vim /etc/crontab
*/5  *  *  *  * root /home/word2pdf.sh > /home/log/word2pdf.log 2>&1 #正确
           

注意:记得使用绝对路径,否则任务可能不执行,执行PATH=/sbin:/bin:/usr/sbin:/usr/bin路径下命令

继续阅读