4

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?

6
  • 1
    Did you run the script manually with the same user as in the crontab? Aug 31, 2020 at 8:45
  • i don't know. I run the script as root. the crontab has been setup by using the same user.
    – bzero
    Aug 31, 2020 at 8:53
  • 1
    sorry my bad ... Try appending 2>&1 to the cronjob stackoverflow.com/questions/4883069/debugging-crontab-jobs Aug 31, 2020 at 8:59
  • @HenrikPingel: did that, no more info in the email. In the syslog I see this : CRON[9476]: (root) CMD (/home/userA/raidchecker.sh 2>&1)
    – bzero
    Aug 31, 2020 at 9:03
  • I think i found it out. cronjob can not find the command smartctl
    – bzero
    Aug 31, 2020 at 9:07

2 Answers 2

2

The solution was to use /usr/sbin/smartctl instead of smartctl in the script, only this way the cronjob could execute this command.

1

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).

  • If you want to receive (by email) the standard and also error output, only add 2>&1 to the command in the crontab
  • If you do not want to receive any of the output of the crontab command (by email), add > /dev/null 2>&1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .