Launch Agent Not Starting - macos

I’m trying to make a launch agent that starts a Python script. It should run when there’s a network connection but it doesn’t do that. launchctl list says it’s loaded:
launchctl list | grep test.Flopsey.DiscordMusicBot
- 0 test.Flopsey.DiscordMusicBot
When I start it with launchctl start test.Flopsey.DiscordMusicBot it works fine. The .plist file (which is stored under ~/Library/LaunchAgents) 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>test.Flopsey.DiscordMusicBot</string>
<key>ProgramArguments</key>
<array>
<string>/Library/Frameworks/Python.framework/Versions/3.5/bin/python3.5</string>
<string>/path/to/MusicBot/run.py</string>
</array>
<key>EnvironmentVariables</key>
<dict>
<key>PATH</key>
<string>/bin:/usr/bin:/usr/local/bin</string>
</dict>
<key>StandardOutPath</key>
<string>/path/to/MusicBot/log.log</string>
<key>StandardErrorPath</key>
<string>/path/to/MusicBot/log.log</string>
<key>WorkingDirectory</key>
<string>/path/to/MusicBot</string>
<key>KeepAlive</key>
<dict>
<key>NetworkState</key>
<true/>
</dict>
</dict>
</plist>
I’m new to launchd and I followed this tutorial. I think the solution to my problem is very basic but I have no idea what it could be. I’ve also made another similar agent and it works fine (unlike the music bot it’s only one file).
Update
Thanks to #LCC’s comment I realised that using NetworkState doesn’t work anymore on OS X 10.10 and higher. Since the script exits when it can’t connect to the Internet I can just set KeepAlive to <true/> and set up a ThrottleInterval so launchd restarts the script after a cooldown if it couldn’t connect.

Related

How to add an application as a login item for all users in Mac OSX

I am trying to add an application as login item for all users by creating an launchd plist and copying it in /Library/LaunchAgents and also by loading it.
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.testapp.UserAgent</string>
<key>ProgramArguments</key>
<array>
<string>/Applications/TestAgent.app/Contents/MacOS/TestAgent</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<false/>
</dict>
</plist>
RunAtLoad will launch the application at every login.
It is working for me when I login in to all different user accounts in my machine but if I quit it manually then also it is launching by itself.
How can I make it to launch only once for each login and if I quit it then it should not be launched by itself.
The best resource to know how to use launchd, the daemon that launches processes on macOS is here:
https://www.launchd.info
In here, it is well explained what other settings you can use apart from RunAtLoad or together with it.
For example, in your case, you could use the key KeepAlive as follows:
<key>KeepAlive</key>
<dict>
<key>SuccessfulExit</key>
<false/>
</dict>
This would only relaunch your process if it exited with a code different than 0, which normally denotes some abnormal situation or error. If the exit is normal then your process will not be relaunched by launchd.
Check the tab Configuration on this site to know which other configurations you could use.

CVS pserver in OS X Sierra times out intermittently

I recently migrated my cvs server from a very old mac to some new hardware. I found instructions for setting up Launch Services to run cvs pserver, and it works sometimes.
/Library/LaunchDaemons/cvspserver.plist contains this
<?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>Label</key>
<string>com.apple.cvspserver</string>
<key>UserName</key>
<string>root</string>
<key>Program</key>
<string>/usr/local/bin/cvs</string>
<key>ProgramArguments</key>
<array>
<string>-f</string>
<string>--allow-root=/home/cvsroot</string>
<string>pserver</string>
</array>
<key>Sockets</key>
<dict>
<key>Listeners</key>
<dict>
<key>SockPassive</key>
<true/>
<key>SockServiceName</key>
<string>2401</string>
<key>SockType</key>
<string>stream</string>
</dict>
</dict>
<key>inetdCompatibility</key>
<dict>
<key>Wait</key>
<false/>
</dict>
</dict>
</plist>
CVS will work for a while, but sometimes I'll get:
cvs [update aborted]: connect to cvs(10.10.0.2):2401 failed: Operation timed out
Trying again sometimes works. If I do this:
sudo launchctl unload -w /Library/LaunchDaemons/cvspserver.plist
sudo launchctl load -w /Library/LaunchDaemons/cvspserver.plist
It'll work right away, but after a while the timeouts start again.
Any ideas? I don't even know how to debug this...
I now believe that the problem was that the server was going to sleep, and even though it was set to wake on network activity, that doesn't actually work. So I set it to never sleep and it's been reliable since then.

OSX launchd plist for node forever process

I am trying to write a launchd.plist file for my node server. I am using forever to run my node server. I would like the server to start on boot. I would also like to wait for the mongodb launchd plist to run first.
I installed mongobb using homebrew and it came with a launchd.plist already. I have executed the following:
$ launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mongodb.plist
plist for mongodb is:
<!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>homebrew.mxcl.mongodb</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/opt/mongodb/mongod</string>
<string>run</string>
<string>--config</string>
<string>/usr/local/etc/mongod.conf</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<false/>
<key>WorkingDirectory</key>
<string>/usr/local</string>
<key>StandardErrorPath</key>
<string>/usr/local/var/log/mongodb/output.log</string>
<key>StandardOutPath</key>
<string>/usr/local/var/log/mongodb/output.log</string>
<key>HardResourceLimits</key>
<dict>
<key>NumberOfFiles</key>
<integer>1024</integer>
</dict>
<key>SoftResourceLimits</key>
<dict>
<key>NumberOfFiles</key>
<integer>1024</integer>
</dict>
</dict>
</plist>
If I shutdown the computer and restart mongodb fires up as it should.
However my node server is not starting. Any ideas?
<?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>
<dict>
<key>SuccessfulExit</key>
<false/>
</dict>
<key>Label</key>
<string>com.test.app</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/bin/forever</string>
<string>-a</string>
<string>-l</string>
<string>/var/log/app/app.log</string>
<string>-e</string>
<string>/var/log/app/app_error.log</string>
<string>/data/server/app.js</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>StartInterval</key>
<integer>3600</integer>
</dict>
</plist>
EDIT:
writing to log file and I see this:
env: node: No such file or directory
I think this means that the node binary cannot be found. I can echo $PATH and /usr/local/bin is in my path. I can start node from the terminal. Ideas?
Add environment Variables worked for me.
<key>EnvironmentVariables</key>
<dict>
<key>PATH</key>
<string>/usr/local/bin/:$PATH</string>
</dict>
You may also need to add WorkingDirectory to your node app.
<key>WorkingDirectory</key>
<string>path/to/your/node/app</string>
I had this problem too, but I solved it using an Automator app that runs at startup.
Open Automator and choose, New Application
Insert in your workflow "Run Shell Script"
Use this code in the shell script, changing the paths to your paths
export PATH=/usr/local/bin/:$PATH
cd /path/to/your/nodejs/app
forever start app.js
Go to System Preferences >> User & Groups and click on Login Items tab
Add your Automator app and be happy.
The important part of the solution is the first line of the script (adding your bin to the path). It would probably work to add a Startup Item pointed at a bash script too (and no Automator script), feel free to try!
Add node before forever:
<key>ProgramArguments</key>
<array>
<string>/usr/local/bin/node</string>
<string>/usr/local/bin/forever</string>
<string>/path/to/app.js</string>
</array>
This is not an answer of the original questions, but I was looking for a simple way to start a node server (and keep it running) after reboot. I found pm2 to be a much easier to set up than the solutions for forever above.
# install pm2
npm install pm2 -g
# start server
pm2 start app.js
# start pm2 after reboot (might need sudo)
$ pm2 startup
http://pm2.keymetrics.io/docs/usage/startup/
I'm not sure when node-launchd came out. However, it seems to be the more reliable solution.
The solution in which a workflow is created and added into the login item is also well appreciated. However, the problem is that if the app is served on a server and when the server is restarted, I wonder the app will be started before the user login to the system. Didn't try, though.

When the launchd will register for the events?

I need to open my application and run a specific task from that application automatically in a given time. So I searched in google and came up with launchd. First question, Is this is correct choice?
I have created a plist and I kept that in the location ~/Library/LaunchAgents/fileName.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>Label</key>
<string>com.companyname.example</string>
<false/>
<key>RunAtLoad</key>
<true/>
<key>UserName</key>
<string>mac_1</string>
<key>Program</key>
<string>open</string>
<key>ProgramArguments</key>
<array>
<string>Argument_1</string>
<string>Argument_2</string>
</array>
<key>StartCalendarInterval</key>
<dict>
<key>Day</key>
<integer>1</integer>
<key>Hour</key>
<integer>10</integer>
<key>Minute</key>
<integer>41</integer>
<key>Month</key>
<integer>8</integer>
<key>Weekday</key>
<integer>3</integer>
</dict>
<key>KeepAlive</key>
<true/>
</dict>
</plist>
The Problem with this is, It is working only when the computer will restarted, otherwise it will not be invoked. So, My Second question when the launchd will register for the events ?
There is a utility called "launchctl"
You need to use it to load your plis file into launchd.
Type: launchctl load plist_file.
If you want to schedule something from your application - one way would be calling "system" to execute launchtl utility. There is also undocumented API for launchd but I couldn't find a way to use it for scheduling.
launchd would be the easiest way to schedule something on the system, as it has reach scheduling properties.

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