天天看點

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路徑下指令

繼續閱讀