Ruby on rails 在cron作业中运行Rails脚本?
我们希望从cron作业运行Rails脚本,例如Rails runner-e production script/test.rb,但它以静默方式失败。当从命令行运行时,脚本会自行工作 这是我们的crontab中的一行:设置在晚上11:40Ruby on rails 在cron作业中运行Rails脚本?,ruby-on-rails,ruby-on-rails-3,cron,crontab,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脚本时我们做错了什么 谢谢 采纳Ivan建议后的日志文件内容: [root@newvps T]# cat
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.
将命令括在引号中或使用反斜杠镜像空间
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任务,包括:
0 6 * * * bash -lc "cd /some_path && bundle exec rake RAILS_ENV=production some_task"
也许您需要bash-lc部分和其中的所有命令将所有输出记录在某个地方。因此,我们可以看到失败的地方。尝试使用rails脚本的完整路径,并确保运行cron脚本的用户可以读取/home/t/t/script/ia.rbwe内部有日志语句,但没有显示任何内容,@IvanDenisov。我指的是rails可执行文件的路径,正如在/usr/local/bin/rails中或安装该脚本的任何地方一样。@Chashalot 45 23***rails runner-e production/home/t/t/script/ia.rb>/path/to/log/file您的脚本不会被执行。这就是为什么您的内部日志没有显示任何内容。添加双引号只会导致一个空日志文件。。。其他建议?很抱歉,我没有时间检查所有可能的选项,但我打赌其中一个或组合会起作用。还可以尝试“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:没有这样的文件或目录,即使在命令提示符下运行命令,它也可以正常工作。有什么线索吗?顺便说一句,bash-lc做什么?你正在执行一个新的bash实例,使用-c命令它在字符串上运行命令,检查文档中的-l,如果你的服务器没有bash,你可以使用任何终端
0 6 * * * bash -lc "cd /some_path && bundle exec rake RAILS_ENV=production some_task"