How can I find out which service started a process so I can disable that service?

How do you link a process to an associated service and then disable that permanently?

I know

ps aux | less 

will give me the process name and port but I want to get the associated service (and even file location) so that I can disable it at boot if necessary and find out where the files are and whether I need to uninstall something.


Solution 1:

On Ubuntu 16.04 and newer (using systemd as init), you can use systemctl status <PID> (from this Unix & Linux post):

For service processes:

$ systemctl status 561
● sshd.service - OpenSSH Daemon
   Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: disabled)
   Active: active (running) since Wed 2017-01-04 15:38:01 JST; 7h ago
 Main PID: 561 (sshd)
    Tasks: 1 (limit: 4915)
   CGroup: /system.slice/sshd.service
           └─561 /usr/bin/sshd -D

For other things, it will probably be in a user session scope:

$ systemctl status $(pgrep chrome -n)
● session-c2.scope - Session c2 of user muru
   Loaded: loaded (/run/systemd/transient/session-c2.scope; transient; vendor preset: disabled)
Transient: yes
   Active: active (running) since Wed 2017-01-04 15:46:30 JST; 7h ago
    Tasks: 422
   CGroup: /user.slice/user-1000.slice/session-c2.scope

Another answer in that U&L post has a simpler command:

ps -o unit -p <PID>

Compare:

$ ps -o pid,unit -p $(pgrep chrome -n) 561 
  PID UNIT
  320 session-c2.scope
  561 sshd.service