nohup(no hang-up)命令在Debian和其他Linux发行版中用于在后台运行命令,即使关闭终端或断开SSH连接,该命令也会继续运行
基本用法:
nohup command &
将command替换为您要在后台运行的命令。&符号将命令放入后台运行。
输出重定向:
默认情况下,nohup会将命令的输出发送到名为nohup.out的文件。您可以通过以下方式更改输出文件:
nohup command > output.log &
这将把输出重定向到output.log文件。
错误输出重定向: 类似于输出重定向,您还可以将错误输出重定向到不同的文件:
nohup command > output.log 2> error.log &
这将把标准输出发送到output.log文件,将错误输出发送到error.log文件。
同时重定向输出和错误输出:
nohup command > output.log 2>&1 &
这将把标准输出和错误输出都发送到output.log文件。
在后台运行多个命令:
要在后台运行多个命令,您可以使用分号(;)或双与符号(&&)连接它们:
nohup command1 ; command2 &
或
nohup command1 && command2 &
这将在后台运行command1,然后运行command2。请注意,使用&&时,只有当command1成功执行时,command2才会运行。
使用nohup运行Python脚本:
nohup python script.py > output.log 2> error.log &
这将在后台运行名为script.py的Python脚本,并将输出和错误输出分别重定向到output.log和error.log文件。
查找正在运行的nohup进程:
使用ps命令查找正在运行的nohup进程:
ps aux | grep nohup
终止nohup进程:
首先,使用ps命令找到要终止的nohup进程的进程ID(PID):
ps aux | grep nohup
然后,使用kill命令终止进程:
kill PID
将PID替换为实际的进程ID。