I'm trying to find out how to run a command in the background and then bring it to the foreground later. I'm fed up of tutorials and answers where people state that it's simple, just append &
to the end of the command to get it run in the background. It's working only within a single terminal window. I want to put the command into the background in one terminal and get it back to foreground after reopening the terminal
Example:
$ grunt &
$ jobs
$ [1]+ Running grunt &
Of course after closing terminal no one job is found.
Next example:
$ grunt & disown #the same behavior has: $ setsid grunt &
$ jobs
$ [nothing] #but ps shows that grunt is working
after close terminal, grunt doesn't work
What did I do wrong? Could anybody explain me how to run the command in the background and get it back to foreground.
Install
screen
:Start
screen
:Execute your commands what you need.
Detach
screen
from the terminal (your commands will be still running):Press CTRL+a+d
Close the terminal
Open another terminal and reattach the last
screen
session:For more information and extra options for
screen
look inman screen
.It's impossible in the way you want.
Let's review some basic concepts:
If you closed the terminal, all the processes in the session are dead except those (daemons) reparented to the init process. And there's no way to give them a controlling terminal again.
In a word, process reparenting is highly restricted in POSIX systems (daemonizing is an exception) and your requirements can't be satisfied.
Have you tried:
byobu
ortmux
which are terminal multiplexers. Not exactly what you are looking for, but it has similar behavior.Then run your command, to detach:
To resume:
See https://help.ubuntu.com/community/Screen