Discussion:
Back trace systemd service unit by pid
(too old to reply)
Kevin Hsu
2017-12-21 05:48:29 UTC
Permalink
Raw Message
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
Raw Message
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

On Wed, Dec 20, 2017 at 9:48 PM, Kevin Hsu <***@gmail.com> wrote:

> 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
> systemd-***@lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/systemd-devel
>
>
Lennart Poettering
2017-12-21 10:08:47 UTC
Permalink
Raw Message
On Do, 21.12.17 13:48, Kevin Hsu (***@gmail.com) wrote:

> 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
Raw Message
On Thu, Dec 21, 2017 at 01:48:29PM +0800, Kevin Hsu wrote:
> 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
Raw Message
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

2017-12-21 23:05 GMT+08:00 Tomasz Torcz 👁 <***@pipebreaker.pl>:

> On Thu, Dec 21, 2017 at 01:48:29PM +0800, Kevin Hsu wrote:
> > 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.
> _______________________________________________
> systemd-devel mailing list
> systemd-***@lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/systemd-devel
>
Loading...