Linux nohup command
nohup The full English name is no hang up, used to run commands in the system background without hanging up; exiting the terminal does not affect the program.
nohup By default (without redirection), the command writes output to a file named nohup.out in the current directory. If nohup.out in the current directory is not writable, output is redirected to $HOME/nohup.out file.
Permissions
All users
Syntax format
nohup Command [ Arg … ] [ & ]
Parameter Description:
Command: The command to execute.
Arg: Some options to specify the output file.
&: Run the command in the background; it continues to run after the terminal exits.
Example
The following command runs the runoob.sh script in the root directory in the background:
nohup /root/runoob.sh &
If you see the following output in the terminal, it means the run was successful:
appending output to nohup.out
Now open the root directory to see the generated nohup.out file.
To stop it, use the following commands to find the PID of the nohup script, then use the kill command to terminate it:
ps -aux | grep "runoob.sh"
Parameter Description:
- a : Display all processes
- u : Display in a user-oriented format
- x : Display all processes, regardless of terminal
You can also use the ps -def | grep "runoob.sh" command to find it.
Once you have the PID, use kill PID to terminate it.
kill -9 进程号PID
The following command runs the runoob.sh script in the root directory in the background and redirects its output to the runoob.log file:
nohup /root/runoob.sh > runoob.log 2>&1 &
2>&1 Explanation:
Redirect standard error 2 to standard output &1, and then standard output &1 is redirected into the runoob.log file.
- 0 - stdin (standard input)
- 1 - stdout (standard output)
- 2 - stderr (standard error)
Linux Command Reference