【问题标题】:Run Rails script in cron job?在 cron 作业中运行 Rails 脚本?
【发布时间】:2014-02-02 04:58:45
【问题描述】:

我们想从 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 脚本时我们做错了什么?

谢谢!

日志文件内容(采纳Ivan的建议):

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

【问题讨论】:

  • 在某处记录所有输出。所以我们可以看到到底是什么失败了。
  • 尝试使用rails脚本的完整路径,并确保运行cron脚本的用户可以读取/home/t/T/script/ia.rb
  • 我们内部有日志语句,但没有显示任何内容,@IvanDenisov。
  • 我指的是 rails 可执行文件的路径,如 /usr/local/bin/rails 或安装该脚本的任何位置。
  • @Chashalot 45 23 * * * rails runner -e production /home/t/T/script/ia.rb > /path/to/log/file 你的脚本没有被执行。这就是为什么你的内心日志什么也没说

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


【解决方案1】:

在工作中,我们在服务器上运行 rails 任务:

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

也许您需要 bash -lc 部分和“”中的所有命令

【讨论】:

  • 顺便说一句,“bash -lc”有什么作用?
  • 您正在执行一个新的 bash 实例,使用 -c 告诉它在字符串上运行命令,如果您的服务器没有 bash,请检查文档中是否有 -l gnu.org/software/bash/manual/bashref.html#Invoking-Bash可以使用任何终端
【解决方案2】:

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

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

【讨论】:

  • 嗯,添加双引号只会导致一个空的日志文件...其他建议?
  • 很抱歉,只是没有时间检查所有可能的选项,但我敢打赌其中一个或组合会起作用。也可以试试`rails runner -e production /home/t/T/script/ia.rb`
  • 日志现在显示“/bin/sh: /usr/local/bin/rails runner -e production /home/t/T/script/ia.rb: No such file or directory”甚至虽然当我们在命令提示符下运行命令时,它工作正常。有什么线索吗?
猜你喜欢
  • 2011-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-11
  • 1970-01-01
  • 2014-03-07
  • 2011-05-10
  • 2020-02-28
相关资源
最近更新 更多