在CentOS中,nohup命令用于在后台运行程序,即使关闭终端或断开SSH连接,程序也会继续运行。要确保进程持续运行,请按照以下步骤操作:
打开终端。
使用nohup命令启动程序。例如,如果要运行名为my_script.sh的脚本,请输入以下命令:
nohup ./my_script.sh &
这里的&符号表示将程序放入后台运行。
nohup命令会将程序的输出重定向到一个名为nohup.out的文件中。你可以使用以下命令查看输出:tail -f nohup.out
nohup ./my_script.sh > output.log 2>&1 &
这将把标准输出和错误输出都重定向到output.log文件中。
systemd创建一个服务单元。首先,创建一个名为my_service.service的文件:sudo vi /etc/systemd/system/my_service.service
my_service.service文件中,添加以下内容:[Unit]
Description=My custom service
[Service]
ExecStart=/path/to/your/script.sh
Restart=always
User=username
Group=groupname
Environment=PATH=/usr/bin:/usr/local/bin
[Install]
WantedBy=multi-user.target
将/path/to/your/script.sh替换为你的脚本路径,username和groupname替换为运行脚本的用户和组。
sudo systemctl enable my_service.service
sudo systemctl start my_service.service
sudo systemctl status my_service.service
现在,你的进程将在CentOS中持续运行,即使关闭终端或断开SSH连接。