XPC service, launch as Launch Agent - cocoa

i did create an XPC service, that runs in background and creates periodical tasks
placed it into path
/Users/Peter/Library/Application Support/AGENT TEST APPS/CS Agent.xpc
i can run it, by directly executing the file
i placed a .plist it into the LaunchAgents, refereeing to the executable
<?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>Debug</key>
<true/>
<key>Label</key>
<string>com.company.CS-Agent</string>
<key>KeepAlive</key>
<true/>
<key>RunAtLoad</key>
<true/>
<key>Program</key>
<string>/Users/Peter/Library/Application Support/AGENT TEST APPS/CS Agent.xpc</string>
</dict>
</plist>
but it doesn't launch upon startup as it should
UPDATE
did try to load manually
Peters-MacBook-Air:LaunchAgents Peter$ launchctl load com.company.CS-Agent.plist
com.company.CS-Agent: Already loaded
But i don't see it in activity monitor, nor any output it should produce (create files)
when i do launchctl list i see
- 13 com.company.CS-Agent

Related

How to start a launch agent on demand after the installation?

I've coded my launch daemon and a launch agent for macOS. When the app is installed, I need to run both the daemon and the agent. I place the .plist for the deamon into /Library/LaunchDaemons and then run (as admin):
launchctl load /Library/LaunchDaemons/com.example.MyDaemon.plist
and since the plist has:
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
the daemon starts up right away.
But I'm struggling to do the same with my launch agent. Here's it's plist that I place into /Library/LaunchAgents directory for it to start for every user and also in the login screen:
<?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>LimitLoadToSessionType</key>
<array>
<string>Aqua</string>
<string>LoginWindow</string>
</array>
<key>Disabled</key>
<false/>
<key>Label</key>
<string>com.example.MyAgent.plist</string>
<key>Program</key>
<string>/Library/PrivilegedHelperTools/com.example/MyAgent</string>
<key>ProgramArguments</key>
<array>
<string>-d</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
</dict>
</plist>
when I try to run it as:
launchctl load /Library/LaunchAgents/com.example.MyAgent.plist
I get an error:
Warning: Expecting a LaunchDaemons path since the command was ran as
root. Got LaunchAgents instead. launchctl bootstrap is a recommended
alternative. /Library/LaunchAgents/com.example.MyAgent.plist:
Path had bad ownership/permissions Load failed: 122: Path had bad
ownership/permissions
what am I doing wrong?

How to set already running binary to run at startup

I have a Go program that is supposed to run as a daemon with minimal front-end for the user (just an icon in the system tray.)
The fact that it's written in Go doesn't really matter much, the main issue I have is getting a binary (that is already running) to run at startup on a Mac.
I can set the binary to run at startup like so:
cp daemon.plist ~/Library/LaunchAgents/daemon.plist
(cp the plist into the LaunchAgents directory)
launchctl load -w ~/Library/LaunchAgents/daemon.plist
(then load the plist with launchctl.)
And that works fine. The only issue is that launchctl load it starts another instance of the binary that is already running. I want to be able to enable (and disable) running the binary at startup for the binary that is running, without starting another instance of it.
And here's the .plist file if you want to see it:
<?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>daemon</string>
<key>ProgramArguments</key>
<array>
<string>/Applications/Daemon.app/Contents/MacOS/daemon</string>
</array>
<key>ProcessType</key>
<string>Interactive</string>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<false/>
</dict>
</plist>
I figured it out.
So first, the binary is running. It starts out as not enabled to run at startup. To initialize, you cp the plist to the ~/Library/LaunchAgents/ directory:
cp daemon.plist ~/Library/LaunchAgents/daemon.plist
And the plist looks like this:
<?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>daemon</string>
<key>ProgramArguments</key>
<array>
<string>/Applications/Daemon.app/Contents/MacOS/daemon</string>
</array>
<key>ProcessType</key>
<string>Interactive</string>
<key>RunAtLoad</key>
<false/>
<key>KeepAlive</key>
<false/>
</dict>
</plist>
NOTE: The RunAtLoad key is set to false.
Then we load it into launchctl:
launchctl load -w ~/Library/LaunchAgents/daemon.plist
It shouldn't load since RunAtLoad is set to false.
Then, we can enable and disable running at startup like so:
To enable:
Set RunAtLoad to true
cp the plist over
To disable:
Set RunAtLoad to false
cp the plist over
Kind of hacky, but it works.

Starting Automator Script with LaunchAgent

I built a job by Automator to clear the downloads folder and empty the trash. When i start the created app by double click, it starts correctly. But it has to be transferred to other computer in our company and i dont want to give the user the job to launch the app.
So i created this LaunchAgent script to start the app.
<?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.herpag.aufraeumen.plist</string>
<key>ProgramArguments</key>
<array>
<string>/Users/hansthiele/Applications/Aufraeumen.app</string>
</array>
<key>StartCalendarInterval</key>
<dict>
<key>Minute</key>
<integer>45</integer>
<key>Hour</key>
<integer>13</integer>
</dict>
</dict>
</plist>
My problem is now, that the console tells me that the job is started, but nothing happens. The downloads are not cleared and the trash is still full. Also the error.log is empty.
There are 2 issues:
The Label argument must be specified without the .plist extension.
The path must represent the executable rather than the application container. In case of an Automator app the executable is named Application Stub (unlike Applet in an AppleScript app). It's assumed that hansthiele is the real short username of the current user.
Try
<?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.herpag.aufraeumen</string>
<key>ProgramArguments</key>
<array>
<string>/Users/hansthiele/Applications/Aufraeumen.app/Contents/MacOS/Application Stub</string>
</array>
<key>StartCalendarInterval</key>
<dict>
<key>Minute</key>
<integer>45</integer>
<key>Hour</key>
<integer>13</integer>
</dict>
</dict>
</plist>

Preventing an application closing using AppleScript

I have created a script with AppleScript and have exported it as an application. How can I force the application to stay open constantly so the user cannot force-quit it: i.e. how can I open the application again on force-quit?
Thanks
You could save a plist like this as ~/Library/LaunchAgents/test.plist:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC -//Apple Computer//DTD PLIST 1.0//EN http://www.apple.com/DTDs/PropertyList-1.0.dtd>
<plist version="1.0">
<dict>
<key>KeepAlive</key>
<true/>
<key>Label</key>
<string>test</string>
<key>Program</key>
<string>/Applications/Untitled.app/Contents/MacOS/applet</string>
<key>RunOnLoad</key>
<true/>
</dict>
</plist>
It can be loaded with launchctl load ~/Library/LaunchAgents/test.plist or by logging out and back in.

Launchd plist service is not shutting down

I have application i want to run as LaunchDaemon on OSX. My plist looks like this:
<?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>KeepAlive</key>
<false/>
<key>Label</key>
<string>my.service</string>
<key>ProgramArguments</key>
<array>
<string>/servertest/MyService</string>
<string>-jvm</string>
<string>server</string>
<string>-nodetach</string>
<string>-outfile</string>
<string>out.txt</string>
<string>-errfile</string>
<string>err.txt</string>
<string>-verbose</string>
<string>-debug</string>
<string>-home</string>
<string>/System/Library/Frameworks/JavaVM.framework/Home</string>
<string>-cp</string>
<string>./lib/hsqldb.jar:./lib/myservice-wrapper.jar:./lib/commons-daemon-1.0.8.jar</string>
<string>my.service.DaemonMac</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>StandardErrorPath</key>
<string>/servertest/stderr.log</string>
<key>StandardOutPath</key>
<string>/servertest/stdout.log</string>
<key>WorkingDirectory</key>
<string>/servertest</string>
</dict>
</plist>
Service starts on load or when i call 'launchctl start my.service'. But it's not going down when i use 'launchctl stop my.service' or even unload plist. It remains in memory and runs as if nothing happened. Have no idea what could be the reason. Please, help!
Does your application daemonize itself (i.e. drop into the background)? If so, it's essentially detaching itself from launchd and hence preventing launchd from managing it.
EDIT: if the application isn't daemonizing itself, the next thing to look at is to see what launchd thinks is going on with launchctl list my.service. This should dump a list of properties of the daemon, including its PID (if launchd thinks it's running) and LastExitStatus (whether it exited successfully last time it ran). Does the PID correspond to the actual running process? Does the result change when you try to stop the service (esp. does the PID change to 0)? Does a process listing (e.g. with ps -axj) show any child processes living on after the parent exits?
Oh, and just to make sure: when managing LaunchDaemons, you need to use sudo launchctl (without the sudo, launchctl will try to manage LaunchAgents within your user session).
The file that worked for me is below. I've learned that my deamon must not detach itself from controling process and it has to be "OnDemand" so i could stop it when i want. I've made some shaman dances while working on it and though it's not clear to me at which point it began to work. Now it does.
<?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>KeepAlive</key>
<false/>
<key>Label</key>
<string>myserver</string>
<key>OnDemand</key>
<true/>
<key>ProgramArguments</key>
<array>
<string>/opt/MYServer/MYServer</string>
<string>-server</string>
<string>-outfile</string>
<string>/opt/MYServer/out.txt</string>
<string>-errfile</string>
<string>/opt/MYServer/err.txt</string>
<string>-verbose</string>
<string>-debug</string>
<string>-nodetach</string>
<string>-home</string>
<string>/System/Library/Frameworks/JavaVM.framework/Home</string>
<string>-cp</string>
<string>/opt/MYServer/lib/hsqldb.jar:/opt/MYServer/lib/my-wrapper.jar:/opt/MYServer/lib/commons-daemon-1.0.8.jar</string>
<string>my.service.DaemonMac</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>StandardErrorPath</key>
<string>/opt/MYServer/stderr.log</string>
<key>StandardOutPath</key>
<string>/opt/MYServer/stdout.log</string>
<key>WorkingDirectory</key>
<string>/opt/MYServer</string>
</dict>
</plist>

Resources