Discussion:
Back trace systemd service unit by pid
(too old to reply)
Kevin Hsu
2017-12-21 05:48:29 UTC
Permalink
Hi folks,

I am looking for a good way to backtrace systemd service by a process id.
The "systemctl status <pid>" command is available to do the magic. It will
show a full status of the service that creates the
pid. But this command gives too many details. The only thing I need is the
service unit name. Is there any simple way instead of parsing results from
"systemctl status" ?

For example like
systemctl service-get 5566
nginx.service
Thanks,
Kevin Hsu
aleivag
2017-12-21 07:21:39 UTC
Permalink
the simplest way is to use busctl as

***@algx:~$ busctl call org.freedesktop.systemd1
/org/freedesktop/systemd1 org.freedesktop.systemd1.Manager GetUnitByPID u
28729
o "/org/freedesktop/systemd1/unit/nginx_2eservice"

from that you are a awk away of what you want. if you want to get clever
you can always do:

***@algx:~$ busctl call org.freedesktop.systemd1
/org/freedesktop/systemd1 org.freedesktop.systemd1.Manager GetUnitByPID u
28729 | awk '{print $2}' | xargs -i busctl get-property
org.freedesktop.systemd1 {} org.freedesktop.systemd1.Unit Id

s "nginx.service"

But depending on what you want, if you are not bound to use a shell, you
can try using sd-bus.h in you code.



Alvaro Leiva
Post by Kevin Hsu
Hi folks,
I am looking for a good way to backtrace systemd service by a process id.
The "systemctl status <pid>" command is available to do the magic. It will
show a full status of the service that creates the
pid. But this command gives too many details. The only thing I need is the
service unit name. Is there any simple way instead of parsing results from
"systemctl status" ?
For example like
systemctl service-get 5566
nginx.service
Thanks,
Kevin Hsu
_______________________________________________
systemd-devel mailing list
https://lists.freedesktop.org/mailman/listinfo/systemd-devel
Lennart Poettering
2017-12-21 10:08:47 UTC
Permalink
Post by Kevin Hsu
Hi folks,
I am looking for a good way to backtrace systemd service by a process id.
The "systemctl status <pid>" command is available to do the magic. It will
show a full status of the service that creates the
pid. But this command gives too many details. The only thing I need is the
service unit name. Is there any simple way instead of parsing results from
"systemctl status" ?
For example like
systemctl service-get 5566
nginx.service
The normal "ps" tool can do that these days:

$ ps hp 5566 -o unit

Lennart
--
Lennart Poettering, Red Hat
Tomasz Torcz 👁️
2017-12-21 15:05:34 UTC
Permalink
Post by Kevin Hsu
Hi folks,
I am looking for a good way to backtrace systemd service by a process id.
The "systemctl status <pid>" command is available to do the magic. It will
show a full status of the service that creates the
pid. But this command gives too many details. The only thing I need is the
service unit name. Is there any simple way instead of parsing results from
"systemctl status" ?
For example like
systemctl service-get 5566
nginx.service
Look into /proc/<pid>/cgroup
--
Tomasz Torcz Once you've read the dictionary,
xmpp: ***@chrome.pl every other book is just a remix.
Kevin Hsu
2017-12-22 09:56:52 UTC
Permalink
Hi folks,

First of all thanks for all good answers. After looking deep how the "ps"
command gets the unit name, I found out that it uses function
"sd_pid_get_unit" from
systemd/sd-login.h in libsystemd.so. So I decide to use it as well. Thanks
a lot.

Best wishes,
Kevin
Post by Tomasz Torcz 👁️
Post by Kevin Hsu
Hi folks,
I am looking for a good way to backtrace systemd service by a process id.
The "systemctl status <pid>" command is available to do the magic. It
will
Post by Kevin Hsu
show a full status of the service that creates the
pid. But this command gives too many details. The only thing I need is
the
Post by Kevin Hsu
service unit name. Is there any simple way instead of parsing results
from
Post by Kevin Hsu
"systemctl status" ?
For example like
systemctl service-get 5566
nginx.service
Look into /proc/<pid>/cgroup
--
Tomasz Torcz Once you've read the dictionary,
_______________________________________________
systemd-devel mailing list
https://lists.freedesktop.org/mailman/listinfo/systemd-devel
Loading...