I am defining common bash files which I want to use across different distributions. I need a way to check if system is using systemd or sysvinit (/etc/init.d/). I need this so I run appropriate command to start the service. What would be safe way to check for this? I currently check for existance of systemctl command, but is that really an option as there might be the case where systemctl command might be available, but it wouldn't necessarily mean that systemd is actually used?

Here is an excerpt from my current bash script:

if [ command -v systemctl >/dev/null ]
    systemctl service start
    /etc/init.d/service start

marked as duplicate by jasonwryan, Gilles, slm, terdon, Faheem Mitha Mar 26 '14 at 21:29

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • That question was more related when creating an installer package and the general consensus on the answer was to implement it in a different way. I feel that my question provides a different case, which could provide a different solution/answer to this. I will keep it open for some time and delete it if it does not get any attention. – phoops Mar 26 '14 at 20:46
  • Unfortunately, there is no clean, surefire way of doing this. You should also have a look through this Q&A for some possible workarounds. – terdon Mar 26 '14 at 21:27
  • on rpm distros, rpm --quiet --query systemd. this avoids the hanky panky involved in looking for a process or pid or symlink. – Trevor Boyd Smith Jan 9 '18 at 19:08

Systemd and init have pid = 1

pidof /sbin/init && echo "sysvinit" || echo "other"

Check for systemd

pidof systemd && echo "systemd" || echo "other"

enter image description here

  • 5
    When will that not echo sysvinit? AFAIK, all (or most) init systems will have a PID of 1, that's kind of part of the definition. – terdon Mar 26 '14 at 21:23
  • 1
    Commenting to say that the check for systemd works on Ubuntu 14.04, 15.04 and 15.10, but not on debian jessie 8.2. – Jose Diaz-Gonzalez Jan 2 '16 at 9:00
  • 21
    First solution is wrong because /sbin/init is probably symbolic link to /lib/systemd/systemd. To check it run file /sbin/init. – patryk.beza Aug 8 '16 at 9:57
  • 7
    +1 for pstree -p, -1 for the rest of this answer. – Darren Cook Jan 17 '17 at 9:27
  • 4
    -1 for the misleading pidof init as it may be a symbolic link to systemd. +1 for the comments here that have more than sufficient answers to help who comes here. Try for example: [[ -L "/sbin/init" ]] && echo 'systemd' || echo 'systemV' – Dr Beco Mar 27 '17 at 13:25

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