Systemd - stop service not forking (but start does)

Hi,

Does anyone know why forking within a shell script when stopping service doesn’t execute on systemctl stop but does on systemctl start?

Tested on:
openSUSE Leap 15.5 - systemd 249

Init Script:
#!/bin/bash
case “$1” in
‘start’)
echo ‘Starting Test Service’ >> /tmp/test 2>&1 &
;;
‘stop’)
echo ‘Stopping Test Service’ >> /tmp/test 2>&1 &
;;
esac

Systemd config:
[Unit]
Documentation=Start and test sample service

[Service]
Type=forking
ExecStart=/etc/init.d/test start
ExecStop=/etc/init.d/test stop

Result:

systemctl start test

systemctl stop test

systemctl start test

cat /tmp/test

Starting Test Service
Starting Test Service

If I removed the fork from the stop init.d script. Stop command from systemctl works

Also running, directly /etc/init.d/test stop will work as expected.

Workaround:
#!/bin/bash
case “$1” in
‘start’)
echo ‘Starting Test Service’ >> /tmp/test 2>&1 &
;;
‘stop’)
echo ‘Stopping Test Service’ >> /tmp/test 2>&1 &
;;
esac

Any ideas what the root cause here would be appreciated. I have tested this on Ubuntu also and I don’t get the same issue.

Thanks,
Stan.

You didn’t describe what test is doing.
The man page discourages the use of type=forking.

Did your test program call fork()?
I think that systemd cannot identify the process to stop.

Regards
Philippe

Your post is unreadable, and your code cannot be copy-pasted because editor replaced normal ASCII quoting with “better” Unicode variants. Always post computer text as preformatted (</> button in editor).

This is wrong, Documentation should be URL, not free text.

It is rather unclear what you mean and your code in “workaround” is identical.

Your service terminates immediately when the echo in start completes, so when you do systemctl stop test from the systemd point of view this service is already inactive and there is nothing to do. ExecStop is only executed when the service is active.

Hello,

Sorry for the formatting. Perhaps I should reword my question.

Why does STOP not work in systemd for background commands but for start it does work:

Test Init Script: (start works, stop doesn’t work)

#!/bin/bash
case “$1” in
   ‘start’)
     echo ‘Starting Test Service’ >> /tmp/test 2>&1 &
     ;;
  ‘stop’)
     echo ‘Stopping Test Service’ >> /tmp/test 2>&1 &
     ;;
esac

Working Init Script (not putting the stop script into the background &) - (start works but stop doesn’t):

#!/bin/bash
case “$1” in
   ‘start’)
     echo ‘Starting Test Service’ >> /tmp/test 2>&1 &
     ;;
  ‘stop’)
     echo ‘Stopping Test Service’ >> /tmp/test 2>&1 
     ;;
esac

Hopefully this question is cleaer.

Sorry, no.

Anyway, I cannot reproduce it. You may want to enable systemd debug level logs or even strace it to see what it really does.

Hello, thanks everyone for these responses and suggestions.

I think I have reproduced the issue to only occur when I substitute the user “su”:

From the init script:

  'stop')
      strace -o /tmp/test_su su root-c "echo '6. Stop Test - With Redirect and SU'  >> /tmp/log 2>&1" &

This line / command doesn’t run as even strace doesn’t create a log.

If I fork the command within “su” then it works:

   strace -o /tmp/test_su su root-c "echo '6. Stop Test - With Redirect and SU'  >> /tmp/log 2>&1 &" 

Any ideas what could cause that, not that it’s an issue. Just interested in why.

If you copied and pasted your command lines, both of them cannot work. You invoke shell of user root-c and pass it the argument echo '6. Stop Test - With Redirect and SU' >> /tmp/log 2>&1 which will not be interpreted as command line.

If you did not copy and paste, further discussion is pointless, as we do not know what you are doing.

1 Like

Hi, applogies - I’m not sure what happened, here is paste:

    strace -o /tmp/test_su su root -c "echo '8. Stop Test - as root'  >> /tmp/log 2>&1 &" # works
     strace -o /tmp/test_su su root -c "echo '9. Stop Test - as root'  >> /tmp/log 2>&1" & # not working