1

I am running a docker image (with cron already installed) with rails. Everything is running fine, except I'm trying to add a cron job which works as a command, but is giving me an error when adding to crontab.

The following command is:

0,2 * * * * /bin/bash -l -c 'cd /app && bin/rails runner -e production '\''Statistic.create_statistic'\'' >> log/cron_log.log 2>> log/cron_error_log.log'

I made it run every 2 minutes, which I omitted in this snippet for clarity sake.

When I only run:

/bin/bash -l -c 'cd /app && bin/rails runner -e production '\''Statistic.create_statistic'\'' >> log/cron_log.log 2>> log/cron_error_log.log'

It works

The error I get is:

   /usr/local/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- bundler/setup (LoadError)
    from /usr/local/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in     `require'
    from /app/config/boot.rb:3:in `<top (required)>'
    from bin/rails:7:in `require_relative'
    from bin/rails:7:in `<main>'

I'm using the whenever gem, which seems to be doing its job correctly. What am I doing wrong?

brunoban
  • 177
  • 3
  • 12

1 Answers1

2

I'm not familiar enough with ruby to give a proper answer but it sounds like there's some kind of environment variable set in your bash shell login that is not getting set during cron execution.

You can set environment variables above your cronjob like

PATH="/path/to/gems;$PATH"
0,2 * * * * /bin/bash -l -c ' ..... '
atxdba
  • 5,158
  • 5
  • 24
  • 30