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

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

 

This post was last updated on August 1st, 2020 at 02:11 pm

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 is the children of the init process.

In this article, we will discuss how to run a process in the background in Linux or Unix.

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

Linux Topics 0 Comments

File Ownership And Permissions In Linux/Unix

This post was last updated on August 3rd, 2020 at 03:11 pmLinux and Unix are multi-user operating systems that means different users can access the operating system at a time

Linux Topics 0 Comments

MX Linux vs Manjaro – A Detailed Analysis

This post was last updated on March 1st, 2021 at 01:26 pmHaving so many Linux distros out there, it is difficult for beginners to figure out which one is better

How To 0 Comments

How To Add A User To A Group In FreeBSD?

This post was last updated on August 1st, 2020 at 12:12 pmIn FreeBSD, a group is a list of users. It makes it easier to define the permissions such as

0 Comments

No Comments Yet!

You can be first to comment this post!

Leave a Reply