28

How could I know if my linux starts with systemd or whatever package?

2

5 Answers 5

36

I know this is an old question, but since I was just asking myself the same question - here are my 2ct.

Best solution I came up with

ps --no-headers -o comm 1

This returns either systemd or init and appears reliable across Linux distributions and releases.

file /sbin/init would work, with help of pattern matching. Output of ps 1 does not appear helpful since on some Linux distributions it will print 'init' (the symlink) despite systemd being used.

Debian 8

$ ps 1
  PID TTY      STAT   TIME COMMAND
    1 ?        Ss     0:02 /sbin/init
$ file /sbin/init
/sbin/init: symbolic link to /lib/systemd/systemd

RHEL 7

$ ps 1
  PID TTY      STAT   TIME COMMAND
    1 ?        Ss     7:46 /usr/lib/systemd/systemd --switched-root --system --deserialize 21
$ file /sbin/init
/sbin/init: symbolic link to `../lib/systemd/systemd'

SLES 12

$ ps 1
  PID TTY      STAT   TIME COMMAND
    1 ?        Ss     0:24 /usr/lib/systemd/systemd --switched-root --system --deserialize 21
$ file /sbin/init
/sbin/init: symbolic link to `../usr/lib/systemd/systemd'

openSUSE 13.1

$ ps 1
  PID TTY      STAT   TIME COMMAND
    1 ?        Ss     0:33 /sbin/init showopts
$ /sbin/init: symbolic link to `../usr/lib/systemd/systemd'
3
  • THIS is the best answer!
    – iconoclast
    Aug 18, 2020 at 18:01
  • 1
    So Microsoft is shipping Ubuntu as part of WSL 2. On it, the systemctl command isn't available, not DBus bindings of Systemd are available, but some of the solutions proposed here literally report "systemd". But even MS confirms it is not systemd they are using in their "Ubuntu". This solution actually reveals the difference. For WSL 2 Ubuntu it returns init, on real Ubuntu it returns "systemd". Thanks for the answer. Jan 8, 2022 at 20:10
  • ps --no-headers -o comm 1 doesn't work on embedded Linux builds using BusyBox as the executable implementation of ps. Here's the error: ps: unrecognized option '--no-headers' BusyBox v1.31.1 (2022-08-04 07:12:02 UTC) multi-call binary. Usage: ps [-o COL1,COL2=HEADER Aug 4, 2022 at 23:48
25

Check what process is running as PID 1. You can do this by running ps 1 and scrolling to the top. If you have some systemd thing running as PID 1, you have systemd running.

Alternatively, run systemctl to list running systemd units.

You might also want to check what /sbin/init is; file /sbin/init will tell you if it's a real executable or if it's a symbolic link to some other package's executable. On a systemd box, for example:

root@boxy / # file /sbin/init
/sbin/init: symbolic link to ../lib/systemd/systemd

For more information, check this out: https://en.wikipedia.org/wiki/Linux_startup_process

Another way of seeing exactly what you have on your system is typing man init and seeing which program's man page you end up on.

3
  • 2
    An easier way to see what's running with pid 1 is ps 1 (the number 1).
    – deltab
    Dec 26, 2015 at 1:58
  • @deltab Thanks for clearing that up! I'll edit it into the answer.
    – ecube
    Dec 26, 2015 at 2:28
  • Great guys(@deltab,@dma1324)!!! My ps 1 shows /sbin/init, and my file /sbin/init shows a binary, so I think it's not systemd. Then I have tried to run the systemctl command and get the "command not found" error, so now I'm sure I have not systemd on my linux.
    – Lucho
    Dec 26, 2015 at 8:19
8

The correct solution is to check the presence of /run/systemd/system directory.

[[ -d /run/systemd/system ]] && echo "using systemd" 

This method is used by systemd's own library function sd_booted(): https://www.freedesktop.org/software/systemd/man/sd_booted.html

3
  • 1
    Using '-d' instead of '-x' would be a bit more accurate. Prevents potential confusion about an executable file vs a directory.
    – dbernard
    Mar 18, 2022 at 12:37
  • 1
    Updated my answer to use -d. I originally intended to use -e but got the options mixed up. These shouldn't make any difference in practice.
    – intgr
    Mar 21, 2022 at 12:12
  • I prefer this instead: [ -d /run/systemd/system ] && echo "using systemd" || echo "NOT using systemd" Aug 4, 2022 at 23:52
0

Best answer I found so far was to ask the package manager of your distro which package installed the /sbin/init file. For example, on debian-based, that would be

dpkg -S /sbin/init

If /sbin/init doesn't exist on your system, you can look for what program as pid 1 instead with ps 1.

-1

@Trevor-Boyd-Smith linked to this discussion on Unix Stackexchange based on which I would like to offer:

[[ `systemctl` =~ -\.mount ]] || { echo 'Systemd not found'; exit 1; }

This bash statement will either just execute or print the message and exit the running script.

1
  • Thanks for the downvote. But this works for me. Mar 7, 2021 at 6:20

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.