一、背景
crontab基本命令介绍:
crontab -e :编辑crontab定时任务
crontab -l :查看crontab定时任务里面所有的任务
二、解决思路
首先查看crontab 日志
cat /var/log/cron
Jul 27 02:00:01 iZ88bfrkh86Z CROND[16210]: (aaron) MAIL (mailed 43 bytes of output but got status 0x004b
)
出现如上错误信息,怀疑是因为crontab出现问题。
接着继续命令查看crontab运行状态信息
[root@iZ88bfrkh86Z mail]# service crond status
Redirecting to /bin/systemctl status crond.service
crond.service - Command Scheduler
Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled)
Active: active (running) since Fri 2017-06-02 16:55:02 CST; 1 months 24 days ago
Main PID: 770 (crond)
CGroup: /system.slice/crond.service
└─770 /usr/sbin/crond -n
Jul 27 10:27:01 iZ88bfrkh86Z crond[770]: sendmail: fatal: parameter inet_interfaces: no local interface found for ::1
可以看出是mail出了问题
vi /etc/postfix/main.cf
发现配置为:
inet_interfaces = localhost
inet_protocols = all
改成:
inet_interfaces = all
inet_protocols = all
重新启动
service postfix start
再次查看crontab运行状态信息
[root@iZ88bfrkh86Z mail]# service crond status
Redirecting to /bin/systemctl status crond.service
crond.service - Command Scheduler
Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled)
Active: active (running) since Fri 2017-06-02 16:55:02 CST; 1 months 24 days ago
Main PID: 770 (crond)
CGroup: /system.slice/crond.service
└─770 /usr/sbin/crond -n
Jul 27 10:27:01 iZ88bfrkh86Z crond[770]: sendmail: fatal: parameter inet_interfaces: no local interface found for ::1
Jul 27 11:04:42 iZ88bfrkh86Z systemd[1]: Started Command Scheduler.
再次调用crontab,查看运行日志
[aaron@iZ88bfrkh86Z ~]$ cat /var/spool/mail/aaron
From aaron@iZ88bfrkh86Z.localdomain Thu Jul 27 11:12:01 2017
Return-Path:
X-Original-To: aaron
Delivered-To: aaron@iZ88bfrkh86Z.localdomain
Received: by iZ88bfrkh86Z.localdomain (Postfix, from userid 1000)
id 26D6480FDD; Thu, 27 Jul 2017 11:12:01 +0800 (CST)
From: "(Cron Daemon)"
To: aaron@iZ88bfrkh86Z.localdomain
Subject: Cron sh /home/aaron/auto.sh >/home/aaron/log
Content-Type: text/plain; charset=UTF-8
Auto-Submitted: auto-generated
Precedence: bulk
X-Cron-Env:
X-Cron-Env:
X-Cron-Env:
X-Cron-Env:
X-Cron-Env:
X-Cron-Env:
X-Cron-Env:
X-Cron-Env:
Message-Id:
Date: Thu, 27 Jul 2017 11:12:01 +0800 (CST)
/home/aaron/auto.sh: line 3: java: command not found
发现错误是因为java环境变量配置引起的
故需要在自己的shell脚本里加入一行代码
source /etc/profile
再次调用crontab ,运行成功