Cronjob从邮件正文中丢失内容,手动正常

时间:2013-06-06 19:14:37

标签: bash cron health-monitoring mutt mailx

编辑:通过Barmar的答案找到解决方案。添加了完整的smartctl命令路径,它现在通过crontab工作。

我有以下脚本:

#!/bin/bash
#set -x
EMAIL="admin@domain.co.uk"
FILE="/root/scripts/hddreport.txt"
HOST=`hostname`
HDD01="/dev/sda"
P=`ping -c 1 $HOST | sed '1 ! d' | awk '{print $3}'`

cd /root/scripts/
echo -en "HDD health check on the server hosting" $HOST $P > $FILE
echo -e "\n" >> $FILE
smartctl -H $HDD01 >> $FILE
# The above commands do correctly write the content to $FILE (proved by removing the rm command at the bottom and doing cat on the file after)

smartctl -H $HDD01

echo "\nEmailed you the health of the Hard Drive $HDD01\n"
mailx -s "HDD health check complete on `date`" $EMAIL < $FILE
rm $FILE

通过执行bash /root/scripts/diskhealth.sh运行正常,因为它在我的邮箱中显示:

HDD health check on the server hosting domain.co.uk (0.0.0.0)

smartctl 5.40 2010-07-12 r3124 [x86_64-unknown-linux-gnu] (local build)
Copyright (C) 2002-10 by Bruce Allen, http://smartmontools.sourceforge.net

SMART Health Status: OK

但是当我让它使用以下任何语法通过crontab运行时:

X 20 * * * /bin/bash /root/scripts/diskhealth.sh
X 20 * * * /bin/sh /root/scripts/diskhealth.sh
X 20 * * * /root/scripts/diskhealth.sh
除了smartctl磁盘之外,它会放置所有内容:

HDD health check on the server hosting domain.co.uk (0.0.0.0)

如果我添加额外的回声线,这是它显示的内容:

This is a test
HDD health check on the server hosting domain.co.uk (0.0.0.0)

下面的“这是一个测试”的修订脚本:

#!/bin/bash
#set -x
EMAIL="admin@domain.co.uk"
FILE="/root/scripts/hddreport.txt"
HOST=`hostname`
HDD01="/dev/sda"
P=`ping -c 1 $HOST | sed '1 ! d' | awk '{print $3}'`

cd /root/scripts/
echo "This is a test" > $FILE
echo -en "HDD health check on the server hosting" $HOST $P >> $FILE
echo -e "\n" >> $FILE
smartctl -H $HDD01 >> $FILE

smartctl -H $HDD01

echo "\nEmailed you the health of the Hard Drive $HDD01\n"
mailx -s "HDD health check complete on `date`" $EMAIL < $FILE
rm $FILE

这是来自cron的/ var / log / syslog输出:

Jun  6 20:25:01 hostname /USR/SBIN/CRON[1018112]: (root) CMD (bash /root/scripts/diskhealth.sh)
Jun  6 20:25:01 hostname postfix/pickup[1016576]: 5740356613F: uid=0 from=<root>
Jun  6 20:25:01 hostname postfix/cleanup[1018125]: 5740356613F: message-id=<20130606192501.5740356613F@hostname>
Jun  6 20:25:01 hostname postfix/qmgr[292015]: 5740356613F: from=<root@hostname>, size=465, nrcpt=1 (queue active)
Jun  6 20:25:01 hostname postfix/pickup[1016576]: 631F156613E: uid=0 from=<root>
Jun  6 20:25:01 hostname postfix/cleanup[1018125]: 631F156613E: message-id=<20130606192501.631F156613E@hostname>
Jun  6 20:25:01 hostname postfix/qmgr[292015]: 631F156613E: from=<root@hostname>, size=759, nrcpt=1 (queue active)
Jun  6 20:25:01 hostname pvemailforward[1018132]: forward mail to <root@localhost.localdomain>
Jun  6 20:25:01 hostname postfix/pickup[1016576]: B597B566148: uid=65534 from=<nobody>
Jun  6 20:25:01 hostname postfix/cleanup[1018125]: B597B566148: message-id=<20130606192501.631F156613E@hostname>
Jun  6 20:25:01 hostname postfix/local[1018131]: 631F156613E: to=<root@hostname>, orig_to=<root>, relay=local, delay=0.39, delays=0.16/0/0/0.23, dsn=2.0.0, status=sent (delivered to command: /usr/bin/pvemailforward)
Jun  6 20:25:01 hostname postfix/qmgr[292015]: 631F156613E: removed
Jun  6 20:25:01 hostname postfix/qmgr[292015]: B597B566148: from=<nobody@hostname>, size=963, nrcpt=1 (queue active)
Jun  6 20:25:01 hostname postfix/smtp[1018135]: B597B566148: to=<root@localhost.localdomain>, relay=none, delay=0.16, delays=0.12/0/0.04/0, dsn=5.4.4, status=bounced (Host or domain name not found. Name service error for name=localhost.localdomain type=A: Host not found)
Jun  6 20:25:01 hostname postfix/qmgr[292015]: B597B566148: removed
Jun  6 20:25:01 hostname postfix/cleanup[1018125]: D6570566147: message-id=<20130606192501.D6570566147@hostname>
Jun  6 20:25:01 hostname postfix/smtp[1018130]: 5740356613F: to=<admin@domain.co.uk>, relay=ASPMX.L.GOOGLE.COM[173.194.67.27]:25, delay=0.68, delays=0.12/0/0.19/0.36, dsn=2.0.0, status=sent (250 2.0.0 OK 1370546701 iy4si8635735wic.1 - gsmtp)
Jun  6 20:25:01 ds9453 postfix/qmgr[292015]: 5740356613F: removed

收到电子邮件,只是错过了smartctl输出。

1 个答案:

答案 0 :(得分:2)

Cron的工作不会运行你的.profile。因此,如果smartctl位于您在个人资料中添加到$PATH的目录中,那么当您通过cron投放时,将无法找到它。尝试使用命令的完整路径名。