get-init-system.sh 602 Bytes
Newer Older
1 2 3 4 5
#!/usr/bin/env bash

# Based from the stackexchange answer
# http://unix.stackexchange.com/questions/18209/detect-init-system-using-the-shell

6 7 8
IS_UPSTART=$(/sbin/init --version 2> /dev/null)

if [[ ${IS_UPSTART} =~ upstart ]]; then
9 10 11
    echo "upstart";
elif [[ `systemctl` =~ -\.mount ]]; then
    echo "systemd";
12 13 14 15
# If on chroot the above alternative will not work.
# Try poking a few systemd directories instead
elif [[ -f /usr/lib/systemd || -f /lib/systemd/systemd ]]; then
    echo "systemd";
16 17 18 19 20
elif [[ -f /etc/init.d/cron && ! -h /etc/init.d/cron ]]; then
    echo "sysv-init";
else
    echo "?";
fi