在 CentOS 中,你可以使用 systemd
服务来实现命令的自动重启。以下是创建一个 systemd
服务的步骤:
vi
或 nano
)创建一个新文件,例如 /etc/systemd/system/my_service.service
。将 “my_service” 替换为你的服务名称。sudo vi /etc/systemd/system/my_service.service
[Unit]
Description=My custom service
After=network.target
[Service]
Type=simple
ExecStart=/path/to/your/command
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target
将 /path/to/your/command
替换为你想要自动重启的命令。Restart=always
表示无论命令退出状态如何,都将始终重启服务。RestartSec=5
表示在重启服务之前等待 5 秒。
保存并关闭服务文件。
重新加载 systemd
配置。运行以下命令,以便 systemd
识别新创建的服务:
sudo systemctl daemon-reload
sudo systemctl start my_service
将 “my_service” 替换为你的服务名称。
sudo systemctl enable my_service
将 “my_service” 替换为你的服务名称。
现在,你的命令将在系统启动时自动运行,并在意外停止时自动重启。你可以使用以下命令查看服务状态:
sudo systemctl status my_service
将 “my_service” 替换为你的服务名称。