今日在一台阿里云主机使用crontab,发现脚本无法执行,状态:
- shell脚本已经有执行权限
chmod +x xxx.sh
- sh文件第一行加上了she-bang:
#!/bin/bash
- 确认 crond 服务正常运行(service crond status)
- crontab 文件正确无误
- 脚本可以手动执行但不能自动执行
查错:
tail /var/log/cron
发现如下错误:
Mar 19 23:03:01 Aliyun crond[613]: (root) RELOAD (/var/spool/cron/root)
Mar 19 23:03:01 Aliyun CROND[32696]: (root) CMD (/root/move_pic.sh)
Mar 19 23:03:01 Aliyun CROND[32695]: (root) MAIL (mailed 54 bytes of output but got status 0x004b#012)
然后看/var/log/maillog
文件:
cat /var/log/maillog
#返回如下信息:
Mar 19 23:03:01 AliyunHZ1 postfix/sendmail[32698]: fatal: parameter inet_interfaces: no local interface found for ::1
搜索一番,这样解决:
- 编辑
/etc/postfix/main.cf
文件,把inet_interfaces = all
这一行前面的注释符号取消。 - 注释掉
inet_interfaces = localhost
service postfix start
此时可以正常发送mail了。如果脚本执行错误可以通过mail
命令查看邮件。在mail程序中,输入序号查看邮件,输入d删除邮件,输入q退出。
根本原因:
脚本错误->需要将错误信息发送到mail->设置错误导致无法发送mail->cat /var/log/cron
中提示发送mail错误
但是我的脚本可以手动执行又是怎么回事?
因为我用了这样的句子:
mv $uploadpath/*.* /var/ftp/ftppath/
这样的句子在crontab中被扩展了,所以改为类似下面的句子就好了:
mv /abs/path/*.jpg /var/ftp/ftppath/