Question

We want to run a Rails script (e.g., rails runner -e production script/test.rb) from a cron job, but it fails silently. The script works on its own when run from the command line.

Here is the line in our crontab: (This was set at 11:40 PM)

45 23 * * * rails runner -e production /home/t/T/script/ia.rb

What we doing wrong in running this Rails scripts from a cron job?

Thanks!

Contents of log file (after taking Ivan's suggestion):

[root@newvps T]# cat script/script.log
Usage:
  rails new APP_PATH [options]

Options:
  -r, [--ruby=PATH]           # Path to the Ruby binary of your choice
                              # Default: /usr/local/bin/ruby
  -d, [--database=DATABASE]   # Preconfigure for selected database (options: mysql/oracle/postgresql/sqlite3/frontbase/ibm_db)
                              # Default: sqlite3
  -b, [--builder=BUILDER]     # Path to an application builder (can be a filesystem path or URL)
  -m, [--template=TEMPLATE]   # Path to an application template (can be a filesystem path or URL)
      [--dev]                 # Setup the application with Gemfile pointing to your Rails checkout
      [--edge]                # Setup the application with Gemfile pointing to Rails repository
      [--skip-gemfile]        # Don't create a Gemfile
  -O, [--skip-active-record]  # Skip Active Record files
  -T, [--skip-test-unit]      # Skip Test::Unit files
  -J, [--skip-prototype]      # Skip Prototype files
  -G, [--skip-git]            # Skip Git ignores and keeps

Runtime options:
  -f, [--force]    # Overwrite files that already exist
  -p, [--pretend]  # Run but do not make any changes
  -q, [--quiet]    # Supress status output
  -s, [--skip]     # Skip files that already exist

Rails options:
  -v, [--version]  # Show Rails version number and quit
  -h, [--help]     # Show this help message and quit

Description:
    The 'rails new' command creates a new Rails application with a default
    directory structure and configuration at the path you specify.

Example:
    rails new ~/Code/Ruby/weblog

    This generates a skeletal Rails installation in ~/Code/Ruby/weblog.
    See the README in the newly created application to get going.
Was it helpful?

Solution

At work, on the server, we run rails tasks with:

0 6 * * * bash -lc "cd /some_path && bundle exec rake RAILS_ENV=production some_task"

maybe you need the bash -lc part and all the command inside " "

OTHER TIPS

Enclose your command into quotes or use backslashes to mirror spaces

45 23 * * * "rails runner -e production /home/t/T/script/ia.rb"

or

45 23 * * * rails\ runner\ -e\ production\ /home/t/T/script/ia.rb

instead of

45 23 * * * rails runner -e production /home/t/T/script/ia.rb
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top