Linux 服务意外终止

Linux Service unexpectedly died

运行 Ubuntu 17.04,我想要一个 systemctl 服务来监督主 bash 脚本,其中三个程序(此处由虚拟脚本代替) foo_script 用参数标记)运行 在无限循环中(因为可能的程序崩溃)。

主脚本 foo_main.sh 如果从命令行调用则可以正常工作;但是我尝试从中设置的服务很快就会崩溃。

文件foo_script.sh:

#!/bin/bash

echo "FooScripting " >> "foo.d/"

文件loop.sh:

#!/bin/bash

nLoop=0
prgName=
prgArg=

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

Loaded: loaded (/etc/systemd/system/food.service; disabled; vendor preset: enabled)

Active: inactive (dead)

Sep 28 14:54:30 john-host systemd[1]: Started Foo Daemon.

Sep 28 14:54:30 john-host foo_main.sh[7376]: foo_main script start in /home/john/bin

Sep 28 14:54:30 john-host foo_main.sh[7376]: << START of foo_script.sh fb loop >>

Sep 28 14:54:30 john-host foo_main.sh[7376]: << looping foo_script.sh fb >> 1

Sep 28 14:54:31 john-host foo_main.sh[7376]: << looping foo_script.sh fb >> 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]: << START of foo_script.sh gb loop >>

Sep 28 15:24:30 john-host foo_main.sh[7921]: << START of foo_script.sh gc loop >>

Sep 28 15:24:30 john-host foo_main.sh[7921]: << looping foo_script.sh gb >> 1

Sep 28 15:24:30 john-host foo_main.sh[7921]: << looping foo_script.sh gc >> 1

已解决...服务被停止只是因为它的执行流程以 foo_main.sh 结束。添加类似

的内容就足够了
# ...
./loop.sh "foo_script.sh" "endless_dummy_loop"

没有背景符号,在foo_main.sh.

结尾

显然实际服务与此大不相同,但我现在明白了。

另一个解决方案是在 /etc/systemd/system/food.service

中使用 Type=oneshot + RemainAfterExit=yes

查看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