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

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.