首页 > 解决方案 > 如果 Unix 进程关闭,它会自行启动

问题描述

设想:

我有 UNIX 进程 myshellscript.sh ,如果 sh 脚本没有运行或停止,我希望重新启动该进程。

注意:不想使用 crontab

标签: unix

解决方案


如果进程因任何原因关闭,shell 脚本将在 5 秒内重新启动。

01.在/etc/systemd/system/中添加服务

例子:

vi  /etc/systemd/system/myshellscript.service
[Unit]
Description = SH Script
After=network-online.target
Wants=network-online.target systemd-networkd-wait-online.service
StartLimitIntervalSec=500
StartLimitBurst=5


[Service]
Restart=on-failure
RestartSec=5s
ExecStart = sh /resource_path/myshellscript.sh



[Install]
WantedBy=multi-user.target

02.创建命令后执行以下命令

systemctl daemon-reload
systemctl enable myshellscript.service
systemctl restart myshellscript.service

推荐阅读