Convenient way to check if system is using systemd or sysvinit in BASH?

95,132

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

Share:
95,132

Related videos on Youtube

phoops
Author by

phoops

Updated on November 26, 2022

Comments

  • phoops
    phoops over 1 year

    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:

    #!/bin/sh
    if [ command -v systemctl >/dev/null ]
    then
        systemctl service start
    else
        /etc/init.d/service start
    fi
    
    • Admin
      Admin about 10 years
      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.
    • Admin
      Admin about 10 years
      Unfortunately, there is no clean, surefire way of doing this. You should also have a look through this Q&A for some possible workarounds.
  • Admin
    Admin about 10 years
    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.
  • Admin
    Admin about 10 years
    well, than the other way will be correct?
  • Admin
    Admin about 10 years
    No, for example Ubuntu with upstart still reports /sbin/init. Have a look at @slm's answer in the linked dupe for more details.
  • Admin
    Admin about 10 years
    I meant if checking for systmed as showed last is ok?
  • Admin
    Admin about 10 years
    Ah, yes, that's better, downvote retracted.
  • Admin
    Admin over 8 years
    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.
  • Admin
    Admin about 8 years
    On my old Debian I get both "sysvinit" and "systemd"
  • Admin
    Admin over 7 years
    First solution is wrong because /sbin/init is probably symbolic link to /lib/systemd/systemd. To check it run file /sbin/init.
  • Admin
    Admin over 7 years
    +1 for pstree -p, -1 for the rest of this answer.
  • Admin
    Admin about 7 years
    -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'
  • Admin
    Admin over 6 years
    Actually this doesn't work with Ubuntu. with pidof systemd doesn't show the correct systemd id. Rather, it would be useful if you use the following instead: cat /proc/1/status|egrep -i 'name|state' because systemd replaces sysvinit with the id == 1 in the system process. Hope this helps
  • Admin
    Admin about 5 years
    The right way to determine whether systemd is managing the system is to check for the existence of /run/systemd/system.