brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: systemctl status with a PID tells you which systemd unit owns a process

Systemd
~/tips/tip-systemd-status-pid

systemctl status 4211

Find out which service or session started a process you spotted in top.

When some process is eating CPU in top, I want to know which service it belongs to, so I know what to restart or blame. systemd keeps track of that.

Pass the PID to systemctl status:

systemctl status 4211

The man page says a PID argument shows the unit containing that PID. You get the full status block of that unit: its name, whether it is active, its process tree and the last log lines. That also works for child processes deep inside a service, because systemd tracks them through the cgroup.

systemctl status 1 shows init.scope - System and Service Manager, and the main PID of a service leads straight to that service.

If the answer is a scope like session-3.scope, the process was started from a login shell, so look for a user. A [email protected] means it belongs to that user’s own service manager.

For a quick one liner in scripts, read the cgroup instead:

cat /proc/4211/cgroup

The PID has to exist right now. Short lived processes may be gone by the time you type the command, and then systemctl just says it found no unit.

Further reading: systemctl man page

More Systemd tips [Service] sudo mkdir -p /etc/systemd/system/service.d [Service] printf 'hunter2' | sudo systemd-creds encrypt --name=dbpass – dbpass.cred systemd-analyze timespan 1h30min All Systemd tips (88) All topics

Link