在CentOS中,使用nohup命令可以在后台运行Shell脚本,即使关闭终端或断开SSH连接,脚本也会继续运行
打开终端。
使用cd命令导航到包含Shell脚本的目录。例如,如果脚本位于/home/user/scripts目录中,请输入:
cd /home/user/scripts
nohup命令运行Shell脚本。例如,如果脚本名为my_script.sh,请输入:nohup ./my_script.sh &
这里,&符号将脚本放入后台运行。
nohup命令会将脚本的输出重定向到一个名为nohup.out的文件中。如果你想将输出重定向到其他文件,请使用以下命令:nohup ./my_script.sh > output.log &
这将把输出保存到output.log文件中。
现在,你可以关闭终端或断开SSH连接。脚本将继续在后台运行。
如果你想检查脚本的输出,可以使用tail命令查看nohup.out或output.log文件的内容。例如:
tail -f nohup.out
或者
tail -f output.log
这样,你就可以在CentOS中使用nohup命令运行Shell脚本了。