在Ubuntu系统中,可以使用systemd来管理服务的启动和停止。要设置进程自动重启,你需要创建一个systemd服务单元文件。以下是创建和设置自动重启的步骤:
your-service-name
替换为你想要的服务名称:sudo nano /etc/systemd/system/your-service-name.service
ExecStart
行中的命令替换为你想要自动重启的进程的命令。[Unit]
Description=Your service description
After=network.target
[Service]
Type=simple
User=your-user
WorkingDirectory=/path/to/your/working/directory
ExecStart=/path/to/your/command
Restart=always
RestartSec=3
[Install]
WantedBy=multi-user.target
这里的Restart=always
表示无论进程退出状态如何,systemd都会自动重启服务。RestartSec=3
表示在重启之前等待3秒。
保存并关闭文件。
重新加载systemd配置以识别新创建的服务:
sudo systemctl daemon-reload
sudo systemctl start your-service-name.service
sudo systemctl enable your-service-name.service
现在,你的进程将在系统启动时自动运行,并在意外停止时自动重启。你可以使用以下命令查看服务的状态:
sudo systemctl status your-service-name.service