1. Intro

Here we will learn how to handle processes in linux.

Detaching processes from a controlling terminal is IMO an important part of learning and knowing linux.

Why is this important?

Imagine you have a terminal open and you're rsyncing a ginormous folder with millions of files from one server to another. Now suppose your terminal on the source server freezes. This would mean your multi-hour rsync terminates at 98 percent. Irritating, isn't it? Wars have been started for less!

So how to go about this?

A solution is to completely detach the process from the controlling terminal.


2. Running processes in the background

Rsync and scp are two popular commands to run in the background. 

# rsync -avh /home/a /home/b


Pressing ctrl-z stops and backgrounds the process.

[1]+ Stopped rsync -ar --partial /home/a /home/b

You can also background the job directly by appending an ampersand, &, to the command.

# rsync -avh /home/a /home/b &


Press bg to start the process in background again.

# bg
[1]+ rsync -ar --partial /home/a /home/b &


Enter jobs to see running processes.

# jobs
[1]+ Running rsync -ar --partial /home/a /home/b &


Run the process in the foreground by entering fg 1. 1 is the process number.

fg 1


3. Keep processes running after exiting the terminal

After the job has been started and backgrounded we'll use disown to remove the shell job from the active jobs list.

This way the job won't be killed when we exit the terminal!

We will not use fg and bg now.


Start a job and background it.

# rsync backups &
[1] 24504

# jobs
[1]+ Stopped                     rsync backups &

# disown -h %1

# jobs
[1]+ Running                     rsync backups &


You can use nohup for the same result too.

With some distros, nohup works better as a method.

# nohup rsync backups &
# jobs

[1]+ Running                     rsync backups &