Linux nohup Command

Linux 命令大全Linux Command Collection

nohupThe full English name is no hang up (not hanging up). It is used to run commands continuously in the system background; exiting the terminal will not affect the program's operation.

nohupThe command, by default (when not redirected), will output a file named nohup.out to the current directory. If the nohup.out file in the current directory is not writable, the output is redirected to$HOME/nohup.outthe file.

Usage Permissions

All users

Syntax Format

 nohup Command [ Arg … ] [ & ]

Parameter Description:

Command: The command to be executed.

Arg: Some parameters; you can specify an output file.

&: Run the command in the background; the command still executes after the terminal exits.

Examples

The following command runs the example.sh script in the root directory in the background:

nohup /root/example.sh &

If you see the following output in the terminal, it means the operation was successful:

appending output to nohup.out

At this point, open the root directory and you can see that the nohup.out file has been generated.

To stop it, you need to use the following command to find the PID of the nohup running script, then use the kill command to terminate it:

ps -aux | grep "example.sh" 

Parameter Description:

  • a: Display all programs
  • u: Display in a user-oriented format
  • x: Display all programs, regardless of terminal

You can also useps -def | grep "example.sh" command to search.

After finding the PID, you can use kill PID to terminate it.

kill -9  进程号PID

The following command runs the example.sh script in the root directory in the background and redirects output to the example.log file:

nohup /root/example.sh > example.log 2>&1 &

2>&1Explanation:

Redirect standard error 2 to standard output &1, and standard output &1 is then redirected to the example.log file.

  • 0 – stdin (standard input)
  • 1 – stdout (standard output)
  • 2 – stderr (standard error output)

Linux 命令大全Linux Command Collection

Other Extensions