我编写了一个 bash 脚本,希望每 60 秒运行一次(不用担心,我已经试过该脚本,它运行完美)。
现在我想实现它systemd .service
文件,但到目前为止它还没有重新启动。脚本正在执行一时间并以返回代码 0 结束,然后服务停止(永远)。所以,它只运行一次,然后就结束了。
它不会重启RemainAfterExit
。我尝试了、StartLimitIntervalSec
或 等多种不同的方法StartLimitBurst
,但都不起作用。
这是我的.service文件:
[Unit]
Description=ExampleScript
[Service]
User=myuser
Restart=allways
RestartSec=60s
ExecStart=/bin/bash /home/myuser/start.sh
StartLimitIntervalSec=0
[Install]
WantedBy=multi-user.target
也许你们中有人了解 System.d 并能帮助我?非常感谢!
附言:
运行脚本时的服务状态:
* examplescript.service - ExampleScript
Loaded: loaded (/usr/lib/systemd/system/examplescript.service; enabled; vendor preset: disabled)
Active: active (running) since Fri 2018-06-29 23:58:04 CEST; 1s ago
Main PID: 31860 (bash)
Memory: 15.6M
CGroup: /system.slice/examplescript.service
|-31860 /bin/bash /home/myuser/start.sh
|-31862 /bin/bash /home/myuser/script/subscript.sh
`-31871 python /home/myuser/script/subscript.py
Jun 29 23:58:04 mysystem systemd[1]: Started ExampleScript.
脚本完成后的服务状态为 rc0:
* examplescript.service - ExampleScript
Loaded: loaded (/usr/lib/systemd/system/examplescript.service; enabled; vendor preset: disabled)
Active: inactive (dead) since Fri 2018-06-29 23:58:48 CEST; 2min 42s ago
Process: 31860 ExecStart=/bin/bash /home/myuser/start.sh (code=exited, status=0/SUCCESS)
Main PID: 31860 (code=exited, status=0/SUCCESS)
Jun 29 23:58:04 mysystem systemd[1]: Started ExampleScript.
...systemd 太复杂了,cron-job 简单多了
答案1
[Service]
User=myuser
Restart=allways # << fix that
RestartSec=60s
ExecStart=/bin/bash /home/myuser/start.sh
StartLimitIntervalSec=0
您在“Restart=”上拼错了“always”。