bashmacoslaunchdlaunchctl

launch daemon won't load from Bash script but will from terminal


I have this line inside a post install Bash script file which runs from within a PKG installer:

launchctl load /Library/LaunchDaemons/com.mycompany.myapp.plist

The PKG correctly installs both the job plist file and the applet which the job calls.

But when I check if the job is loaded in terminal, I get nothing returned by this:

launchctl list | grep mycompany

If I execute the same load command in Terminal, the job is loaded as expected.

Why does the job not get loaded when the script runs?


Solution

  • When you run launchctl list as a normal user, it lists Launch Agents running in your user session; to list Launch Daemons, run it as root (i.e. sudo launchctl list).

    More details: some of the launchctl subcommands allow you to explicitly specify which domain you want to deal with. For example:

    launchctl print system      # Prints Launch *Daemons*
    launchctl print user/501    # Prints Launch *Agents* for user #501's session
    

    But the older "legacy" subcommands, like list, load, unload, start, stop, etc predate this convention and use the user ID the command runs as to determine the domain to act on. So for example:

    launchctl load /path/to/plist         # Loads the plist as an Agent in my user session
    launchctl list                        # Lists Agents in my user session
    sudo launchctl load /path/to/plist    # Loads it as a Daemon in the system domain
    sudo launchctl list                   # Lists Daemons in the system domain
    

    Your package is presumably running its scripts as root, so it will load the job as a Daemon (which is what you want), but it can depend on exactly how the package is configured.