Announcement

Collapse
No announcement yet.

Sudo not working when called by crontab

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Sudo not working when called by crontab

    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






    #2
    Re: Sudo not working when called by crontab

    You need to add the option visiblepw to the sudoers file.

    Please Read Me

    Comment


      #3
      Re: Sudo not working when called by crontab

      Thanks! I appreciate the quick reply.

      Alas,I tried it, and then all I got was this from stderr:
      [sudo] password for bear:
      Sorry, try again.
      [sudo] password for bear:
      Sorry, try again.
      [sudo] password for bear:
      Sorry, try again.
      sudo: 3 incorrect password attempts

      It was still expecting a password.

      Well I looked a little farther and found the problem. My /etc/sudoers file contained another line, after the NOPASSWD: line that apparently rescinded the "no password" attribute;

      bear ALL = NOPASSWD: /sbin/shutdown
      . . .
      # Members of the admin group may gain root privileges
      %admin ALL=(ALL) ALL

      As you probably guessed by now, user "bear" is a member of group "admin". That later line was saying "Let all members of group 'admin' use sudo to execute all commands, but require them to enter a password" Aha! I reversed the order of the lines, putting the specific NOPASSWD: line farther down. Now everything works fine.

      Thanks again!

      Comment


        #4
        Re: Sudo not working when called by crontab

        Maybe putting the shutdown script in the system crontab where it can be run by root would be a better idea?

        Just my preference but I put system tasks in /etc/crontab
        we see things not as they are, but as we are.
        -- anais nin

        Comment


          #5
          Re: Sudo not working when called by crontab

          I must be missing something here but is there some reason you can't or don't want to adjust your power settings to turn off when there has been no activity for x amount of time?

          Comment


            #6
            Re: Sudo not working when called by crontab

            Originally posted by positmothy
            I must be missing something here but is there some reason you can't or don't want to adjust your power settings to turn off when there has been no activity for x amount of time?
            An automated process won't keep the machine running - your power settings are dependent on keyboard or mouse activity.
            we see things not as they are, but as we are.
            -- anais nin

            Comment

            Working...
            X