Patrizio Bertoni
Patrizio Bertoni

Reputation: 2712

Linux Service unexpectedly died

Running Ubuntu 17.04, I'd like to have a systemctl service which oversees a main bash script, where three programs (here substituted by dummy script foo_script tagged with an argument) run under an endless loop (because of possible program crashes).

The main script, foo_main.sh, works correctly if called from a command line; but the service I'm trying to set up from it crashes soon.

File foo_script.sh:

#!/bin/bash

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

File 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 >>"

File 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"

File /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

What I obtain from every sudo systemctl status food.service (after a start ofc) is almost the same output

● 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

Upvotes: 0

Views: 961

Answers (2)

MrCryo
MrCryo

Reputation: 681

Another solution is to use Type=oneshot + RemainAfterExit=yes in your /etc/systemd/system/food.service

Look at https://www.freedesktop.org/software/systemd/man/systemd.service.html to refer onshot type of service.

The service file like following should solve your issue too.

[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

Upvotes: 1

Patrizio Bertoni
Patrizio Bertoni

Reputation: 2712

Solved... the service was stopped simply because its execution flow ended with foo_main.sh. It was enough to add something like

# ...
./loop.sh "foo_script.sh" "endless_dummy_loop"

Without background ampersand, at the end of foo_main.sh.

Clearly real services are far different from this, but I got the point now.

Upvotes: 0

Related Questions