Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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.

share|improve this question

3 Answers 3

up vote 8 down vote accepted

It's impossible in the way you want.

Let's review some basic concepts:

  • A process group is a collection of related processes which can all be signalled at once.
  • A session is a collection of process groups, which are either attached to a single terminal device (known as the controlling terminal) or not attached to any terminal.

If you closed the terminal, all the processes in the session are dead except those (daemons) repareted 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.

share|improve this answer
5  
Technically this is the correct answer, but I believe visitors who are having this problem will find "their solution" in the other answers mentioning screen/byobu/tmux. –  Programster Jan 18 at 11:46
    
You are right.., but from my point of view, the basic concepts are more important to understand system working better and understand what you want and doing wrong. Thank you both! –  Timur Fayzrakhmanov Jan 18 at 11:51

Have you tried: byobu or tmux which are terminal multiplexers. Not exactly what you are looking for, but it has similar behavior.

byobu-screen

Then run your command, to detach:

byobu-screen -d

To resume:

byobu-screen -r

See https://help.ubuntu.com/community/Screen

share|improve this answer
    
Those ones also, +1. –  falconer Jan 18 at 11:35
    
Thank you too! +1 –  Timur Fayzrakhmanov Jan 18 at 11:47
  1. Install screen:

    sudo apt-get install screen
    
  2. Start screen:

    screen -S session_name
    
  3. Execute your commands what you need.

  4. Detach screen from the terminal (your commands will be still running):

    Press CTRL+a+d

  5. Close the terminal

  6. Open another terminal and reattach the last screen session:

    screen -r
    

For more information and extra options for screen look in man screen.

share|improve this answer
2  
Screen or byobu (especially if you are running ubuntu). Byobu is based on screen if I'm correct, but I find that it has extra functionality that I find useful and easier to use, but that's a matter of opinion. –  Programster Jan 18 at 11:40
    
Thank you good suggestion! Maybe It will be need in the feature!) From Russia with love <3 –  Timur Fayzrakhmanov Jan 18 at 11:46
    
@Programster, yeah, Byobu is ex. screen. It has both byobu-screen and byobu-tmux. –  Sneetsher Jan 18 at 11:51
1  
I recommend tmux as an alternative to screen. –  gerrit Jan 18 at 15:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.