MacOS – Can’t get launchd task to run

launchdmacosplist

I've done this once and I'm trying to do it on a new computer and I can't see what I'm doing wrong.

I have a ruby script in my user folder named forecast.rb. I have set it to executable (a+x). It writes a line out to another file called .weather.txt.

I want to run forecast.rb every 5 minutes.

I have a plist file named com.jwhite.forecast_tmux.plist. That file looks like:

<?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.jwhite.forecast_tmux</string>
    <key>ProgramArguments</key>
    <array>
        <string>/Users/jwhite/forecast.rb</string>
    </array>
    <key>StartInterval</key>
    <integer>300</integer>
</dict>
</plist>

This file is located in ~/Library/LaunchAgents. And after moving it there I ran Launchctl load -w ~/Library/Launchagents/com.jwhite.forecast_tmux.plist.

If I run forecast.rb (./forecast.rb) it runs fine. But my launchd is not executing.

What am I missing?

EDIT:

I've started using LaunchControl and got the error.

/Users/jwhite/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- geocoder (LoadError)
from /Users/jwhite/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /Users/jwhite/forecast.rb:3:in `<main>'

But when I run this from the terminal everything executes fine. So launchctl can't read in Ruby Gems?

Best Answer

I had the same issue, trying to run a script on an interval using LaunchControl (great app, check it out). Will copy and paste straight from the source that helped me:

For GUI programs the default shell environment scripts are ignored, and therefore RVM managed ruby versions.

The workaround is to define them via launchctl. Those values are gone though after a reboot, so I put them in .bash_profile since I open a shell anyhow all the time, see below.

launchctl setenv GEM_HOME ~/.rvm/gems/ruby-2.2.1 launchctl setenv GEM_PATH ~/.rvm/gems/ruby-2.2.1:/Users/markus/.rvm/gems/ruby-2.2.1@global

All more permanent solutions (e.g. launchd.conf) don't work anymore in 10.10. Hope that helps anyone who ran into a similar issue.

In my case I just set it up the correct GEM_HOME and PATH in LaunchControl.