在 CentOS 上,结合使用 nohup 和 systemd 可以有效地管理系统进程
nohup 运行进程:nohup 是一个在 Unix 和类 Unix 系统上用于在后台运行程序的命令行实用程序。它允许程序在终端关闭后继续运行。要使用 nohup 运行进程,只需在命令前加上 nohup,并将输出重定向到一个名为 output.log 的日志文件中。例如:
nohup your_command > output.log 2>&1 &  这将在后台运行 your_command,并将标准输出和标准错误输出重定向到 output.log 文件。& 符号表示将进程放入后台运行。
为了使用 systemd 管理进程,您需要创建一个名为 your_service.service 的 systemd 服务文件。这个文件将包含有关如何运行和管理您的进程的信息。
在 /etc/systemd/system/ 目录下创建一个名为 your_service.service 的文件,并添加以下内容:
[Unit] Description=Your Service Description After=network.target [Service] User=your_user WorkingDirectory=/path/to/working/directory ExecStart=/usr/bin/nohup your_command > /path/to/output.log 2>&1 & Restart=always [Install] WantedBy=multi-user.target  请根据您的需求修改 Description、User、WorkingDirectory 和 ExecStart 等字段。
要使您的更改生效,请运行以下命令重新加载 systemd 配置:
sudo systemctl daemon-reload  接下来,启动新创建的服务:
sudo systemctl start your_service  如果您希望在系统启动时自动启动此服务,请运行以下命令:
sudo systemctl enable your_service  要查看服务的当前状态,请运行以下命令:
sudo systemctl status your_service  这将显示有关服务状态的详细信息,包括是否正在运行、启动状态以及任何错误消息。
通过以上步骤,您可以在 CentOS 上结合使用 nohup 和 systemd 来管理进程。这样,您可以轻松地启动、停止、重启和管理后台进程。