0

I am looking for a way of putting a job which requires sudo to background, with as little interaction as possible except maybe typing my password.

sudo requires password from terminal, while background job will give up terminal:

$ sudo sleep 10 &
[1] 32749
$ fg 1
sudo sleep 10
[sudo] password for t: 

My current solution is like above to type in my password, and then bg 1 the job again. It requires me to interactively perform three commands

sudo only work with executables, not shell specific commands:

$ sudo ( sleep 10 & )
bash: syntax error near unexpected token `sleep'
$ sudo { sleep 10 & }
bash: syntax error near unexpected token `}'

The following way will detach the back ground job from the shell, undesirably:

$ sudo bash -c "sleep 10 & "
[sudo] password for t: 
$ jobs
$ 

I can also focus on sudo. For example, make the command NOPASSWD in sudoers, or take advantage of password cache by running a dummy sudo command ahead of my actual background sudo command. I am not sure if they are elegant ways. Let me know if any of them is.

1

I think your last paragraph is the way to go about this. If disabling the password requirement is appropriate, then by all means configure the command with NOPASSWD. Otherwise, you can set things up ahead of time, if your sudo allows caching credentials (which it does by default):

sudo -v
sudo sleep 10 &
2

I use sudo -v for this, as your "dummy sudo command ahead of an actual background sudo command"; the sudo man page gives this:

By running sudo with the -v option, a user can update the cached credentials without running a command.

This doesn't require that the user have any other sudo-granted commands that would function as "dummy" sudo commands.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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