How to get service PID using systemctl

Use systemctl to get process identifier for specified service.

systemd version.

$ systemctl --version
systemd 247 (247.3-1)

Inspect ssh service status.

$ systemctl status ssh
● ssh.service - OpenBSD Secure Shell server
     Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2021-03-14 13:48:08 UTC; 1h 30min ago
       Docs: man:sshd(8)
    Process: 380 ExecStartPre=/usr/sbin/sshd -t (code=exited, status=0/SUCCESS)
   Main PID: 384 (sshd)
      Tasks: 1 (limit: 526)
     Memory: 6.1M
        CPU: 118ms
     CGroup: /system.slice/ssh.service
             └─384 sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups

Warning: some journal files were not opened due to insufficient permissions.

Display process identifier for ssh service (property and value).

$ systemctl show --property MainPID ssh

Display process identifier for ssh service (only value).

vagrant@bullseye:~$ systemctl show --property MainPID --value ssh

Inspect found PID.

$ ps -f --pid 384
UID          PID    PPID  C STIME TTY          TIME CMD
root         384       1  0 13:48 ?        00:00:00 sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups