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.

 

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
.
.

 

 

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 server1.foo.com:/backups &
[1] 24504

# jobs
[1]+ Stopped                     rsync backups server1.foo.com:/backups &

# disown -h %1

# jobs
[1]+ Running                     rsync backups server1.foo.com:/backups &

 

You can use nohup for the same result too.

With some distros, nohup works better as a method.

# nohup rsync backups server1.foo.com:/backups &
# jobs

[1]+ Running                     rsync backups server1.foo.com:/backups &

 

 

Sources

https://stackoverflow.com/questions/6141710/running-rsync-in-background

https://www.tecmint.com/run-linux-command-process-in-background-detach-process/

 

 

 

 

 

 

Stop Spam Harvesters, Join Project Honey Pot

 

Get a free SSL certificate!

 

The leading nonprofit defending digital privacy, free speech, and innovation.

 

The Linux Foundation provides a neutral, trusted hub for developers and organizations to code, manage, and scale open technology projects and ecosystems.

 

Kubuntu is an operating system built by a worldwide community of developers, testers, supporters and translators.

 

 43ef5c89 CanonicalUbuntudarktext