How to Run Linux Commands in Background

Introduction

Before we begin talking about how to run Linux Commands in the background, let's briefly understand – What is Background Process ?

A background process is a program or process that is launched from a terminal and runs in the background without requiring human intervention. It runs separately from the foreground applications and remains active even if the user switches to another task.

By running in the background, these processes perform various functions without disrupting or requiring constant attention from the user. They are typically initiated from a terminal or command line interface and continue executing without intervention until their tasks are completed.

In this tutorial, you will run Linux Commands in the background. We will also address a few FAQs on how to run Linux Commands in the background.

Advantages of Background Process

  1. Multitasking: Background processes allow users to perform multiple tasks simultaneously, enhancing productivity.
  2. System Efficiency: They keep the foreground applications responsive, ensuring smooth operation without slowdowns.
  3. Automated Tasks: Background processes automate repetitive or time-consuming tasks, saving users valuable time and effort.
  4. Continuous Updates: They enable applications to update seamlessly in the background, ensuring users have the latest features and security patches.
  5. Improved User Experience: With background processes handling various operations, users can enjoy uninterrupted usage of their devices.

Run a Linux Command in the Background

Add the ampersand character (&) to the end of a command to have it run in the background:

command &

On the terminal, the shell job ID (in brackets) and process ID will be printed:

Output

[1] 25177

You can have numerous background processes operating at the same time.

The background process will continue to send messages to the terminal you used to run the command from. Use the following syntax to disable stdout and stderr messages:

/dev/null 2>&1 means redirect stderr to stdout and stdout to /dev/null.

To see the status of all halted and background jobs in the current shell session, use the jobs utility:

jobs -l

The job number, process ID, job state, and the command that launched the job are all included in the output:

Output

[1]+ 25177 Running                 ping google.com &

Use the fg command to bring a background process to the foreground:

fg

Include % and the job ID after the command if you have numerous background jobs:

fg %1

Use the kill command followed by the process ID, to kill the background process.

kill -9 25177

Move a Foreground Process to Background

To put a running foreground process in the background, do the following:

  • Ctrl+Z will stop the process.
  • By typing bg, you can move the paused process to the background.

Keep Background Processes Running After a Shell Exits

The background processes are stopped if your connection drops or you log out of the shell session. After the interactive shell session has ended, there are numerous options for keeping the process running.

One method is to use the disown shell builtin to remove the job from the shell's job control:

disown

Include % and the job ID after the command if you have more than one background job:

disown %1

Using the jobs -l command, confirm that the job has been deleted from the table of active jobs. The ps aux command can be used to list all running processes, including those that are disowned.

Nohup is another approach to keep a process running after a shell exit.

The nohup command runs another program as an input while ignoring all SIGHUP (hangup) signals. When a process's controlling terminal is closed, a signal called SIGHUP is issued to it.

To use the nohup command to run a command in the background, type:

nohup command &

The output of the command is directed to the nohup.out file.

Output

nohup: ignoring input and appending output to 'nohup.out'

The process does not end when you log out or close the terminal.

Alternatives

Multiple interactive sessions can be held at the same time using a variety of tools.

Screen

Screen, also known as GNU Screen, is a terminal multiplexer application that lets you create a screen session and then open as many windows (virtual terminals) as you want within that session. Even if you are offline, processes running in Screen will continue to run even if their window is not visible.

Tmux

Tmux is a newer version of the GNU screen. You may also use Tmux to create a session and open numerous windows within it. Tmux sessions are persistent, which implies that programs running in Tmux keep running even if the terminal is closed.

FAQs to Run Linux Commands in Background

Can I switch to other tasks while the command runs in the background?

 Yes, running a command in the background allows you to continue using the terminal or perform other tasks simultaneously.

How can I check the status of background commands? 

You can use the "jobs" command to see a list of all running background processes along with their status and job numbers.

How do I bring a running command to the foreground?

 To bring a command running in the background to the foreground, use the "fg" command followed by the job number displayed by "jobs".

Can I send a running command back to the background?

Yes, you can suspend a foreground command by pressing "Ctrl+Z" and then use the "bg" command to move it to the background for continued execution.

How can I stop or terminate a background process?

First, identify the job number of the background process using "jobs". Then, use the "kill %job_number" command to terminate it.

How can I make a background command persist even after I log out? 

To make a command continue running after you log out, use the "nohup" command before the actual command. Example: "nohup command &".

How do I ensure a background command does not produce any terminal output? 

To prevent any terminal output from a background command, redirect both standard output and standard error to /dev/null. Example: "command > /dev/null 2>&1 &".

Conclusion

Include & at the end of a command to make it run in the background.

When you run a command in the background, you don't have to wait for it to complete before running another.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.