3

It seems that somewhere between sudo 1.7.2p2 and 1.7.4p5 the behaviour for waiting for executing processes has changed. It looks like in the older versions sudo would start the new process, and then quit. In the newer versions it starts the new process, and then waits for it. There is a bit of a discussion about it here: http://www.sudo.ws/pipermail/sudo-users/2010-August/004461.html which mentions that it is to stop it from breaking PAM session support.

This change is breaking one of my scripts which uses sudo to execute commands in the background, as with the older version sudo the command I want to execute would be backgrounded, and with the new version it is sudo itself that is backgrounded.

For example, the process returned by $! in this case is for sleep

user@localhost$ sudo -V
Sudo version 1.7.2p2
user@localhost$ sudo -u poweruser sleep 60 &
[1] 17491
user@localhost$ ps -fp $!
UID        PID  PPID  C STIME TTY          TIME CMD
poweruser 17491 17392  0 16:43 pts/0    00:00:00 sleep 60

Whereas in this case it is for sudo

user@localhost$ sudo -V
Sudo version 1.7.4p5
user@localhost$ sudo -u poweruser sleep 60 &
[1] 792
user@localhost$ ps -fp $!
UID        PID  PPID  C STIME TTY          TIME CMD
root       792 29257  0 16:42 pts/3    00:00:00 sudo -u poweruser sleep 60

Is it possible to get the process ID for a child process executed by sudo version 1.7.4p5? The $! variable returns the PID for sudo, and running sudo with the -b option doesn' seem to make the child PID available. Is it possible (without recompiling sudo) to revert the behaviour of sudo to stop it from waiting for child processes?

Thanks

1 Answer 1

1

This is certainly a hack, and it doesn't set $!, but you can echo the pid of the command:

$ sudo sh -c 'echo $$; exec sleep 60'

I'm guessing that your explanation of the old behavior is not quite right and that sudo simply exec'd the command rather than forking and exiting. Echoing the pid and then exec'ing the desired command might work for you, but you may need creative redirections. For example:

#!/bin/sh

exec 3>&1
pid=$( sudo sh -c 'echo $$; exec sh -c "{ sleep 1;
    echo my pid is $$; }" >&3 &')
echo Child pid is $pid

In the above, you lose the pid of sudo...but it wouldn't be too hard to find it.

3
  • Thanks William. I actually just needed to store the pid in a file, so my redirection wasn't quite so creative. I ended up with something like this: sudo -u poweruser sh -c "echo \$\$ > ${pid_file}; exec sleep 60" &
    – nickelaway
    Feb 20, 2012 at 15:23
  • Hi, tried your code and I keep getting sh: 1: 3: Bad file descriptor errors. What am I doing wrong? Oct 27, 2013 at 16:04
  • @Frederick exec 1>&3 would likely give the error you describe, as would failing to supply the exec. Oct 28, 2013 at 12:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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