banner
三文字

方寸之间

居善地,心善渊,与善仁,言善信,正善治,事善能,动善时。
github
email
misskey

Analysis of the nohup command in Linux

Introduction#

When we execute a long-running command or script on a Linux or Unix system, we often encounter a problem where the process stops running when the terminal is closed or exited. In such cases, we need a way to run the process in the background without being affected by the terminal closure. This is where the nohup command comes in handy.

What is nohup?#

nohup is a command in Linux and Unix systems that allows a process to continue running in the background even after the terminal is closed. Its full name is "no hang up," meaning "do not hang up." The nohup command allows you to continue running a command after exiting the terminal or closing an SSH connection.

Syntax rules for nohup#

The basic syntax of the nohup command is as follows:

nohup COMMAND [ARGS ...] [> output-file 2> error-file] &

The meanings of the parameters are as follows:

  • COMMAND: The command or script to run in the background.
  • ARGS: The arguments for the command or script.
  • > output-file: Redirects the output to the specified file.
  • 2> error-file: Redirects the error messages to the specified file.
  • &: Runs the command in the background.

The execution process of the nohup command consists of the following steps:

  1. The nohup command redirects the standard input, standard output, and standard error output of the current shell to the /dev/null device to avoid being interrupted by the signal of closing the terminal.
  2. The nohup command puts the process in the background and outputs the process ID (PID) to the terminal.
  3. The process starts executing and redirects the standard output and standard error output to the specified file.
  4. The user can exit the terminal or close the terminal window, and the process will continue running in the background.

How to use nohup#

Using the nohup command is very simple, just follow the basic syntax mentioned above. Here are some examples of how to use the nohup command:

Running a command in the background#

To run a command in the background, simply enter the following command in the command line:

nohup COMMAND &

For example, to run a Bash script in the background:

nohup bash test.sh &

Redirecting standard output to a file#

nohup bash test.sh > stdout.txt &

Redirecting standard error to a file#

nohup bash test.sh 2> stderr.txt &

Redirecting both standard output and standard error to a file#

  1. Redirecting to the same file
nohup bash test.sh > output.txt 2>&1 &
  1. Redirecting to different files
nohup bash test.sh > stdout.txt 2> stderr.txt &
  1. A more complex example, redirecting standard input (stdin):
nohup ./myprogram > foo.out 2> foo.err < /dev/null &

Here, an additional < /dev/null is used to redirect the standard input to /dev/null to ensure that the program does not read any data from the standard input.

This is to solve a practical problem: SSH sessions often refuse to log out (or hang up) because they don't want to lose data that interacts with background job(s). When encountering this problem, you can use the above command to solve it by redirecting three times.

nohup background process management#

The jobs command can be used to view the list of background tasks currently running in the shell, including task numbers, status, and commands.

For example, if we run the sleep 1000 command in the background and use the jobs command to view it:

$ jobs
[1]+  Running                 nohup sleep 1000 &

In the above output, the number in the square brackets represents the task number, the plus or minus sign represents the priority of the task, and Running indicates that the task is running in the background. In addition to this, there are other possible statuses, including Stopped, Done, etc.

We can also use the fg command to move a background task to the foreground and continue running it, for example:

$ fg %1

This command will move the task with the task number 1 to the foreground and continue execution. If you want to pause or resume a task, you can use the Ctrl-Z key to send the SIGTSTP signal in the current shell.

$ fg %1
nohup sleep 1000

^Z
[1]+  Stopped                 nohup sleep 1000

If you want to resume execution at this point, you can use the bg command:

$ bg %1
[1]+ nohup sleep 1000 &

If you want to kill the task, you can use the kill command:

$ kill %1
[1]+  Terminated              nohup sleep 1000

If you want to kill all background tasks but find it cumbersome to do it one by one, you can use the disown command:

$ disown -a

This command kills all background tasks without any prompts, and you can confirm it by using the jobs command.

Summary#

The nohup command allows programs to run in the background without being affected by the closure of the terminal or the disconnection from a remote server. Processes started with the nohup command ignore all terminal signals, so even if you use Ctrl+C to close the terminal, the process will not stop. You can use the jobs command to view background processes and use the fg command to switch a process to the foreground.

nohup is ideal for running scripts or programs that require a long time to execute, such as web servers and databases. However, it should be noted that the nohup command does not completely prevent the possibility of a process being interrupted, such as system crashes or process errors.

References#

https://en.wikipedia.org/wiki/Nohup


Welcome to visit my personal blog: 方寸之间

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.