| by Arround The Web | No comments

How to Run a Process in the Background in Linux

You must know the services that are running in the background if you run a command that takes much time to process. It is a crucial concept in process handling and can benefit you in different approaches. Furthermore, whatever process you send into the background runs independently which allows you to start another process alongside.

However, most users are unaware of its methods and are deprived of their devices’ most effective yet basic functionality. So, in this quick guide, we aim to explain how to run a process in the background in Linux.

How to Run a Process in the Background in Linux

By running a process in the background, you can free your terminal from a long-running process and run multiple commands simultaneously. There are only two simple methods for it. We divided this section further into two parts to explain each of them at a time.

1. The Bg Command

If you entered a command and later realized its execution time, use the “bg” command. To enter this command, pause the ongoing process by pressing the “CTRL + Z” keys. Then, enter the “bg” command.

For example, let’s create a dummy job using the “sleep” command and send it to the background.

bg

After creating the dummy job for 50 seconds, we execute the “bg” command that sends the process to the background.

Use the “jobs” command to view the queue of the pending background processes.

jobs

2. Appending “&”

You can use the ampersand symbol “&” with your command to run it in the background.

command &

For instance, let’s send a dummy process to the background using ampersand.

sleep 15 &

In the previous image, [1] is the task’s ID, and the task IDs will increase in subsequent tasks. For example, the following task ID is [2]. Now, you can see the job status again by entering the following command:

jobs

3. The Tmux Command

You can also use the “tmux” command to create multiple terminal sessions. Create a new session, start any process that you want, and leave that session. Meanwhile, your process in that session will keep running in the background. To use the “tmux” command, you must install the tmux utility using the following commands:

sudo apt update
sudo apt upgrade -y
sudo apt install tmux -y

To create a new session, enter the following command:

tmux new-session -s session_1

Here, you can replace the term “session_1” with any name that you want to give to the new session. Just after entering the given command, a terminal tab will open. You can enter your desired commands there and press the “CTRL + B” and D combination to detach the session from the main session.

This detached session stays active in the background. If you desire to add some commands to it, attach that session back by running the following command:

tmux attach-session -t session_1

In place of “session_1”, type the name of the session that you want to connect.

Conclusion

If you run a process in the background, it helps you to work effectively and efficiently by running multiple processes simultaneously. We explained the three simple ways to run the processes in the background – using the “bg” command, “ampersand”, and “tmux” command. Although tmux requires an installation, it is the easiest way to run the different processes simultaneously once installed.

Share Button

Source: linuxhint.com

Leave a Reply