Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Linux nohup command

Linux nohup command

Linux Command Reference Linux Command Reference

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:

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.

Linux Command Reference Linux Command Reference