I've got a Kubuntu 11.04 machine I'm using as a storage server to back up some Windows PCs.
Its BIOS wakes it up at midnight, it acts as a samba server for the Windows boxes to do their backups. I want it to shut down and power off whenever the backups are completed.
So, I'm using crontab to run a script that checks that the backup directory hasn't changed in 60 minutes, sends me an email, and then calls "sudo /sbin/shutdown " to take the system down and turn it off.
I added the following line to /etc/sudoers:
bear ALL=(ALL)NOPASSWD: /sbin/shutdown
to allow me (username bear) to run sudo /sbin/shutdown without being prompted for a password.
When I run it from an interactive shell, it works perfectly.
When run from crontab, it fails and says this on stderr:
sudo: no tty present and no askpass program specified
The mail it sends me confirms that cron it is running as user "bear". The sudoers file says (I think) that bear can run /sbin/shutdown with no password. So why does it fail?
I have no clue. Help?
Test version of the script:
---------------------------
#! /bin/bash
CHANGED=`find /data -mmin -60 -ls | wc -l`
echo "CHANGED = $CHANGED"
if test $CHANGED -gt 0
then
echo "keep running"
else
echo "Shutdown at `date`" > /tmp/mailbody
echo "whoami:`whoami`" >>/tmp/mailbody
echo "id:`id`" >>/tmp/mailbody
echo "pwd:`pwd`" >>/tmp/mailbody
/usr/bin/mail -s "Backup Shutdown" bear@di.org </tmp/mailbody
echo "shutdown"
sudo /sbin/shutdown -k +5 "Going down"
fi
The email that it sends me shows it is being executed as use "bear":
---------------------------------------
Shutdown at Sat May 14 11:55:01 EDT 2011
whoami:bear
id:uid=1000(bear) gid=1000(bear) groups=1000(bear),4(adm),20(dialout),24(cdrom),46( plugdev),112(lpadmin),119(admin),120(sambashare)
pwd:/home/bear
The crontab entry:
------------------------------
*/5 * * * * /home/bear/fakeshutoff.sh >~bear/crontab.out 2>~bear/crontab.err
The /etc/sudoers entry:
-------------------------------------
bear ALL=(ALL)NOPASSWD: /sbin/shutdown
Its BIOS wakes it up at midnight, it acts as a samba server for the Windows boxes to do their backups. I want it to shut down and power off whenever the backups are completed.
So, I'm using crontab to run a script that checks that the backup directory hasn't changed in 60 minutes, sends me an email, and then calls "sudo /sbin/shutdown " to take the system down and turn it off.
I added the following line to /etc/sudoers:
bear ALL=(ALL)NOPASSWD: /sbin/shutdown
to allow me (username bear) to run sudo /sbin/shutdown without being prompted for a password.
When I run it from an interactive shell, it works perfectly.
When run from crontab, it fails and says this on stderr:
sudo: no tty present and no askpass program specified
The mail it sends me confirms that cron it is running as user "bear". The sudoers file says (I think) that bear can run /sbin/shutdown with no password. So why does it fail?
I have no clue. Help?
Test version of the script:
---------------------------
#! /bin/bash
CHANGED=`find /data -mmin -60 -ls | wc -l`
echo "CHANGED = $CHANGED"
if test $CHANGED -gt 0
then
echo "keep running"
else
echo "Shutdown at `date`" > /tmp/mailbody
echo "whoami:`whoami`" >>/tmp/mailbody
echo "id:`id`" >>/tmp/mailbody
echo "pwd:`pwd`" >>/tmp/mailbody
/usr/bin/mail -s "Backup Shutdown" bear@di.org </tmp/mailbody
echo "shutdown"
sudo /sbin/shutdown -k +5 "Going down"
fi
The email that it sends me shows it is being executed as use "bear":
---------------------------------------
Shutdown at Sat May 14 11:55:01 EDT 2011
whoami:bear
id:uid=1000(bear) gid=1000(bear) groups=1000(bear),4(adm),20(dialout),24(cdrom),46( plugdev),112(lpadmin),119(admin),120(sambashare)
pwd:/home/bear
The crontab entry:
------------------------------
*/5 * * * * /home/bear/fakeshutoff.sh >~bear/crontab.out 2>~bear/crontab.err
The /etc/sudoers entry:
-------------------------------------
bear ALL=(ALL)NOPASSWD: /sbin/shutdown
Comment