2tails
2tails

Reputation: 31

Crontab not really running as root when it should be?

I have Ubuntu and am running under the user "alex". I've got the following bash script running as root with crontab however it sends me an email and it looks like it hasn't run correctly as the result is not present:

/dev/sda - Reallocated_Sector_Ct is

However if I run the crontab job manually from webmin, it works without issues. But when it's scheduled to run, that's when it fails. Maybe it's not really running as root?

Here's my code:

#!/bin/bash
SMARTCHECK=`smartctl -a /dev/sda | grep "Reallocated_Sector_Ct" | awk 'NR==1 {print $10}'`
echo "/dev/sda - Reallocated_Sector_Ct is $SMARTCHECK"
if [ "$SMARTCHECK" != "0" ]; then
    mail -s "Failing: /dev/sda" alex <<< "/dev/sda - Reallocated_Sector_Ct is $SMARTCHECK"
fi

Thanks!

Upvotes: 0

Views: 633

Answers (2)

BMW
BMW

Reputation: 45263

more possible issues in cronjob scripting.

  • change all commands in script with full path, especially the command smartctl
  • check your system has /bin/bash, not /usr/bin/bash, or set the cronjob as:

    0 * * * * ~/.profile;/usr/bin/bash YOUR_SCRIPT

  • check the mail in alex account, if there are any error messages can be found.

Upvotes: 0

Igor Chubin
Igor Chubin

Reputation: 64573

There are two possibilities to run scripts using cron:

  1. You use system crontab /etc/crontab. In this case they run as root.
  2. You use users' crontabs, which are accessible using crontab -e. In this case they run with privileges of the user, that has added the command to his crontab.

Upvotes: 1

Related Questions