Tell if a process is under systemd, init.d or Upstart control

1

1

Is it possible to reliable tell for a given Linux process if it was launched or is still supervised by an init.d script, Upstart or systemd ?

dronus

Posted 2015-05-16T11:04:32.117

Reputation: 1 482

1You should be able to tell from its parent hierarchy. The easiest way is to use a system monitor in tree view. On Ubuntu 15.04 I find ksysguard the easiest to use, though you can also use gnome-system-monitor. If your Linux doesn't have these programs available, you can use ps -eHl|less and follow the tree from the program you're interested in. – AFH – 2015-05-16T14:30:25.027

I don't think so. Looking at pstree at my system, most of the processes seem to have detached from their parent. There are only a few processes started by my desktop session, so most of the processes in the topmost level must have been started by some init deamon I guess. – dronus – 2015-05-16T15:41:05.627

My pstree shows everything deriving from systemd (PID 1), including upstart, which is well down the list. I get the same from ps -eHl. – AFH – 2015-05-16T16:39:21.610

Because systemd has PID 1 (the topmost one), which is the only allowed top level process. This behavior of systemd is often criticised. So any process released from it's parent is parented by systemd. If you use Upstart or init.d scripts on a systemd-free system, you see that most processes are just below init, despite the fact most of them where launched by Upstart or init.d scripts. On your system sporting systemd, you will see any process released by its parent below systemd, so you cannot tell who has launched the process in the first place that way. – dronus – 2015-05-16T18:31:56.743

Sorry, I didn't know about this behaviour. Since most processes on 15.04 have systemd as progenitor, this seems in keeping with what I had read, that systemd mostly replaced upstart in 15.04. By the way, just as an aside, I seem to have two top-level processes: because I run some KDE processes, there is also kthreadd (PID 2), also with no parent, but with a lot of children. – AFH – 2015-05-16T20:03:25.467

Answers

2

With systemd, you can find which systemd service a process belongs to by looking at the cgroup it is in.

For example:

$ ps -eo pid,comm,cgroup | grep dhclient
6476 dhclient 8:devices:/system.slice NetworkManager.service,1:name=systemd:/system.slice/NetworkManager.service

will tell you that the dhclient process is part of the NetworkManager.service, and is thus managed by systemd.

Whereas:

$ ps -eo pid,comm,cgroup | grep firefox
3302 firefox 8:devices:/user.slice,1:name=systemd:/user.slice/user-1000.slice/session-c2.scope

shows that this Firefox process is part of the session launched by the user with the UID 1000.

Processes launched by initscripts or Upstart jobs are indistinguishable from other manually launched background processes.

Siosm

Posted 2015-05-16T11:04:32.117

Reputation: 396