How to know a specific launchd.plist file location?

我只是一个虾纸丫 提交于 2019-12-03 01:40:42

This issue comes up a lot and unfortunately locate and mdfind both don't show results from the appropriate directories on my system. I put the following function in my .bashrc so I could quickly search the directories where launchctl looks for plist files.

launchctlFind () {
    LaunchctlPATHS=( \
        ~/Library/LaunchAgents \
        /Library/LaunchAgents \
        /Library/LaunchDaemons \
        /System/Library/LaunchAgents \
        /System/Library/LaunchDaemons \
    )

    for curPATH in "${LaunchctlPATHS[@]}"
    do
        grep -r "$curPATH" -e "$1"
    done
    return 0;
}

Note that this only checks in the directories where launchctl looks for files at boot-up and login. It may not find everything because jobs can be manually loaded by the user and/or other processes.

UPDATE: For those running macOS 10.12.6 or higher I would recommend using Joel Bruner's solution below.

As of macOS 10.12.6 (not sure about earlier versions) it is possible to invoke: launchctl dumpstate and you will get a wealth of information about all running processes

Look for <LABEL> = { as the first line of info pertaining to that job

Here's a one liner to get all the active daemons and their plist paths:

grep -B 1 -A 1 "active count = 1$" <<< "$(launchctl dumpstate)"

The process name used in launchctl list is declared in a plist. While the plist should be at the location mentioned above, they can be almost anywhere.

I found the plist i was looking for with' locate. I was looking for org.postgresql.postgres locate *.plist | grep org.postgresql.postgres narrowed it down to 4 files

Here is the command to list all loaded .plist files and their corresponding files:

find /System/Library/Launch* /Library/Launch* ~/Library/Launch* -name '*.plist' -exec sh -c '/usr/libexec/PlistBuddy -c "Print Label" {} && echo {}' ';' | grep -wf <(launchctl list | grep -o "\S\+\..*$") -A1 | strings

or another version:

find /System/Library/Launch* /Library/Launch* ~/Library/Launch* -name '*.plist' -exec /usr/libexec/PlistBuddy -c "Print Label" {} ';' -print | grep -wf <(launchctl list | grep -o "\S\+\..*$") -A1 | strings

Explanation:

  • find all .plist files in the following locations: /System/Library/Launch* /Library/Launch* ~/Library/Launch*
  • Use PlistBuddy command to print Label of all found .plist files.
  • Use -print parameter of find to print the path to that file.
  • Fetch another list of all the jobs loaded into launchd and use as pattern file for grep -f.
  • Filter both lists and find the common elements and print its label along with its path (-A1).
  • Filter via strings to avoid printing binary files.

Since launchctl list list PIDs, one method is to use lsof command to see all loaded files of the process, e.g.

launchctl list | grep -o '^[0-9]\+' | xargs -n1 lsof -p | grep plist$

Another way is to run fs_usage command and re-load the .plist file, e.g.

sudo fs_usage | grep -w launchd | grep -w plist
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!