How to run Linux commands in the background

How to run Linux commands in the background

Normally, when you run a command in the terminal, you have to wait for the current command to finish before you can start typing another command. This is called running the command in the foreground, or the foreground process. When a process is running in the foreground, it occupies your shell and you can interact with it through your input devices.

So what should you do when a command takes a long time to run and you want to run other commands at the same time? There are several options to choose from. The most obvious and direct option is to start a new shell session and run the command from there. Another option is to run the command in the background.

A background process is a process/command that runs in the background after the terminal is started and does not interact with the user.

In this article, we will discuss background processes in Linux. We will show you how to start a command in the background and keep the process running until the session ends.

Run a Linux command in the background

To run a command in the background, add an ampersand ( & ) after the command:

command &

The shell's task id (the content enclosed in brackets) and process ID will be printed to the terminal:

[1] 25177

You can run many processes in the background at the same time.

The background process will continue to write information to the terminal. To suppress stdout and stderr messages, use the following syntax:

command > /dev/null 2>&1 &

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

Use the jobs command to display the status of all stopped and background tasks in the current shell session.

jobs -l

The output includes the task id, process id, task status, and the command that started the task:

[1]+ 25177 Running ping google.com &

If you want to bring the background process to the foreground, use the fg command:

fg

If you have multiple tasks in the background, add % + task ID after the command:

fg %1

To stop a background process, add the process ID after the kill command:

kill -9 25177

Move the foreground process to the background

To move a foreground process to the background:

01. Stop the current process by pressing Ctrl+Z

02. Move the stopped process to the background by typing bg

Keep the background process running until the shell exits

If you lose the connection, or if you exit the shell session, the background process will be terminated. There are a number of ways to ensure that a process runs until the interactive shell terminates.

One way is to remove the task from Shell task control, using disown built-in:

disown

If you have many background tasks, add % + task ID after the command:

disown %1

Verify that the job has been removed from the job list by using jobs -l . In order to list the running processes, use the ps aux command.

Another way to ensure that the process runs until the shell exits is to use nohup .

The nohup command followed by another program as an argument will ignore all SIGHUP (hangup) signals. SIGHUP signal is sent to the process to notify it that the terminal has been closed.

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

nohup command &

The command output will be redirected to the nohup.out file.

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

If you log out or close the terminal, the process will not be terminated.

Alternatives

Some programs allow you to have multiple non-interactive sessions at the same time.

Screen

Screen or GNU Screen is a terminal multiplexer that allows you to open a screen session and open any number of windows (virtual terminals) within that session. Processes running in Screen will continue to run even if the window is not visible or you lose connection.

Tmux

Tmux is a modern GNU screen interaction program. With Tmux, you can create a session and then open multiple windows in that session. Tmux sessions are persistent, which means that even if you close the terminal, the programs running on Tmux will still be running.

Summarize

To run a command in the background, add the & symbol after the command.

When you run a command in the background, you can run another command without waiting for it to finish.

The above is the detailed content of how to run Linux commands in the background. For more information about running Linux commands in the background, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Use of Linux gzip command
  • Usage of Linux userdel command
  • Use of Linux date command
  • Use of Linux stat command
  • Use of Linux ls command
  • Use of Linux ln command
  • Linux cut command explained
  • Use of Linux bzip2 command

<<:  MySQL common backup commands and shell backup scripts sharing

>>:  In-depth discussion of memory principles: Are variables stored in the heap or stack in JS?

Recommend

Detailed explanation of the problems and solutions caused by floating elements

1. Problem Multiple floating elements cannot expa...

What is COLLATE in MYSQL?

Preface Execute the show create table <tablena...

Button is stretched on both sides in IE

When you write buttons (input, button), you will f...

Native js to implement drop-down menu

Drop-down menus are also very common in real life...

JS implements request dispatcher

Table of contents Abstraction and reuse Serial Se...

Summary of JavaScript's setTimeout() usage

Table of contents 1. Introduction 2. The differen...

Solve the problem of ifconfig being unavailable in docker

Recently, when I was learning docker, I found tha...

Uniapp realizes sliding scoring effect

This article shares the specific code of uniapp t...

How to install openjdk in docker and run the jar package

Download image docker pull openjdk Creating a Dat...

JS quickly master ES6 class usage

1. How to construct? Let's review the common ...

Vue implements paging function

This article example shares the specific code of ...

Angular framework detailed explanation of view abstract definition

Preface As a front-end framework designed "f...

Let's talk about bitwise operations in React source code in detail

Table of contents Preface Several common bit oper...

Detailed steps to build the TypeScript environment and deploy it to VSCode

Table of contents TypeScript environment construc...