Is there an easy was to launch a process with sudo and send it into the background?
$ sudo myapp & .. will go straight into the background then stop, waiting for a password to be entered. So I have to bring it back to the foreground (fg), enter the password, stop it (ctrl-z) then send it back into the background (bg).
Of-course what normally happens is that I don't realise it's stopped at all, only to come back several hours after a long process should have been running to find it hasn't yet started....
I'm sure there's a better way?
Mark
On Thu, Jun 08, 2017 at 11:32:10AM +0100, Mark Rogers wrote:
Is there an easy was to launch a process with sudo and send it into the background?
$ sudo myapp & .. will go straight into the background then stop, waiting for a password to be entered. So I have to bring it back to the foreground (fg), enter the password, stop it (ctrl-z) then send it back into the background (bg).
Of-course what normally happens is that I don't realise it's stopped at all, only to come back several hours after a long process should have been running to find it hasn't yet started....
I'm sure there's a better way?
If you haven't got lots of jobs under job control you could do:-
sudo myapp && bg
You could use the sudo NOPASSWD directive (using visudo), e.g. something like:
myuser ALL = (ALL) NOPASSWD: /sbin/ifconfig
then "sudo ifconfig" should work without a password. Note that this must come after any other visudo definitions for that user.
Simon
On 08/06/17 11:32, Mark Rogers wrote:
Is there an easy was to launch a process with sudo and send it into the background?
$ sudo myapp & .. will go straight into the background then stop, waiting for a password to be entered. So I have to bring it back to the foreground (fg), enter the password, stop it (ctrl-z) then send it back into the background (bg).
Of-course what normally happens is that I don't realise it's stopped at all, only to come back several hours after a long process should have been running to find it hasn't yet started....
I'm sure there's a better way?
Mark
On Thu, 2017-06-08 at 11:32 +0100, Mark Rogers wrote:
Is there an easy was to launch a process with sudo and send it into the background?
$ sudo myapp & .. will go straight into the background then stop, waiting for a password to be entered. So I have to bring it back to the foreground (fg), enter the password, stop it (ctrl-z) then send it back into the background (bg).
Of-course what normally happens is that I don't realise it's stopped at all, only to come back several hours after a long process should have been running to find it hasn't yet started....
I'm sure there's a better way?
This is what /etc/sudoers is for. You can add an entry that allows a specific user to run a specific command as root without a password.
Be careful; If you're using this as a means to restrict access to root, you have to make sure that there's no way a user can get a root shell, by using a shell escape from the application, or replacing the binary with a copy of a shell, for example.
On 08 Jun 11:32, Mark Rogers wrote:
Is there an easy was to launch a process with sudo and send it into the background?
$ sudo myapp & .. will go straight into the background then stop, waiting for a password to be entered. So I have to bring it back to the foreground (fg), enter the password, stop it (ctrl-z) then send it back into the background (bg).
Of-course what normally happens is that I don't realise it's stopped at all, only to come back several hours after a long process should have been running to find it hasn't yet started....
I'm sure there's a better way?
So, how about looking at the magical man page... or even sudo --help...
then you'll find that there's this magic option:
(man page) -b, --background Run the given command in the background. Note that it is not possible to use shell job control to manipulate background processes started by sudo. Most interactive commands will fail to work properly in background mode.
(--help)
-b, --background run command in the background
So, that means that sudo will stay in the foreground and ask for your password, as you'd expect, but then fork the process off in to the background.
Simples.
Thanks,
On 8 June 2017 at 12:40, Brett Parker iDunno@sommitrealweird.co.uk wrote:
So, how about looking at the magical man page... or even sudo --help...
Fair point (I started with Google and didn't find many options, but did find this one after my post). However it's not equivalent as it then doesn't allow the jobs to be managed in the same way once started, and that rules it out for me.
As to the other suggestions: Yes, editing sudoers will work for specific cases but I don't want to avoid the password security (just avoid it being hidden in the background).
sudo myapp && bg .. looks like a good one to play with, but surely bg won't run until myapp completed?.
To expand on the reason I asked: Today I ran out of disk space and started going through a tidy up. I found some large directories (unused virtual machines that I don't want to throw away but don't really need either) that I wanted to compress. So I used: vm=vm1 ; sudo tar -cjf $vm.tar.bz2 --remove-files $vm & .. which worked fine as I had already done some tasks with sudo by that point so no password was needed.
a while later I came back (after it had finished), edited the command to backup vm2, hit enter and got on with other tasks again. Of-course my sudo session had timed out, and whilst nothing looked any different it was sat there doing nothing and I didn't realise for a few hours, when "jobs" showed me that the task was stopped.
In this scenario, unless I enable password-less sudo, /etc/sudoers doesn't help.
I did find one useful suggestion after more Googling that created a "sudobg" alias[1] which looks like the best option so far.
[1] https://askubuntu.com/questions/224649/how-to-run-a-command-asking-password-... (last answer on page)
On 08/06/17 13:08, Mark Rogers wrote:
To expand on the reason I asked: Today I ran out of disk space and started going through a tidy up. I found some large directories (unused virtual machines that I don't want to throw away but don't really need either) that I wanted to compress. So I used: vm=vm1 ; sudo tar -cjf $vm.tar.bz2 --remove-files $vm & .. which worked fine as I had already done some tasks with sudo by that point so no password was needed.
a while later I came back (after it had finished), edited the command to backup vm2, hit enter and got on with other tasks again. Of-course my sudo session had timed out, and whilst nothing looked any different it was sat there doing nothing and I didn't realise for a few hours, when "jobs" showed me that the task was stopped.
In this scenario, unless I enable password-less sudo, /etc/sudoers doesn't help.
I did find one useful suggestion after more Googling that created a "sudobg" alias[1] which looks like the best option so far.
Sometimes it helps to explain why you want what you want, then people can give a more informed answer.
Use screen.
Type screen then enter. You'll get a page of intro, then you're in a terminal window. Start your commands. Don't worry about trying to background them....
Once it's started, press CTRL+A, then D. CTRL+A grabs screen's Attention, then you Detach from it. The screen-ed commands continue, even if you close the terminal window. To get back to it, use screen -r for re-attach from any terminal window.
Screen is an extremely useful command, especially over ssh or some sort of remote login, because if the connection is lost due to connection problems, the commands continue executing on the remote machine, and/or you can start something on a home machine via ssh from work, and then look at results on the home machine when you get home.
Commands run under screen will run at normal priority. If you want them to run with lower priority, use nice, or renice and/or ionice....
screen sudo nice -n 10 SomeCommand
or screen sudo SomeCommand ps -Af | grep "SomeCommand"
[look for the PID of SomeCommand] sudo renice -n 10 -p PID sudo ionice -c 3 -p PID
Obviously, look at man for screen, nice, renice and ionice for more info.
Alternatively, not using screen, type your commands into a command window. Once the commands have started, simply press CTRL+Z. This sends that command to the background, then use bg to restart it in the background. Info here... http://www.thegeekstuff.com/2010/05/unix-background-job
Alternatively, put all your commands into a shell script then sudo ./myshellscript.sh
Alternatively and possibly not recommended, use sudo -i to become root, then run all your commands in that window. It will only as for the password once.
Steve
On 13 June 2017 at 09:41, steve-ALUG@hst.me.uk wrote:
Sometimes it helps to explain why you want what you want, then people can give a more informed answer.
Use screen.
Type screen then enter. You'll get a page of intro, then you're in a terminal window. Start your commands. Don't worry about trying to background them....
Interesting approach, thanks. Screen is one of those tools I've never really used because every time I think about using it I need to go and dig out the documentation to remember how to use it, and it's quicker not to, so I never learn...
One thing I will say though is that with the way I do things now it's trivial to see if the task has finished (just hit enter and see if I'm told the process has completed, or use "jobs" to check). Using screen would mean re-attaching to check the detaching again. But it's certainly something I should be using more broadly so it's good to be reminded; although in this case everything I was doing was local so losing connection wasn't an issue, I do work via SSH enough that this should be second nature by now.
Alternatively, not using screen, type your commands into a command window. Once the commands have started, simply press CTRL+Z. This sends that command to the background, then use bg to restart it in the background.
That's the "obvious" answer but when you run lots of commands in the background its cumbersome, and when they're mostly short lived sudo doesn't time out, so just sticking & on the end is a lot easier (and habit forming...). Then one task takes a bit longer than expected (or you go for lunch), then you come back later and run an edited command from history and everything looks fine but of-course it's sat in the background stopped waiting for a password....
Alternatively, put all your commands into a shell script then sudo ./myshellscript.sh
This is ideal when you've worked out what you want to do. In my case it was "compress this in the background, then while its running go hunting for something else to compress once its done", so scripting wasn't ideal.
Alternatively and possibly not recommended, use sudo -i to become root, then run all your commands in that window. It will only as for the password once.
Indeed, and this would have been simplest, although it also means that anything else I do is run as root, which is non-ideal from the point of view of safety but also because I'll tend to leave files scattered around with wrong ownership.
Probably what I need is a way to tell my current session to have passwordless sudo without changing the default behaviour (or changing the timeout but only for this session).
On 13/06/17 10:42, Mark Rogers wrote:
On 13 June 2017 at 09:41, steve-ALUG@hst.me.uk wrote:
Alternatively and possibly not recommended, use sudo -i to become root, then run all your commands in that window. It will only as for the password once.
Indeed, and this would have been simplest, although it also means that anything else I do is run as root, which is non-ideal from the point of view of safety but also because I'll tend to leave files scattered around with wrong ownership.
Open a new window. Do the sudo -i stuff in it. Do the non sudo normal stuff in another window.
Simples! :-)
Steve