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).