14

Consider the following nohup execution:

nohup script.sh > script.out &

Is there a way to log off the terminal, reconnect, bring the process back to foreground and interact with it using the keyboard?

7

If you wanted to start a script, have it run without the output bothering your terminal and then bring it up later to interact with it, you might want to take a look at a terminal multiplexer. Depending on your system I would recommend tmux or screen. You can find some info on how to use them in the links below:

tmux:

screen:

edit: added links for tmux primers

  • +1 Excellent tool I didn't know of. Many thanks. – Adam Matan Aug 17 '11 at 16:36
19

nohup does not disconnect a command from terminal, it makes your script ignore SIGHUP, and redirects stdout/stderr to a file nohup.out, so that the command can continue running in the background after you log out.

nohup does not automatically put the command it runs in the background. One must do that explicitly, by ending the command line with an &.

$ nohup ./script.sh &
[1] 3390
$ nohup: ignoring input and appending output to ‘nohup.out’

jobs can print currently running jobs and their status. If the command jobs cannot find it, then it is no longer a child process of that shell.

$ jobs 
[1]+  Running                 nohup ./script.sh &

One can be bring back a background job to foreground in bash using fg even if it is run with nohup. But that won't change the output redirection, which will still be going to the file nohup.out.

$ fg
nohup ./script.sh

If you close the shell/terminal or log off, your command is no longer a child of that shell. It belongs to init process. If you search in pstree you'll see it is now owned by process 1 (init). That cannot be brought back to the foreground because the foreground no longer exists.

  • If the command jobs cannot find it, then it is no longer a child process of that shell. This is wrong: try this sequence to see how: sleep 100 & disown ; echo Jobs:; jobs; echo Tree:; pstree -ap $$. After this command you'll still see sleep work with the same PID printed out when you spawned it, while jobs will print nothing. The only way a child process can stop being child is losing its parent (then the orphan becomes the child of init) or forking+exiting (but then the PID changes, so it's not the original child). – Ruslan Jan 13 '18 at 10:07
4

I'll second the use of a terminal multiplexer, but tmux, not screen. Screen is, for all intents and purposes, unmaintained. Its configuration is a dark art, likely first recorded in an appendix of the Necronomicon. Attempting to write your own config is as a glimpse of Cthulu. Seriously. Look around at people's .screenrc files. To be sure, there are ways to mediate screen's abomination of a config flle. Byobu does an admirable job as the veil between mortal users and unholy code. It comes with sensible colours, a menu interface, and perhaps most importantly, a status bar.

But as I said, I recommend tmux instead. It still requires some minor configuration, but it is well documented, and your config file won't look like gibberish. Also, you start out with colours and a status bar. Compare my screenrc and tmux.conf files:

0

If you are just interested in looking live hat the output this might suit you:

nohup script.sh 2>&1 script.out &
# later:
tail -f script.out
0

Any Linux command can be sent to the background with '&' appended at the last.

To view all the background jobs, you can issue jobs

jobs
[1]  + suspended  nohup sh zookeeper-server-start.sh ../config/zookeeper.properties
[2]    running    nohup sh kafka-server-start.sh ../config/server.properties
[3]  - running    nohup sudo mongod --dbpath=/var/lib/mongodb

Here I am running 3 jobs in background: zookeeper, kafka and mongo daemon. [1],[2] and [3] are respective job numbers for these tasks.

You can bring those task to foreground using fg %$taskNumber.

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.