erikbstack
erikbstack

Reputation: 13254

How to fork from a running process?

I often do this:

$ gitk &

To start a program (like gitk in this case, but it doesn`t matter which) and get back to the shell prompt to continue working in the same shell. How would you go about it, after you already wrote

$ gitk

without the &? Is there a key combination like ^z, that puts the process in the background but without interrupting it?

Upvotes: 0

Views: 2321

Answers (2)

cdarke
cdarke

Reputation: 44354

Every time you call an external program, like ls, or gitk, or whatever, you execute a fork. Running a program with & in Bash does a lot more than a fork. It creates a new process group which runs in the background.

Ctrl+z usually has the effect (using a signal) of placing the job in a stopped state in the background, restart with bg. I say "usually" because it can be changed, using stty. To list your terminal settings (and to see other magic Ctrl keys), try stty -a.

A characteristic of background jobs is that they don't normally have access to the keyboard, but do have access to the screen. That could be mixed with other output, so you can use stty tostop to prevent it. The job has to be brought into foreground to use the screen, or use stty -tostop. Jobs are brought into the foreground using fg.

Note that you can have many background jobs, see them by typing jobs. Some commands, including fg, bg, and kill, take an optional job number (prefixed %).

Upvotes: 1

R Kaja Mohideen
R Kaja Mohideen

Reputation: 917

If you have started the process without &, it'll run in the foreground. Press Ctrl+Z which will stop the process, then enter the command bg to resume the process execution in background.

Upvotes: 5

Related Questions