Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a launchctl job I define as such:

<key>ProgramArguments</key>
    <array>
        <string>/bin/sh</string>
        <string>-c</string>
        <string>~/projects/script.sh</string>
    </array>

When I run script.sh manually, the script works fine, as it uses the currently configured rvm version of ruby. When I run this through launchctl, the system version of Ruby is used, which breaks the script. How can I get this script to run with the right version of ruby available?

share|improve this question

2 Answers 2

Change script.sh to set environment variable PATH before it invokes any programs that depend on PATH being set correctly.

share|improve this answer
    
The only problem is that RVM dynamically points to the current Ruby, which could be anywhere, e.g. ~/.rvm/rubies/ruby-1.9.3-head/bin/ruby. There's no fixed path that can be included. –  slhck Jun 12 '12 at 19:14

The best way will be to use an rvm "wrapper", which loads the ruby environment and then runs the script. For more info:

$ rvm help wrapper

Example:

$ rvm wrapper 2.0.0 hello
$ hello_ruby -e "puts RUBY_VERSION"
$ hello_rake -T
$ which hello_ruby # => /Users/me/.rvm/bin/hello_ruby

...etc...

And you can use "/Users/me/.rvm/bin/hello_ruby" as the executable in your launchd configs, and the rvm environment will be set up for you.

This is also really nice to do without specifying the exact ruby version so when a patch level comes out, upgrade rvm and your wrappers will point to the newer patched ruby without needing to unload/load the launchd files.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.