How To Run A Process In Background In Linux/Unix?

How To Run A Process In Background In Linux/Unix?

   

In a Linux/Unix system, a process is an instance of a program that is running on a computer. When a program starts executing it can have multiple processes associated with it. A process can start a subprocess, which is called a child process. And the process that starts a subprocess will be known as the parent process. The init process is the very first process that gets started when a Linux machine starts. Every process of a system are the children of the init process. As I already discussed running a process in the background in an article. In this article, we will discuss the same in more detail.

Two ways of starting a process

A process can be started in two ways –

1. In foreground –

By default, a process started by a user runs in the foreground. A foreground process takes input from the command prompt and displays output to the computer screen. If a foreground process is running terminal prevents initiating a new process until the existing one does not get finished.

2. In background –

Running a process in the background does not require keyboard input. You can start another process from the terminal while a process is running in the background.

Why do we need to run a process in the background?

Suppose you have a long-running task, that can take up to several minutes to complete. By default, a process starts in the foreground and it can take input from the user. So it keeps terminal busy until the task doesn’t get completed. A background process runs independently from the shell leaving the terminal free for other work. It is not always wise to wait for a long-running task to finish. Then what you can do? You can start a long-running task in the background or you can send a running task into the background.

Starting a process in the background

To start a process in the background include & (an ampersand) at the end of the command.

command &

For example –

Taking backup can take up to several minutes, you can run it in the background.

tar -cpzf home_backup.tar.gz --exclude=/home_backup.tar.gz .&

Once you execute this command this will show job id and process id. Now if you look in the image above 1 surrounded with the brackets is the job Id and 4397 is the process Id.

To display all the commands or processes running in the background, use the following command in the terminal-

jobs

This command will list all the jobs with their job id and status.

Now to bring a background process to foreground use the following command –

fg

Or if you have multiple jobs then use –

fg %Job_Id

For example –

fg %1

Moving a foreground process to background

If a foreground process is taking too much time. You can send it to the background while it is running by using the below steps –

1. Press ctrl+z to stop a process this will suspend the process but still exist in the system.

2. You can check the suspended process with the jobs command

3. Now execute –

bg

Conclusion

In this article, you learned to run a command in the background. If you have any questions regarding this topic you can write to us.

Previous How To Install a Package From .tar.gz Or .tar.bz2 File In Linux?
Next How To Install Or Remove A Package With Synaptic Package Manager In Ubuntu/Linux Mint?

About author

You might also like

Uncategorized 0 Comments

How To Manage KVM Virtual Machines From The Host Terminal?

Virtualization means creating virtual instances of resources such as storage devices, network resources, etc. Kernel-based virtual machine or KVM is a virtualization module in the Linux Kernel which turns the

Uncategorized 0 Comments

The Concept Of RAID

Originally the term RAID defined as “Redundant Array of Inexpensive Disks” but now it is referred to as “Redundant Array of Independent Disk”. It is a data storage virtualization technology.

Uncategorized 0 Comments

Process Management In Linux/Unix

This post was last updated on September 15th, 2019 at 05:39 pmA process is an instance of a computer program that is currently running. A program can have multiple instances

0 Comments

No Comments Yet!

You can be first to comment this post!

Leave a Reply