0

How can I stop a service if and only if the service exists, meaning I wish to only get an error code if the service exists, runs but can't be stopped. I shall not get an error code when the service either does not exist, or when the server is already stopped or successfully stopped.

systemctl stop foobar.service || true will swallow any failed attempts to stop a running service, which is too aggressive. The objective is to ensure the service does not run and only error when it can't be stopped.

1 Answers1

3

What about

systemctl is-active foobar.service && systemctl stop foobar.service

That will try to stop the service only if it's currently 'active'.

Popup
  • 516