nohup(no hang-up)命令在Debian环境下用于在后台运行程序,即使关闭终端或断开SSH连接,程序也会继续运行。以下是一些使用nohup的技巧:
基本用法:
nohup command &
这将在后台运行command,并将输出重定向到名为nohup.out的文件。
指定输出文件:
nohup command > output.log 2>&1 &
这将把标准输出和标准错误都重定向到output.log文件。
在指定目录下创建输出文件:
nohup command > /path/to/output.log 2>&1 &
使用disown命令将进程从当前shell会话中分离:
nohup command & disown
使用screen或tmux等终端复用器在后台运行程序,这样可以在需要时重新连接到会话并查看输出。例如,使用screen:
screen -S mysession
command
Ctrl-A D # 按下Ctrl-A,然后按D键将screen会话分离到后台
要重新连接到会话,请运行:
screen -r mysession
使用nohup运行多个命令:
nohup bash -c 'command1; command2' &
这将在后台运行command1和command2。
使用nohup运行Python脚本:
nohup python script.py > output.log 2>&1 &
使用nohup运行Java程序:
nohup java -jar myapp.jar > output.log 2>&1 &
使用nohup运行Node.js脚本:
nohup node script.js > output.log 2>&1 &
使用nohup运行Shell脚本:
nohup bash script.sh > output.log 2>&1 &
总之,nohup命令在Debian环境下非常有用,可以帮助您在后台运行程序并确保它们在关闭终端或断开SSH连接后继续运行。