Reputation: 19
I am creating an OSX app the requires GUI at the login page of a iMac and I was wondering (if possible) I could run a launchd plist file after reboot. The following plist file is in the "/Library/LaunchAgents/" directory:
<?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>Disabled</key>
<false/>
<key>Label</key>
<string>com.example.apple-samplecode.PreLoginAgentCocoa</string>
<key>LimitLoadToSessionType</key>
<string>LoginWindow</string>
<key>KeepAlive</key>
<true/>
<key>ProgramArguments</key>
<array>
<string>/Library/PrivilegedHelperTools/PreLoginAgentCocoa.app/Contents/MacOS/PreLoginAgentCocoa</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
The file works for when I log out from the desktop but doesn't work when I restart the computer.
Any help is useful.
Upvotes: 1
Views: 2410
Reputation: 1322
Building upon @shubham's answer, the minimum required ownership/file permissions for the .plist file to run at user-login (under Monterrey, at least) are:
~/Library/LaunchAgents
In my own case, I got it executing automatically, following reboot (+login) with:
-r-------- 1 foobar wheel 1.6K Nov 4 14:26 myScript.plist
Upvotes: 0
Reputation: 2816
Difference between putting plist
file into below directories
/Library/LaunchAgents
: It will run as agent. Agents run within user sessions, as the logged-in user.
/Library/LaunchDaemons
: It will run as a daemon. Daemons run in system context independent of who is logged in and usually run as a root.
If you want to start your service/script at the reboot of the machine then you need to put your plist
file into the /Library/LaunchDaemons
folder and execute the following commands.
sudo launchctl bootstrap system /Library/LaunchDaemons/${YOUR_SERVICE_NAME}.plist
sudo launchctl enable system/${YOUR_SERVICE_NAME}
sudo launchctl kickstart -kp system/${YOUR_SERVICE_NAME}
Note: It will launch service with user root and start system-wide.
References: Launchctl Man Page(https://ss64.com/osx/launchctl.html)
Upvotes: 1
Reputation: 27611
an OSX app the requires GUI at the login page of a iMac
LaunchAgents
are executed for the currently logged in session user. When you reboot the Mac and are presented with the login screen, there is no session user.
As Apple Documentation states: -
A user agent is essentially identical to a daemon, but is specific to a given logged-in user and executes only while that user is logged in
Without the given 'logged-in user', your application cannot be executed as a LaunchAgent
.
The file works for when I log out from the desktop
The session user is still available at this time, so will function on logout, before the session user is released.
Upvotes: 1