I have a cronjob installed (Ubuntu 16) that runs with the root user. So in
sudo crontab -e
I have the following entry:
10 5 * * mon /home/userA/raidchecker.sh
And the script raidchecker.sh
does this:
#!/bin/sh
mail=`which mail`
rec="[email protected] [email protected]"
now=$( date +%Y%m%d-%H%M-%S )
mdstat=$( cat /proc/mdstat )
details=$( /sbin/mdadm --detail /dev/md2 )
diskusg=$( df -kh )
disk1=$( smartctl -A /dev/sda | grep Realloc | grep Sector )
disk2=$( smartctl -A /dev/sdb | grep Realloc | grep Sector )
disk1f=$( smartctl -A /dev/sda )
disk2f=$( smartctl -A /dev/sdb )
buddyinfo=$( cat /proc/buddyinfo | grep Normal )
echo "Check from $now\n\n=================\nMemory Fragmentation:\n$buddyinfo\n\nAll partitions:\n\n$mdstat\n\n=================\nData Partition:\n\n$details\n\n=================\nDisk Usage:\n$diskusg\n\n=================\nSMARTCTL:\n\nData Disk /dev/sda and sdb:\n$disk1\n$disk2\n\nFull Reports:\nDisk1:\n$disk1f\n\nDisk2:\n$disk2f" | $mail -s "Check from $now" $rec
When I execute the script manually from the shell ./raidchecker.sh
the email contains all infos as expected.
When the cronjob executes the script, the parameters disk1, disk2, disk1f and disk2f are empty (do not contain any data).
Why?
The solution was to use
/usr/sbin/smartctl
instead ofsmartctl
in the script, only this way the cronjob could execute this command.Using a crontab, most of the usual environment set for a regular user login is absent or reduced (unless you set it), when the script is run automatically by
cron
.To be sure, it's better to either provide a relevant
PATH
, or give the absolute path to all commands.Always test a crontab (setting a time a couple minutes after the current time).
2>&1
to the command in the crontab> /dev/null 2>&1