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 noticed that sudo continues to run after executing any of the following lines. Is there any way to detach the daemon completely so sudo does not continue running?

sudo -u user daemon &
sudo -u user -b daemon
share|improve this question
    
Do you mean you want to kill it? –  quanta Oct 12 '11 at 8:49
    
@quanta Why is sudo still running when it has aleady executed the program ? Shouldn't sudo terminate by itself without needing to kill it ? –  Jane Watson Oct 12 '11 at 8:52
1  
Because you put a & or -b (background) option when running it. –  quanta Oct 12 '11 at 8:53
    
@quanta If i omit the "&" or "-b", then it will not detach from the terminal. –  Jane Watson Oct 12 '11 at 8:56
5  
sudo is the program you have called, and sudo is calling "program". you have put sudo into the background, so sudo is still running. I think you're believing "program" is using sudo, when in reality its the other way around ? –  Sirex Oct 12 '11 at 9:00
show 5 more comments

migrated from serverfault.com Oct 12 '11 at 10:35

This question came from our site for professional system and network administrators.

4 Answers

up vote 4 down vote accepted

You can background it in one shot like this:

sudo -u user sh -c "daemon & disown %1"

Looking with ps will show that daemon is running as user. There will be no sudo associated with it.

share|improve this answer
add comment

sudo does not remain running after it executes the program you ask it to; in fact, it uses the exec() system call to replace itself with the program you specify so that program has the same PID that sudo did. You can verify this by running the following commands:

sudo -u user -b sleep 60
ps aux | grep sudo
ps aux | grep sleep

You will see that sleep is still running, but sudo is not.

If you ask the shell to background sudo with the & operator, then sudo will block and wait there in the background indefinitely if it must prompt you for your password. If it does not prompt you for your password, then the program you specify is run and sudo is no longer running. Because of the potential to block, the -b switch to sudo is the preferred method, so it can prompt you for your password if needed, and then it will fork into the background to run the requested program.

share|improve this answer
add comment

If you have the daemonize program, you can use sudo daemonize PATH_TO_DAEMON.

share|improve this answer
add comment

Sorry, this isn't really an answer, more of an additional comment.

Despite psusi's response, it appears that some versions of sudo will always fork() (and wait for the child process if -b is not specified).

The problem with using -b is that then $! does not contain the PID of the daemon; the problem with using daemonize is that it's not shipped with all distributions; the problem with using sudo sh -c 'run_my_daemon arg1 arg2 arg3... & save_pid $!' is that passing args in to run_my_daemon is quite contorted; getting away from such contortions is a major reason to use sudo in the first place.

Worrying about waiting for a password only applies if you're not root to begin with; in particular consider the case of using sudo -u $run_as_uid prog "${args[@]}" inside a script which is already running as root.

share|improve this answer
add comment

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.