Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a script run from a non-privaleged users' crontab that invokes some commands using sudo. Except it doesn't. The script runs fine but the sudo'ed commands silently fail.

  • The script runs perfectly from a shell as the user in question.

  • Sudo does not require a password. The user in question has (root) NOPASSWD: ALL access granted in /etc/sudoers.

  • Cron is running and executing the script. Adding a simple date > /tmp/log produces output at the right time.

  • It's not a permissions problem. Again the script does get executed, just not the sudo'ed commands.

  • It's not a path problem. Running env from inside the script being run shows the correct $PATH variable that includes the path to sudo. Running it using a full path doesn't help. The command being executed is being given the full path name.

  • Trying to capture the output of the sudo command including STDERR doesn't show anything useful. Adding sudo echo test 2>&1 > /tmp/log to the script produces a blank log.

  • The sudo binary itself executes fine and recognizes that it has permissions even when run from cron inside the script. Adding sudo -l > /tmp/log to the script produces the output:

    User ec2-user may run the following commands on this host:
    (root) NOPASSWD: ALL

Examining the exit code of the command using $? shows it is returning an error (exit code: 1), but no error seems to be produced. A command as simple as /usr/bin/sudo /bin/echo test returns the same error code.

What else could be going on?

This is a recently created virtual machine running the latest Amazon Linux AMI. The crontab belongs to the user ec2-user and the sudoers file is the distribution default.

share|improve this question
    
I was going to talk about a solution but then I read The user in question has (root) NOPASSWD: ALL access granted in /etc/sudoers and my brain started screaming too loud to keep reading. –  Shadur Sep 25 '12 at 12:05
    
@Shadur: Talk to the hand. That isn't my way of setting up a machine either, but these machines come this way out of the box. Even through the machine is yours, you don't get a root password, your key as the owner of the box goes into the ec2-user account which has (as noted) full sudo access. You don't get a password for ec2-user either unless you set one, it's a key only login. –  Caleb Sep 25 '12 at 12:12
    
Then the first thing I'd recommend you do is set up a separate user with restricted sudo rights /only/ for the commands you need in the script and disabling their login ability completely. –  Shadur Sep 25 '12 at 12:27
    
if you have root, and you want the cron job to run as root, then why put it in ec2-user's crontab? wouldn't root's crontab be more appropriate? or /etc/crontab? –  cas Sep 25 '12 at 12:33
    
@CraigSanders: I don't want the cron job run as root, in fact most of it should be run as a user. The question is not about running a job as root, but about a script having specially access to one function through sudo. –  Caleb Sep 25 '12 at 12:40

1 Answer 1

up vote 10 down vote accepted

Sudo has some special options in it's permissions file, one of which allows a restriction on it's usage to shells that are are running inside a TTY, which cron is not.

Some distros including the Amazon Linux AMI have this enabled by default. The /etc/sudoers file will look something like this:

# Disable "ssh hostname sudo <cmd>", because it will show the password in clear.
#         You have to run "ssh -t hostname sudo <cmd>".
#
Defaults    requiretty

#
# Refuse to run if unable to disable echo on the tty. This setting should also be
# changed in order to be able to use sudo without a tty. See requiretty above.
#
Defaults   !visiblepw

If you had captured output to STDERR at the level of the shell script rather than the sudo command itself, you would have seem a message something like this:

sorry, you must have a tty to run sudo

The solution is to allow sudo to execute in non TTY environments either by removing or commenting out these options:

#Defaults    requiretty
#Defaults   !visiblepw
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.