[ALUG] sudo &

steve-ALUG at hst.me.uk steve-ALUG at hst.me.uk
Tue Jun 13 09:41:36 BST 2017


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






More information about the main mailing list