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

CSS XTHML writing standards and common problems summary (page optimization)

Project Documentation Directory Div+CSS Naming Sta...

Linux uses bond to implement dual network cards to bind a single IP sample code

In order to provide high availability of the netw...

Simple steps to configure Nginx reverse proxy with SSL

Preface A reverse proxy is a server that receives...

Detailed analysis of the difference between Ref and Reactive in Vue3.0

Table of contents Ref and Reactive Ref Reactive T...

Analysis of the principle and creation method of Mysql temporary table

This article mainly introduces the principle and ...

Summary of commonly used tool functions in Vue projects

Table of contents Preface 1. Custom focus command...

Today I encountered a very strange li a click problem and solved it myself

...It's like this, today I was going to make a...

Complete steps to configure basic user authentication at the Nginx level

Preface Application scenario: probably the intern...

Vue basic instructions example graphic explanation

Table of contents 1. v-on directive 1. Basic usag...

SQL Server Comment Shortcut Key Operation

Batch comments in SQL Server Batch Annotation Ctr...

In-depth understanding of MySQL various locks

Table of contents Lock Overview Lock classificati...

Example usage of Linux compression file command zip

The ".zip" format is used to compress f...

Implementation of element multiple form validation

In the project, form testing is often encountered...