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