nohup
(no hang-up)命令在 Debian 中用于在后台运行程序,即使关闭终端或断开连接,程序也会继续运行
使用 nohup
命令的基本语法如下:
nohup command-to-run &
其中,command-to-run
是您要运行的命令,&
符号将命令放入后台运行。
例如,如果您想在后台运行名为 my_script.sh
的脚本,可以使用以下命令:
nohup ./my_script.sh &
运行此命令后,您会看到类似以下的输出:
nohup: ignoring input and appending output to 'nohup.out'
这意味着程序的输出已被重定向到名为 nohup.out
的文件中。您可以使用 tail
命令查看输出:
tail -f nohup.out
如果您希望将输出重定向到其他文件,可以使用以下命令:
nohup ./my_script.sh > output.log 2>&1 &
这将把标准输出和错误输出都重定向到 output.log
文件中。
要查看后台运行的进程,可以使用 ps
命令:
ps aux | grep my_script.sh
要停止后台运行的进程,可以使用 kill
命令,后面跟上进程 ID(PID):
kill PID
例如,如果进程 ID 为 12345,可以使用以下命令停止进程:
kill 12345