在 CentOS 中,使用 nohup
命令可以在后台运行脚本,即使关闭终端或断开连接,脚本也会继续执行
打开终端。
使用 cd
命令导航到脚本所在的目录。例如,如果脚本位于 /home/user/scripts
目录中,请输入 cd /home/user/scripts
。
确保脚本具有可执行权限。如果没有,请使用以下命令添加可执行权限:chmod +x script_name.sh
(将 script_name.sh
替换为实际的脚本文件名)。
使用 nohup
命令运行脚本。例如,要使用 nohup 在后台运行名为 script_name.sh
的脚本,请输入以下命令:
nohup ./script_name.sh &
这里的 &
符号表示将脚本放入后台运行。
nohup: ignoring input and appending output to 'nohup.out'
这意味着脚本的输出已被重定向到名为 nohup.out
的文件中。你可以使用 tail
命令查看输出,例如:tail -f nohup.out
。
nohup.out
文件即可。注意:如果你想将脚本的输出重定向到其他文件,可以使用以下命令:
nohup ./script_name.sh > output_file.txt &
这将把脚本的输出保存到名为 output_file.txt
的文件中,而不是 nohup.out
。