I am writing a shell script to check if mongod server is running and if not, to start it. To acheive that I am using the below command:

systemctl is-active --quiet mongod

This does the job as expected. But it's possible my script will run on systems that do not have systemd, so I am interested in knowing whether there is a more portable way to do this.

  • Can you assume the name of the service will be the same? I mean, will the service always be named mongod on all systems?
    – terdon
    Mar 1, 2019 at 12:55
  • @terdon Yes that we can assume. Mar 1, 2019 at 14:16
  • 1
    If the ultimate goal is to ensure the service is running, you could start it without checking — sensible service managers will only start a service if it’s not already running. Mar 1, 2019 at 16:43
  • @Stephen Hmm that's a good point. I'll check if mongod also handle that internally. Mar 1, 2019 at 16:52

2 Answers 2


This is not perfect, but will work on many systems. Note that the pgrep approach is suboptimal since the name might be slightly different, or you might pass a name that is a substring of another name, but it's a start.


if systemctl is-active --quiet "$1"  > /dev/null 2>&1; then
  echo "Running! (systemctl)"
elif service "$1" status  2>&1 | grep -Eq 'is running|: active (running)'; then
  echo "Running! (service)"
elif pgrep "$1" > /dev/null 2>&1 ; then
  echo "Running (pgrep)"
  echo "Not running"

Save that as checkIfRunning and run it like this:

checkIfRunning mongod
  • Oh this looks clean approach. But still we are parsing the output right ? Do you think it's not a problem ? Mar 1, 2019 at 15:31
  • @TheLittleNaruto no, I think it is absolutely a problem :) But I can't think of any way to avoid that if you need to go for portability.
    – terdon
    Mar 1, 2019 at 16:47
  • Hmm thought same. :) What do you think about the way I mentioned in my question ? Is it better than your approach ? Mar 1, 2019 at 18:27
  • @TheLittleNaruto what do you mean? I do the exact same thing as you have in your question here. The others are only executed if the first one fails.
    – terdon
    Mar 1, 2019 at 18:29
  • 1
    @Zanna d'oh! Of course we shouldn't redirect both streams. Total PEBKAC there, thanks. I got carried away by the others. And good point about the non-systemd service output, thanks!
    – terdon
    Mar 2, 2019 at 14:06

There is no one, portable way of achieving that. But many current Linux distributions have old, legacy service command that behaves as a proxy to systemctl if run on systemd powered OS.

So you can run the command like this:

# service sshd status
Redirecting to /bin/systemctl status sshd.service
● sshd.service - OpenSSH server daemon
   Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
   Active: active (running) since Wed 2019-02-20 12:16:17 UTC; 1 weeks 2 days ago
     Docs: man:sshd(8)
 Main PID: 3388 (sshd)
   CGroup: /system.slice/sshd.service
           └─3388 /usr/sbin/sshd -D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.