在 CentOS 系统中,使用 nohup
命令可以在后台运行脚本,并且不受终端关闭的影响。以下是具体步骤:
编写脚本:首先,确保你有一个要运行的脚本文件。例如,假设你的脚本名为 my_script.sh
。
赋予执行权限:确保脚本具有执行权限。你可以使用以下命令赋予执行权限:
chmod +x my_script.sh
使用 nohup
运行脚本:使用 nohup
命令在后台运行脚本,并将输出重定向到一个文件中(例如 output.log
)。命令如下:
nohup ./my_script.sh > output.log 2>&1 &
解释:
nohup
:表示“no hang up”,即使终端关闭,进程也会继续运行。./my_script.sh
:运行你的脚本。> output.log
:将标准输出重定向到 output.log
文件。2>&1
:将标准错误输出重定向到标准输出,这样错误信息也会被写入 output.log
文件。&
:将命令放入后台运行。查看输出:你可以随时查看 output.log
文件来查看脚本的输出:
tail -f output.log
查找进程:如果你想查看脚本的进程 ID(PID),可以使用 ps
命令:
ps aux | grep my_script.sh
通过以上步骤,你可以在 CentOS 系统中使用 nohup
命令在后台运行脚本,并且不受终端关闭的影响。