I have a script run from a non-privileged 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.
The user in question has (root) NOPASSWD: ALL access granted in /etc/sudoers
and my brain started screaming too loud to keep reading.sudo
rights /only/ for the commands you need in the script and disabling their login ability completely.