0
votes

I'm trying to setup a cron job on Openshift due to import emails in a Redmine application. Therefore, I prepared a minutely script like this:

#!/bin/bash
rake RAILS_ENV=production -f ${OPENSHIFT_REPO_DIR}/Rakefile redmine:email:receive_imap host=imap.googlemail.com port=993 ssl=1 [email protected] password=yyy ...

It runs without problems when launched by hand on a ssh connection. When run by cron, instead, rake could not be found. Making some debugging, I found that the path is not the same as the login shell; and even if I used a full path for rake, ruby that is found is version 1.8 (not 1.9 as per the cartridge), and whenever I set the very same path as the shell, then libruby-1.9 is not found.

Following some other advice I tried to add the following line in place of setting a custom PATH:

source /usr/bin/rhcsh

but nevertheless rake is still not found. I also tries to use bundle exec.

What is the right way to set an environment for cron on Openshift so that it can run like a login shell?

3
In a shell script, especially in a cron job, you should never rely on $PATH, you should reference the rake you want absolutely. This is a security measure as otherwise it is much too simple to get privileged code executed. That way there will be no path issues either. - Patru

3 Answers

1
votes

You may need to cd to the directory where your bundle is installed first (where your Gemfile is) something like this maybe?

cd $OPENSHIFT_REPO_DIR && bundle exec rake .....
0
votes

This is a bug in the cron cartridge. You can refer to this question in SO. It is actually a question with the Python cartridge and the cron cartridge. But it is the cron cartridge which will affect all. There is also a OpenShift Bug Report mentioned within.

The bug is as you have observed, the cron cartridge uses Ruby 1.8 instead of Ruby 1.9. Thus, the gems installed with Ruby 1.9 are not available to the cron cartridge using Ruby 1.8.

There is already a bugfix for this bug, you can refer to the OpenShift Bug Report. But not too sure if it is pushed out already.

Meanwhile, there is a temporary workaround, by exporting the PATH and LD_LIBRARY_PATH in the cron script. You can refer to the OpenShift Bug Report.

Hope this helps.

0
votes

If you are using rvm, openshift may getting some problem to shift to default rvm.You can also try something like this so it will set rvm to default before running bundle and can also generate your cron log as well to get the exact status of your cron job:

https://rvm.io/rvm/install

use bundle exec to get rid from more than one version of rake

cd $OPENSHIFT_REPO_DIR && rvm gemset use "yourgemsetname" && RAILS_ENV=production bundle exec rake cron_job:cron_job --silent >> log/cron_log