39

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

jww
  • 12,722
Lucho
  • 518

7 Answers7

50

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'
sborsky
  • 1,125
24

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.

ecube
  • 555
19

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

intgr
  • 323
6

Run systemd-notify --booted (or systemctl is-system-running --quiet) and check if it returns 0 or not.

From systemd-notify(1) man page:

   --booted
       Returns 0 if the system was booted up with systemd,
       non-zero otherwise. If this option is passed, no
       message is sent. This option is hence unrelated to
       the other options. For details about the semantics
       of this option, see sd_booted(3). An alternate way
       to check for this state is to call systemctl(1)
       with the is-system-running command. It will return
       "offline" if the system was not booted with
       systemd.

Update: 2024-09-25

systemctl is-system-running reports degraded and exits with 1 if the systemd is running but one or more units failed.

fumiyas
  • 191
-1

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.

ychaouche
  • 541
-1

The process ID of the first process on boot is PID 1.

So I use this command to get the comm field of the PID 1 :

ps -p 1 -o comm=

The -p option is used to specify the PID where here is that of the first process.

The -o option is used to output only the specified comm field which here is only the executable name.

The = sign at the end of the headername prevents the header line from behind displayed.

The man ps says :

$ man ps | sed -n '/^\s*comm\s*COMMAND/,/^$/ p'
       comm        COMMAND   command name (only the executable name).  Modifications to the command name will not be
                             shown.  A process marked <defunct> is partly dead, waiting to be fully destroyed by its
                             parent.  The output in this column may contain spaces.  (alias ucmd, ucomm).  See also the
                             args format keyword, the -f option, and the c option.
                             When specified last, this column will extend to the edge of the display.  If ps can not
                             determine display width, as when output is redirected (piped) into a file or another command,
                             the output width is undefined (it may be 80, unlimited, determined by the TERM variable, and
                             so on).  The COLUMNS environment variable or --cols option may be used to exactly determine
                             the width in this case.  The w or -w option may be also be used to adjust width.

SebMa
  • 2,035
-2

@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.