LaunchDaemons not launching my app, but LaunchAgents launch it instead

I am developing an app, in this case for macOS. My app has two parts:

  • A GUI
  • A Server

From the GUI i can start and stop the serve. My problem is that i need the server starts when the mac boots. Using the LaunchAgents i got to launch it when the user logins on, but i need to initiate it before.

I saw that i need to saving my .plis inside of /Library/LaunchDaemons. I did it but when i restart the computer the server didn t initiate.

I checked that my service gave me the status 1 so, it didn t launch correctly. Maybe the failure is inside the plist... but i dont understand why it works in LaunchAgent and not inside LaunchDaemons

My plist is:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.turnoffpc.turnoffpcplist</string>
    <key>ProgramArguments</key>
    <array>
        <string>/Applications/Turn Off PC.app/Contents/Resources/Turn Off PC/Server/dist/turn_off_pc_server.app/Contents/MacOS/turn_off_pc_server</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
</dict>
</plist>

For loading the service im using the following sh:

#!/bin/bash

# Copiar el archivo plist a /Library/LaunchDaemons/
sudo cp com.turnoffpc.turnoffpcplist.plist /Library/LaunchDaemons/

# Establecer los permisos adecuados
sudo chown root:wheel /Library/LaunchDaemons/com.turnoffpc.turnoffpcplist.plist
sudo chmod 644 /Library/LaunchDaemons/com.turnoffpc.turnoffpcplist.plist

# Descargar y luego cargar el servicio utilizando launchctl
sudo launchctl unload /Library/LaunchDaemons/com.turnoffpc.turnoffpcplist.plist
sudo launchctl load /Library/LaunchDaemons/com.turnoffpc.turnoffpcplist.plist

I checked that the service is in the correct location

The path to my app is: inside /Applications

Turn Off PC.app

  • Resources
    • Turn Off PC - Server - dist - turn_off_pc_server.app - Contents - MacOS - turn_off_pc_server

Someone sees the error or the thing that i am doing wrong? Thanks in advance

Replies

My app has two parts

Just to check, these are two separate executables, right? Trying to do this all in a single executable would be… tricky.

As to why your launchd property list isn’t working, if you’re running on macOS 13 or later, the first thing to check is System Settings > General > Login Items > Allow in the Background.

If that’s not it, please run this and let me know what it prints:

% sudo launchctl list com.turnoffpc.turnoffpcplist

Also, while none of the following are likely to be the cause of this issue, some notes:

  • I recommend that you use SMAppService to install your daemon. That’s much easier than messing around with shell scripts.

  • Specifically, if you switch to BundleProgram then the system will automatically launch your daemon from within your app, which breaks the requirement that your app be installed in /Applications.

  • If you don’t use SMAppService, make sure to set AssociatedBundleIdentifiers in your launchd property list. That allows the system to associate your daemon with your app.

  • The paths you’re using suggest that you’re not following the rules outlined in Placing Content in a Bundle. Things go more smoothly if you do.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Thanks for reply.

Yes i have several executables but there is only one which i want to run it before login. My computer is higher than MacOS 13.

The thing is ... why is LaunchAgents working properly with the exact same configuration of the plist?, the thing that changes is the sh for saving it in other path. and why is it not working with the LaunchDaemons? i dont know, may it be something related with permissions?

I guess that if the structure would be wrong LaunchAgents wouldn't work too