运行Ubuntu 17.04,我希望有一个systemctl
服务来监督主bash
脚本,其中有三个程序(这里用虚拟脚本foo_script
替换为参数标记)在无限循环下运行(因为程序可能崩溃)。
如果从命令行调用,主脚本foo_main.sh
可以正常工作;但是我试图建立的服务很快崩溃了。
档案foo_script.sh
:
#!/bin/bash
echo "FooScripting "$1 >> "foo.d/"$1
档案loop.sh
:
#!/bin/bash
nLoop=0
prgName=$1
prgArg=$2
echo "<< START of "${prgName} ${prgArg}" loop >>"
while :
do
let nLoop=nLoop+1
echo "<< looping "${prgName} ${prgArg}" >>" ${nLoop}
"./"${prgName} ${prgArg}
sleep 1
done
echo "<< END of "${prgName} ${prgArg}" loop >>"
档案foo_main.sh
:
#!/bin/bash
echo "foo_main start in "${PWD}
./loop.sh "foo_script.sh" "fb" &
sleep 2
./loop.sh "foo_script.sh" "gc" &
./loop.sh "foo_script.sh" "gb" &
echo "foo_main end"
档案/etc/systemd/system/food.service
:
[Unit]
Description = Foo Daemon
After = network.target
[Service]
Type = simple
# User = <<USER>>
# PIDFile=/var/food.pid
WorkingDirectory = /home/john/bin
ExecStart = /home/john/bin/foo_main.sh
# ExecStop = killall loop.sh
# ExecReload = killall loop.sh && /home/john/bin/foo_main.sh
# Restart = on-abort
[Install]
WantedBy = multi-user.target
我从每个sudo systemctl status food.service
获得的内容(在start
ofc之后)几乎都是相同的输出
●food.service - Foo Daemon
已加载:已加载(/etc/systemd/system/food.service;已禁用;供应商预设:已启用)
活跃:无效(死亡)
Sep 28 14:54:30 john-host systemd [1]:启动Foo Daemon。
Sep 28 14:54:30 john-host foo_main.sh [7376]:foo_main脚本在/ home / john / bin中启动
Sep 28 14:54:30 john-host foo_main.sh [7376]:&lt;&lt;启动foo_script.sh fb loop&gt;&gt;
Sep 28 14:54:30 john-host foo_main.sh [7376]:&lt;&lt;循环foo_script.sh fb&gt;&gt; 1
Sep 28 14:54:31 john-host foo_main.sh [7376]:&lt;&lt;循环foo_script.sh fb&gt;&gt; 2
Sep 28 14:54:32 john-host foo_main.sh [7376]:foo_main script end
Sep 28 15:24:30 john-host foo_main.sh [7921]:&lt;&lt;启动foo_script.sh gb loop&gt;&gt;
Sep 28 15:24:30 john-host foo_main.sh [7921]:&lt;&lt;启动foo_script.sh gc loop&gt;&gt;
Sep 28 15:24:30 john-host foo_main.sh [7921]:&lt;&lt;循环foo_script.sh gb&gt;&gt; 1
Sep 28 15:24:30 john-host foo_main.sh [7921]:&lt;&lt;循环foo_script.sh gc&gt;&gt; 1
答案 0 :(得分:1)
另一种解决方案是在Type=oneshot
RemainAfterExit=yes
+ /etc/systemd/system/food.service
请https://www.freedesktop.org/software/systemd/man/systemd.service.html查看 onshot 类型的服务。
以下服务文件也可以解决您的问题。
[Unit]
Description = Foo Daemon
After = network.target
[Service]
Type = oneshot
RemainAfterExit=yes
# User = <<USER>>
# PIDFile=/var/food.pid
WorkingDirectory = /home/john/bin
ExecStart = /home/john/bin/foo_main.sh
# ExecStop = killall loop.sh
# ExecReload = killall loop.sh && /home/john/bin/foo_main.sh
# Restart = on-abort
[Install]
WantedBy = multi-user.target
答案 1 :(得分:0)
已解决...服务已停止,因为其执行流程以foo_main.sh
结束。添加像
# ...
./loop.sh "foo_script.sh" "endless_dummy_loop"
没有背景&符号,位于foo_main.sh
的末尾。
显然,真正的服务与此截然不同,但我现在明白了这一点。