Linux: определить init систему – Systemd vs Upstart

Автор: | 04/05/2016
 

linux_logoПоиск по процессам

Первый вариант – выполнить ps -aux | [initname].

Например, Ubuntu 12 и 14 используют Upstart:

$ ps aux | grep upstart
root       354  0.0  0.1  19472   648 ?        S    16:31   0:00 upstart-udev-bridge --daemon
root       566  0.0  0.1  15256   636 ?        S    16:31   0:00 upstart-socket-bridge --daemon
root       906  0.0  0.1  15272   644 ?        S    16:31   0:00 upstart-file-bridge --daemon
vagrant   2326  0.0  0.1  10436   884 pts/0    S+   17:58   0:00 grep --color=auto upstart

А Ubuntu 16 – Systemd:

$ ps aux | grep systemd
root       236  0.0  0.5  27712  2800 ?        Ss   16:37   0:00 /lib/systemd/systemd-journald
root       276  0.0  0.8  44708  4352 ?        Ss   16:37   0:00 /lib/systemd/systemd-udevd
root       494  0.0  0.6  28624  3136 ?        Ss   16:37   0:00 /lib/systemd/systemd-logind
message+   525  0.0  0.7  42892  3740 ?        Ss   16:37   0:00 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation
vagrant   2297  0.0  0.9  45248  4524 ?        Ss   17:58   0:00 /lib/systemd/systemd --user
vagrant   2345  0.0  0.2  14200  1080 pts/0    S+   17:59   0:00 grep --color=auto systemd

Так же, как и CentOS 7:

$ ps aux | grep systemd
root       423  0.0  0.0  52224  2444 ?        Ss   кві22   0:00 /lib/systemd/systemd-udevd --daemon
root       523  0.0  0.0  43520  2764 ?        Ss   кві22   0:00 /lib/systemd/systemd-logind
root      2057  0.0  0.0 177996  1020 ?        Sl   кві22   0:00 /usr/lib/x86_64-linux-gnu/systemd-shim
setevoy  27775  0.0  0.0  13200  2372 pts/11   S+   21:00   0:00 grep systemd

А CentOS 6 – использует Upstart, но с процессом init:

$ ps aux | grep init
root         1  0.0  0.2  19232  1496 ?        Ss   16:37   0:02 /sbin/init
vagrant   2687  0.0  0.1 103308   928 pts/0    S+   18:00   0:00 grep init

Еще один способ проверить систему с помощью процессов – узнать имя процесса с PID == 1.

Ubuntu 12, 14:

 ps -p 1
  PID TTY          TIME CMD
    1 ?        00:00:00 init

Ubuntu 16:

$ ps -p 1
  PID TTY          TIME CMD
    1 ?        00:00:03 systemd

Поиск по файлам

Второй способ – это просто выполнить /sbin/init с опцией --version.

Ubuntu 12, 14 и CentOS 6 с Upstart вернут:

$ /sbin/init --version
init (upstart 1.12.1)
Copyright (C) 2006-2014 Canonical Ltd., 2011 Scott James Remnant

This is free software; see the source for copying conditions.  There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

А CentOS 7 и Ubuntu 16:

$ /sbin/init --version
/sbin/init: unrecognized option '--version'

Ещё один способ – проверить, чем является сам файл /sbin/init.

Например, на Ubuntu 12 и 14:

$ file /sbin/init
/sbin/init: ELF 64-bit LSB  shared object, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=7a4c688d009fc1f06ffc692f5f42ab09e68582b2, stripped

И Ubuntu 16:

$ file /sbin/init
/sbin/init: symbolic link to /lib/systemd/systemd

Следующий вариант, тоже с помощью файловой системы – проверить симлинк exe в каталоге /proc/1, где 1 – PID первого процесса.

Ubuntu 12, 14, CentOS 6:

$ sudo ls -l /proc/1/exe
lrwxrwxrwx 1 root root 0 May  3 16:31 /proc/1/exe -> /sbin/init

Ubuntu 16:

$ sudo ls -l /proc/1/exe 
lrwxrwxrwx 1 root root 0 May  3 18:13 /proc/1/exe -> /lib/systemd/systemd

И простой скрипт для определения системы:

$ cat getinit.sh 
#!/usr/bin/env bash

# init process is pid 1
INIT=`ls -l /proc/1/exe`
if [[ $INIT == *"upstart"* ]]; then
  SYSTEMINITDAEMON=upstart
elif [[ $INIT == *"systemd"* ]]; then
  SYSTEMINITDAEMON=systemd
elif [[ $INIT == *"/sbin/init"* ]]; then
  INIT=`/sbin/init --version`
  if [[ $INIT == *"upstart"* ]]; then
    SYSTEMINITDAEMON=upstart
  elif [[ $INIT == *"systemd"* ]]; then
    SYSTEMINITDAEMON=systemd
  fi
fi

if [ -z "$SYSTEMINITDAEMON" ]; then
  echo "WARNING: Unknown distribution, assuming defaults - this may fail." >&2
else
  echo "Init system discovered: $SYSTEMINITDAEMON"
fi

Выполнение на Ubuntu 12, 14 и CentOS 6:

$ sudo bash getinit.sh 
Init system discovered: upstart

Ubuntu 16:

$ sudo bash getinit.sh 
Init system discovered: systemd

Примеры взяты тут>>>.