0

I'm trying to setup a systemd unit that should restart a bash script if it fails.

[Unit]
Description=Bash script Service

[Service]
Type=simple
Restart=always
RestartSec=1
ExecStart=/bin/bash -c '/simple/bash/script.sh'

But from systemctl status the_bash_script.service it seems that it is started with multiple instances:

● the_bash_script.service - Bash script Service
   Loaded: loaded (/etc/systemd/system/the_bash_script.service; static; vendor preset: enabled)
   Active: active (running) since Thu 2016-11-03 18:16:53 CET; 3 years 6 months ago
 Main PID: 1766 (bash)
   CGroup: /system.slice/the_bash_script.service
           ├─1766 /bin/bash -c '/bin/bash -c '/simple/bash/script.sh'
           └─1778 /bin/bash -c '/bin/bash -c '/simple/bash/script.sh'

Why there are 2 pids 1766 and 1778? Can I allow only one instance at time?

If this is the content of the_bash_script.service I don't need thata multiple instance are run at the same time:

#!/bin/bash


while [ true ]; do
    cat /dev/virtual  | nc -v 192.168.1.1 5005    
    sleep 5s
done

exit
1
  • 2
    Please put in your question why you think that forking off another process is not a function of whatever your shell script does.
    – JdeBP
    Jun 3, 2020 at 15:25

1 Answer 1

1

Your ExecStart=/bin/bash -c '/simple/bash/script.sh' contains /bin/bash -c which starts a bash shell with command /simple/bash/script.sh and then again when the script starts executing /simple/bash/script.sh it will start another bash shell i think.

Try this in your service file :

ExecStart=/simple/bash/script.sh

Also add the below lines and try:

ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .