创建守护进程来运行 .sh

创建守护进程来运行 .sh

我已经尝试了一段时间,但一直无法创建运行脚本的服务。我尝试运行的脚本如下。

#!/bin/sh
while true

do

sudo bbb-record --rebuildall

sleep 300
done

我尝试创建一个守护进程来运行它,如下所示:

[Unit]
Description=bbb-rebuild
[Service]
ExecStart=/home/sysadmin/rebuild
Restart=always

[Install]
WantedBy=multi-user.target 

exit=0 

我收到错误消息

==== AUTHENTICATION COMPLETE ===
Failed to start rebuild.service: Unit rebuild.service is not loaded properly: Invalid argument.
See system logs and 'systemctl status rebuild.service' for details.

after checking status
Apr 22 16:46:55 bbb2 systemd[1]: rebuild.service: Unit entered failed state.
Apr 22 16:46:55 bbb2 systemd[1]: rebuild.service: Failed with result 'exit-code'.
Apr 22 16:46:55 bbb2 systemd[1]: rebuild.service: Service hold-off time over, scheduling restart.
Apr 22 16:46:55 bbb2 systemd[1]: Stopped bbb-rebuild.
Apr 22 16:46:55 bbb2 systemd[1]: rebuild.service: Start request repeated too quickly.
Apr 22 16:46:55 bbb2 systemd[1]: Failed to start bbb-rebuild.
Apr 22 16:46:55 bbb2 systemd[1]: rebuild.service: Unit entered failed state.
Apr 22 16:46:55 bbb2 systemd[1]: rebuild.service: Failed with result 'start-limit-hit'.
Apr 22 16:47:22 bbb2 systemd[1]: [/etc/systemd/system/rebuild.service:12] Missing '='.

答案1

你最好使用systemd-定时器为了达成这个。

创建一个名为“systemd 服务”的计时器rebuild.timer

# nano /etc/systemd/system/rebuild.timer

[Unit]
Description=Start script every hour

[Timer]
OnCalendar=hourly

[Install]
WantedBy=timers.target

编辑脚本如下

#!/bin/sh
bbb-record --rebuildall

确保脚本可运行chmod +x /home/sysadmin/rebuild

编辑你的 systemd 服务以删除restart=alwaysexit 0

[Unit]
Description=bbb-rebuild

[Service]
ExecStart=/home/sysadmin/rebuild

[Install]
WantedBy=multi-user.target 

然后只需使用 启用你的 systemd-timer 即可systemctl enable rebuild.timer
然后使用 检查计时器是否已启用systemctl status rebuild.timer

相关内容