0

A process put to sleep by issuing ^Z can be brought back to the foreground by listing the running jobs $ jobs and giving the job id to fg %id. But when I tried to list the jobs in a separate screen of the same terminal there was no results. How to achieve this ?

What I want is to start the same process without killing it in a separate screen of the same terminal.

2 Answers2

2

You can't. The handling of shell jobs (foreground and background processes) is done by each individual shell instance. You can't migrate a job from one interactive shell to another, not even within something like screen.

The terminal doesn't really have anything to do with this. It just runs the shell.

What you may do is to migrate a whole shell session with a terminal multiplexer, such as tmux or screen. A shell session within one of these may be moved to another terminal through detaching the screen or tmux session in one terminal and reattaching to it in another on the same machine.

With tmux this is done by first starting tmux, then starting your program inside of the tmux session. When the program is running, you may press prefixd (prefix is Ctrl+B by default) to detach the tmux session. In another terminal, you then give the command tmux attach to get the tmux session back. Detaching can also be done through the command tmux detach.

See the manuals for screen and tmux on your system.

Kusalananda
  • 333,661
1

The terminal is a viewer onto a process such as the shell. The terminal itself doesn't run any commands - that's the shell's purpose - but without the terminal you cannot interact with the shell or any program it runs.

Normally, when you close a terminal window it sends a signal to the shell to tell it to close down, which in turn kills any process you may have running there.

You can run a process under screen. What this does is create a "virtual terminal" for a shell to run in. Your "real" terminal window can attach to that screen session and control the virtual terminal that screen has provided. The net effect is that it feels perfectly normal BUT you can also detach from the virtual screen and let the shell continue running.

screen                     # Create a leader and a virtual terminal/screen running a shell
screen -ls                 # List sessions (attached/detached)
screen -r                  # Reattach to the single detached session
screen -r {identifier}     # Reattach to the named detached session
screen -md {command...}    # Make a new detached session running {command...}

Finally, Ctrl Ad detaches from the current session and Ctrl A? gets you the list of interactive commands.

Chris Davies
  • 116,213
  • 16
  • 160
  • 287