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

How to modify the root user password in mysql 8.0.16 winx64 and Linux

Please handle basic operations such as connecting...

Shorten the page rendering time to make the page run faster

How to shorten the page rendering time on the bro...

Example of using nested html pages (frameset usage)

Copy code The code is as follows: <!DOCTYPE ht...

Vue implements the product tab of the product details page function

This article example shares the specific code of ...

JavaScript function call, apply and bind method case study

Summarize 1. Similarities Both can change the int...

A simple method to regularly delete expired data records in MySQL

1. After connecting and logging in to MySQL, firs...

MySQL 8.0.23 free installation version configuration detailed tutorial

The first step is to download the free installati...

Detailed explanation of MySQL database triggers

Table of contents 1 Introduction 2 Trigger Introd...

How to detect if the current browser is a headless browser with JavaScript

Table of contents What is a headless browser? Why...

Build a file management system step by step with nginx+FastDFS

Table of contents 1. Introduction to FastDFS 1. I...

How to use @media in mobile adaptive styles

General mobile phone style: @media all and (orien...

How to change mysql password under Centos

1. Modify MySQL login settings: # vim /etc/my.cnf...

How to build Apr module for tomcat performance optimization

Preface Tomcat is a widely used Java web containe...

A brief discussion on how to learn JS step by step

Table of contents Overview 1. Clearly understand ...