Centos7的crontab定时任务和错误解决

保证已经安装crontab
yum install crontabs
crontab基本指令
crontab -l   # 列出定时任务列表
crontab -e   # 编辑定时任务
添加需要定时执行的脚本
# test1.sh
#!/bin/bash
source /etc/profile
function test1()
{
	echo "hello world!";
}

test1
# test2.sh
#!/bin/bash
source /etc/profile
echo "start...";
echo $PWD
source /home/test_shell/test1.sh >> /home/test_shell/log &
echo "end...";
crontab -e
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

*/1 * * * * ./home/test_shell/test2.sh  # 定时指令
启动
systemctl restart crond.service   # 重启定时任务, 只有restart和reload两个命令
service cornd status  # 查看执行报错情况 
查看执行和监控
tail -f /var/log/cron    # 执行日志查看看
tail -f /var/spool/mail/root  # 执行结果日志
报错
tail -f /var/log/cron    # 执行日志查,发现
# 出错位置
1288 Oct 11 14:13:01 iz2zeglob57mds4wy3xxfbz CROND[22676]: (root) CMD (123 >> /home/test_shell/test2.sh)
1289 Oct 11 14:13:01 iz2zeglob57mds4wy3xxfbz CROND[22675]: (root) MAIL (mailed 32 bytes of output but got status 0x004b#01     2)
解决:

查看是否是路径问题

*/1 * * * * ./home/test_shell/test2.sh    # 需要放在sh脚本的绝对路径

查看crontab执行转态

service cornd status
# 报错
send-mail: fatal: parameter inet_interfaces: no local interface found for ::1

crontab执行会发送邮件,修改postfix的配置文件 (116行位置)

# The inet_interfaces parameter specifies the network interface
# addresses that this mail system receives mail on.  By default,
# the software claims all active interfaces on the machine. The
# parameter also controls delivery of mail to user@[ip.address].
#
# See also the proxy_interfaces parameter, for network addresses that
# are forwarded to us via a proxy or network address translator.
#
# Note: you need to stop/start Postfix when this parameter changes.
#
#inet_interfaces = all
#inet_interfaces = $myhostname
#inet_interfaces = $myhostname, localhost
inet_interfaces = localhost
# Enable IPv4, and IPv6 if supported
inet_protocols = all

改为:

inet_interfaces = all
inet_protocols = all

重启:

service postfix start 或者
systemctl start postfix.service
最后

在重启crontab,查看 /var/spool/mail/root中的结果

From root@iz2zeglob57mds4wy3xxfbz.localdomain  Thu Oct 11 16:30:01 2018
Return-Path: <root@iz2zeglob57mds4wy3xxfbz.localdomain>
X-Original-To: root
Delivered-To: root@iz2zeglob57mds4wy3xxfbz.localdomain
Received: by iz2zeglob57mds4wy3xxfbz.localdomain (Postfix, from userid 0)
	id 43F7540FD4; Thu, 11 Oct 2018 16:30:01 +0800 (CST)
From: "(Cron Daemon)" <root@iz2zeglob57mds4wy3xxfbz.localdomain>
To: root@iz2zeglob57mds4wy3xxfbz.localdomain
Subject: Cron <root@iz2zeglob57mds4wy3xxfbz> ./home/test_shell/test2.sh
Content-Type: text/plain; charset=UTF-8
Auto-Submitted: auto-generated
Precedence: bulk
X-Cron-Env: <XDG_SESSION_ID=1090>
X-Cron-Env: <XDG_RUNTIME_DIR=/run/user/0>
X-Cron-Env: <LANG=en_US.UTF-8>
X-Cron-Env: <SHELL=/bin/bash>
X-Cron-Env: <PATH=/sbin:/bin:/usr/sbin:/usr/bin>
X-Cron-Env: <MAILTO=root>
X-Cron-Env: <HOME=/>
X-Cron-Env: <LOGNAME=root>
X-Cron-Env: <USER=root>
Message-Id: <20181011083001.43F7540FD4@iz2zeglob57mds4wy3xxfbz.localdomain>
Date: Thu, 11 Oct 2018 16:30:01 +0800 (CST)

start...
/
end...

这样就应该可以了!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值