How to use & and nohup in the background of Linux

How to use & and nohup in the background of Linux

When we work in a terminal or console, we may not want to occupy the screen by running a job, because there may be more important things to do, such as reading emails. For processes that intensively access the disk, we prefer that they run during non-peak hours of the day (such as early morning). To enable these processes to run in the background, that is, not on the terminal screen, several options are available.

&

When a job is running in the foreground, the terminal is occupied by the job; you can add & after the command to run it in the background. For example: sh test.sh &
Commands suitable for running in the background include find, time-consuming sorting, and some shell scripts. Be careful when running jobs in the background: do not run commands that require user interaction in the background, because then your machine will just sit there waiting. However, jobs running in the background will still output results to the screen, interfering with your work. If the job you want to run in the background generates a lot of output, it is best to redirect its output to a file using the following method:

command > out.file 2>&1 &

In this way, all standard output and error output will be redirected to a file called out.file.

PS: After you successfully submit the process, a process ID will be displayed, which can be used to monitor the process or kill it. (ps -ef | grep process number or kill -9 process number)

nohup

After using the & command, the job is submitted to the background for execution. The current console is not occupied, but once the current console is closed (when logging out of the account), the job will stop running. The nohup command can continue running the corresponding process after you log out of your account. nohup means no hang up. The general form of the command is:

nohup command &

If you submit a job using the nohup command, then by default all output from the job is redirected to a file named nohup.out, unless an output file is specified otherwise:

nohup command > myout.file 2>&1 &

After using nohup, many people just ignore it. In fact, it is possible that the command will end automatically when the current account is exited or ended abnormally. Therefore, after using the nohup command to run the command in the background, you need to use exit to exit the current account normally to ensure that the command continues to run in the background.

ctrl + z
A command being executed in the foreground can be put into the background and put into a paused state.

Ctrl+c
Terminates the foreground command.

jobs
Check how many commands are currently running in the background.

The jobs -l option displays the PID of all tasks. The status of jobs can be running, stopped, or Terminated. But if the task is terminated (killed), the shell removes the task's process ID from the list known to the current shell environment.

2>&1Analysis

command >out.file 2>&1 &
  1. command>out.file redirects the output of command to the out.file file, that is, the output content is not printed on the screen, but output to the out.file file.
  2. 2>&1 redirects the standard error to the standard output. The standard output here has been redirected to the out.file file, that is, the standard error is also output to the out.file file. The last & causes the command to be executed in the background.
  3. Think about what 2>1 means. 2 combined with > means error redirection, and 1 means that the error is redirected to a file 1, not standard output. If it is changed to 2>&1, & combined with 1 means standard output, and the error is redirected to standard output.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Linux nohup to run programs in the background and view them (nohup and &)
  • Solve the problem of python nohup linux background running output
  • PHP daemon process plus Linux command nohup to implement task execution once per second
  • Detailed explanation of the solution to the problem of nohup log output being too large under Linux
  • Linux nohup and tail-f usage
  • Nohup and background running process viewing and termination in Linux

<<:  Information transmission and function calls between WeChat mini program pages and components

>>:  WeChat applet implements the snake game

Recommend

Several ways to schedule backup of MySQL database (comprehensive)

Table of contents 1. mysqldump command to back up...

CSS and CSS3 flexible box model to achieve element width (height) adaptation

1. CSS realizes fixed width on the left and adapt...

Detailed explanation of Linux text editor Vim

Vim is a powerful full-screen text editor and the...

How to use module fs file system in Nodejs

Table of contents Overview File Descriptors Synch...

MySQL deadlock routine: inconsistent batch insertion order under unique index

Preface The essence of deadlock is resource compe...

Analysis and Solution of ERROR:2002 Reported When MySQL Starts

Preface This article mainly introduces the analys...

【HTML element】Detailed explanation of tag text

1. Use basic text elements to mark up content Fir...

Vue3 uses axios interceptor to print front-end logs

Table of contents 1. Introduction 2. Use axios in...

Why TypeScript's Enum is problematic

Table of contents What happened? When to use Cont...

How to encapsulate timer components in Vue3

background When you open the product details on s...

XHTML Getting Started Tutorial: XHTML Tags

Introduction to XHTML tags <br />Perhaps you...

Small details of web front-end development

1 The select tag must be closed <select><...

UDP DUP timeout UPD port status detection code example

I have written an example before, a simple UDP se...

WeChat applet realizes the function of uploading pictures

This article example shares the specific code for...

Sample code for separating the front-end and back-end using FastApi+Vue+LayUI

Table of contents Preface Project Design rear end...