简体   繁体   English

RAILS 6:如何在 crontab 中运行 rails 任务?

[英]RAILS 6 : How to run rails tasks in crontab?

I wuould like to run rails tasks started by cron (crontab entry).我想运行由 cron(crontab 条目)启动的 rails 任务。 But I get always the error that the rails command could not be found.但我总是收到无法找到 rails 命令的错误。

/bin/sh: rails: No such file or directory

So what I tried is the same as the .profile or .bashrs contains :所以我尝试的与 .profile 或 .bashrs 包含的内容相同:

source "$HOME/.rvm/scripts/rvm"
export PATH="$PATH:$HOME/.rvm/bin"

It dont know which variables need to be exported, or which programm/tools/sub-command need to be execuuted before, in which order, first execute command XYZ then export GEM___ variable 1 2 3, then execute another sub-command, then check by command ABC but not when tool rvm list returns string not equal RVM_string_1 ....它不知道需要导出哪些变量,或者之前需要执行哪些程序/工具/子命令,按照什么顺序,先执行命令XYZ然后导出GEM___变量1 2 3,然后再执行另一个子命令,然后检查通过命令 ABC 但不是当工具 rvm list 返回字符串不等于 RVM_string_1 ....

Hi you can use whenever gem for this.嗨,你可以使用,只要创业板这一点。 Please follow these bellow steps:请按照以下步骤操作:

Step 1: Add whenever gem into your Gemfile第 1 步:将每当 gem 添加到您的 Gemfile 中

# Gemfile

gem 'whenever', '~> 1.0'

Step 2: Run bundle install to install this gem.第 2 步:运行bundle install来安装这个 gem。

Step 3: Run wheneverize .第 3 步:运行wheneverize . This will create an initial config/schedule.rb file for you (as long as the config folder is already present in your project).这将为您创建一个初始的config/schedule.rb文件(只要 config 文件夹已经存在于您的项目中)。

Step 4: Open newly created config/schedule.rb and write your scheduler task.第 4 步:打开新创建的config/schedule.rb并编写您的调度程序任务。

# config/schedule.rb
# here `fetch_user_meetings:sync` this is one task in my case.
# please replace this with your task.

every :hour do
  rake "fetch_user_meetings:sync", output: {
    error: 'log/fetch_user_meetings_sync_error.log',
    standard: 'log/fetch_user_meetings_sync_access.log'
  }
end

Step 5: Run whenever .第 5 步: whenever运行。 This command will output a preview of the generated schedules in the actual cron format and attach your job with crontab.此命令将以实际的 cron 格式输出生成的计划的预览,并使用 crontab 附加您的作业。 Whenever you are modifying your config/scheduler.rb , run this command.每当您修改config/scheduler.rb ,请运行此命令。 This will update your cron job.这将更新您的 cron 作业。

NB: There are many options to set your scheduler job.注意:有很多选项可以设置您的调度程序作业。 Please follow this doc .请遵循此文档

Happy Coding :-)快乐编码:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM