English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Linux nohup command

Linux Command大全

nohup The full name in English is no hang up (not hang up), used to run commands in the system background without interruption. Exiting the terminal will not affect the operation of the program.

nohup The 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 will be redirected to $HOME/nohup.out in the file.

Permission to use

All users

Syntax format

 nohup Command [Arg …] [ & ]

Parameter Description:

Command: The command to be executed.

Arg: Some parameters can specify the output file.

&: Allows commands to run in the background. The command will still execute even after the terminal exits.

Online examples

The following command executes the w in the root directory in the background:3codebox.sh script:

nohup /root/w3codebox.sh &

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

appending output to nohup.out

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

If you want to stop it, you need to use the following command to find the PID of the script running nohup and then use the kill command to delete it:

ps -aux | grep "w3codebox.sh" 

Parameter Description:

  • a : Show all programs
  • u : Display programs in a user-centered format
  • x : Show all programs, without distinguishing between terminals

You can also use ps -def | grep "w3Use the 'codebox.sh' command to find.

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

kill -9  Process ID PID

The following command runs the w in the root directory in the background3Run the codebox.sh script and redirect input to w3codebox.log file:

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

2>&1 Explanation:

Redirect standard error 2 Redirected to standard output &1 , standard output &1 Then it is redirected as input to w3in the codebox.log file.

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

Linux Command大全