MacOS – How to remove a service from launchd added by mistake

homebrewlaunchdmacos

By mistake I added an invalid (non-existing) service to launchd:

sudo launchctl enable system/homebew.mxcl.cntlm

The above should be system/homebrew.mxcl.cntlm instead.

However, after removing the service:

sudo launchctl remove system/homebew.mxcl.cntlm

it still shows in the list of disabled services:

nlykkei-mbp:~ nlykkei$ launchctl print-disabled system
disabled services = {
    "org.apache.http" => false
    "homebrew.mxcl.dnsmasq" => false
    "com.apple.CSCSupportd" => true
    "com.apple.ftpd" => true
    "com.apple.mdmclient.daemon.runatboot" => true
    "com.openssh.sshd" => false
    "org.apache.httpd" => true
    "com.apple.bootpd" => true
    "homebrew.mxcl.cntlm" => false
    "org.macosforge.xquartz.privileged_startx" => false
    "com.launchd.maxfiles" => false
    "com.apple.ftp-proxy" => true
    "homebew.mxcl.cntlm" => false     <-- doesn't exist
}

How can I remove all traces of homebew.mxcl.cntlm from my system, so that it doesn't show up using launchctl?

Best Answer

Immediately after removing the service you won't be able to find the service name in a distinctive and editable file. It's probably hidden in some cache.

After a reboot you should be able to remove its traces by performing these steps though:

  1. Search for the name of the non-existent service to remove in the files of this directory: /var/db/com.apple.xpc.launchd

    sudo grep -rnw '/var/db/com.apple.xpc.launchd/' -e "homebew"
    
  2. Edit the file found by grep with nano (the string is probably found in disabled.plist)

    sudo nano /var/db/com.apple.xpc.launchd/disabled.plist
    

    Move the cursor to the two incriminating lines (service name e.g. <key>homebew.mxcl.cntlm</key> followed by its status e.g. <false/>) and remove each with ctrlK. Hit ctrlO, and ctrlX, to save the changes to disk and leave nano.

  3. Reboot again and launchctl print-disabled system won't show the service anymore.

Due to an agressive caching mechanism or other opaque system procedures, removing the lines doesn't work reliably with vim or nano (on the OP's system it didn't work at all, in my Catalina VM 5 out of 6 attempts have been successful).

Then the file has to be cleaned in Recovery Mode:

  1. Boot to Recovery Mode
  2. In the menubar open Terminal
  3. Edit the file with nano. The paths depend on your main volume name (e.g. Macintosh HD):

    /Volumes/Macintosh\ HD/usr/bin/nano /Volumes/Macintosh\ HD//var/db/com.apple.xpc.launchd/disabled.plist
    
  4. Enter reboot to boot normally.