Question

Edit: Solution found via Barmar's answer. Added full smartctl command path and it works via crontab now.

I have the below script:

#!/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

which runs fine by doing bash /root/scripts/diskhealth.sh as it shows this in my mailbox:

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

But when I let it run via crontab using any of the following syntax:

X 20 * * * /bin/bash /root/scripts/diskhealth.sh
X 20 * * * /bin/sh /root/scripts/diskhealth.sh
X 20 * * * /root/scripts/diskhealth.sh

it puts everything but the smartctl disk check:

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

Here's what it shows if I add extra echo lines:

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

Amended script for "This is a test" below:

#!/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

Here is the /var/log/syslog output from cron:

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

The email is received, just missing the smartctl output.

Was it helpful?

Solution

Cron jobs don't run your .profile. So if smartctl is in a directory you add to $PATH in your profile, it won't be found when you run via cron. Try using the full pathname to the command.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top