在cron作业中运行Rails脚本?

时间:2014-01-14 07:52:58

标签: ruby-on-rails ruby-on-rails-3 cron crontab

我们希望从cron作业运行Rails脚本(例如,rails runner -e production script / test.rb),但它无声地失败。从命令行运行时,脚本可以自行运行。

以下是我们的crontab中的行:(这是在晚上11:40设置的)

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

从cron作业运行这个Rails脚本我们做错了什么?

谢谢!

日志文件的内容(在接受伊万的建议之后):

[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.

2 个答案:

答案 0 :(得分:0)

将命令括在引号中或使用反斜杠来镜像空格

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

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

而不是

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

答案 1 :(得分:0)

在工作中,在服务器上,我们使用以下命令运行rails任务:

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

也许您需要 bash -lc 部分以及“”

中的所有命令