0

When I do systemctl stop service on a stopped or failed service, it returns 0 and no output. I need to know whether it was running before I call this.

Calling systemctl status before stopping is not an option: it increases running time and introduces race conditions. Both reasons are important in my case.

2
  • 1
    I'm confused by your requirements; stop stops a service and status would tell you "whether it was running". Are you looking for a "stop only if it's already running" command? Or do you want "stop" to return different exit codes if the service was running and was successfully stopped? (In which case this may turn into a feature request of systemd)
    – Jeff Schaller
    Jan 20, 2020 at 13:14
  • I want stop to tell me what it has really done. A special exit code would be the best solution. Although a message in the output is acceptable too. Jan 20, 2020 at 17:39

1 Answer 1

3

If killing your service with a TERM signal is an appropriate way to stop it, you can use

systemctl kill --fail service

instead of

systemctl stop service

This will exit with code 0 if the service was running and killed, 1 if the service wasn’t running.

1
  • This might be a nice workaround. I'll check it! Jan 20, 2020 at 16:57

You must log in to answer this question.

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