0

编辑:通过 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 输出。

4

1 回答 1

2

Cron 作业不运行您的.profile. 因此,如果smartctl它位于您在配置文件中添加到的目录$PATH中,则在您运行时不会找到它cron。尝试使用命令的完整路径名。

于 2013-06-06T19:41:48.417 回答