Why can't this process terminate?

o

Just recently I ran into an issue where my network program couldn't bind to a port because it was already in use. I opened the activity monitor and realized it (named "server") was already running from previously, even though I had closed the window and it was supposedly terminated. Quit or Force Quit on the process do nothing. I even restarted the computer but it was still there! This hasn't happened with my program before. I even typed in the terminal kill 4517 and nothing happened. Help?


Solution 1:

OS X uses a process launching system called launchd which consolidates functions provided by Init scripts, crontab and more in *nix systems (see the Wikipedia article for a high level overview, and Apple’s Developer docs on launch daemons and agents for details). One of the abilities of launchd is to keep a process it launched alive, if so defined by its configuration file – in that case, the process will relaunch whenever it is terminated. Your problem with a process apparently persisting across reboots and manual termination sounds very much like a case of launchd initiated process with a keepAlive key.

launchd configuration files are in plist format and found in

  • ~/Library/LaunchAgents – agents for the current user account only
  • /Library/LaunchAgents and /Library/LaunchDaemons – agents and daemons for all user accounts
  • /System/Library/LaunchAgents and /System/Library/LaunchDaemons – system level agents and daemons

and are usually named in reverse domain notation (tld.domain.process.plist). Depending if the user account of server is yours or not (can’t say, as you have blanked it), you should look in one of the first two locations above for a likely plist (if you have Xcode installed, you can QuickLook them easily). If found, your server is indeed controlled by launchd. The correct procedure to stop it is to remove it from launchd’s process list through

launchctl unload <tld.domain.process>

which will unload and stop the process (note you omit the plist suffix).

There is also a GUI for handling launchd files, Peter Borg’s Lingon (make sure to get “Lingon”, not “Lingon 3”, which is a dumbed down version safe for vanilla use), which might be more convenient than manually rooting through the file locations.

Solution 2:

Give this a try.

    sudo kill -9 4517

Where 4517 is the PID of the process.

If the process comes back with a new PID, something relaunched it. See kopischke's answer for how to deal with that.