Linux: Move a process from fore- to background

Hi,

bash offers the ability to move a running foreground process into background.

An example 🙂 Listen with netcat at tcp port 80, this process to not exit and stays in foreground.

root@debdev:~# nc -l -t -p 80 -s 10.254.1.1

Now I want to do some other things in this console.

Press Ctrl -Z move stops the process and moves it into background

^Z
[2]+ Stopped nc -l -t -p 80 -s 10.254.1.1

To activate the process in background

root@debdev:~# bg
[2]+ nc -l -t -p 80 -s 10.254.1.1 &

This is possible with multiple processes. To list all background process enter

root@debdev:~# jobs
[1]+ Running nc -l -t -p 80 -s 10.254.1.1 &

And to get the job back to foreground hit

root@debdev:~# fg 1

If the process should kept running after you exit the shell. disown prevents the sending of the signal SIGHUP to jobs like starting a process with nohup.

root@debdev:~# nc -l -t -p 80 -s 10.254.1.1
Now I want to do some other things in this console.
Press Ctrl -Z move stops the process and moves it into background

^Z
[2]+ Stopped nc -l -t -p 80 -s 10.254.1.1
root@debdev:~# bg
[2]+ nc -l -t -p 80 -s 10.254.1.1 &
root@debdev:~# disown -h %2

Michael

Leave a Reply Cancel reply