在 CentOS 上,nohup(no hang-up)命令用于在后台运行程序,即使关闭终端或断开连接,程序也会继续运行
打开终端。
输入 nohup 命令,后跟你想要在后台运行的命令。例如,如果你想在后台运行名为 my_script.sh 的脚本,你可以输入:
nohup ./my_script.sh &
这里,& 符号将命令放入后台运行。
nohup 会自动将输出重定向到一个名为 nohup.out 的文件中。你可以在当前目录下找到这个文件,查看程序的输出。nohup: ignoring input and appending output to 'nohup.out'
nohup ./my_script.sh > output.log 2>&1 &
这里,> 将标准输出重定向到 output.log 文件,2>&1 将标准错误重定向到标准输出,& 将命令放入后台运行。
ps 命令:ps aux | grep my_script.sh
kill 命令,后跟进程 ID(PID):kill PID
将 PID 替换为实际的进程 ID。
现在你已经知道如何在 CentOS 上使用 nohup 在后台运行命令了。