Docker

如何設置 cron 作業以在官方 Redmine Docker 映像中發送提醒電子郵件?

  • January 7, 2021

在基於Docker Hub上的官方圖像的 Redmine 容器中,我正在嘗試設置一個 cron 作業以定期發送提醒電子郵件。

按照此處的說明,在容器內鍵入bundle exec rake redmine:send_reminders RAILS_ENV=productionas可以正常工作。root但是,設置相同的命令 usingcrontab -e不起作用並輸出各種錯誤消息。我懷疑它們與PATH為 Bundler 或 rake 設置正確有關,但不熟悉 Ruby 應用程序我現在完全迷路了。我什至嘗試將(看似)相關的環境變數添加到crontab

PATH=/usr/local/bundle/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
RUBY_MAJOR=2.6
RUBY_VERSION=2.6.5
RUBY_DOWNLOAD_SHA256=d5d6da717fd48524596f9b78ac5a2eeb9691753da5c06923a6c31190abe01a62
GEM_HOME=/usr/local/bundle
BUNDLE_SILENCE_ROOT_WARNING=1
BUNDLE_APP_CONFIG=/usr/local/bundle
RAILS_ENV=production
HOME=/home/redmine
REDMINE_VERSION=4.1.0

但到目前為止無濟於事。

以下是我crontab -e到目前為止嘗試添加的內容。每個命令(當然)前面都是 cron 的日期時間字元串格式,後面是> /var/log/myjob.log 2>&1記錄它輸出的任何內容。

  • root cd /usr/src/redmine; bundle exec rake redmine:send_reminders RAILS_ENV=production
  • root cd /usr/src/redmine; /usr/local/bin/bundle exec /usr/local/bundle/bin/rake redmine:send_reminders RAILS_ENV=production

Could not locate Gemfile or .bundle/ directory

  • cd /usr/src/redmine; /usr/local/bin/bundle exec /usr/local/bundle/bin/rake redmine:send_reminders RAILS_ENV=production
  • /usr/src/redmine/remind.sh(以下腳本)

remind.sh

#!/bin/bash
cd /usr/src/redmine
bundle exec /usr/local/bundle/bin/rake redmine:send_reminders days=7 RAILS_ENV="production"
bundler: failed to load command: /usr/local/bundle/bin/rake
(/usr/local/bundle/bin/rake) Bundler::GemNotFound: Could not find
rake-13.0.1 in any of the sources  
/usr/local/lib/ruby/2.6.0/bundler/spec_set.rb:91:in `block in
materialize'   /usr/local/lib/ruby/2.6.0/bundler/spec_set.rb:85:in
`map!'   /usr/local/lib/ruby/2.6.0/bundler/spec_set.rb:85:in
`materialize'   /usr/local/lib/ruby/2.6.0/bundler/definition.rb:170:in
`specs'   /usr/local/lib/ruby/2.6.0/bundler/definition.rb:237:in
`specs_for'   /usr/local/lib/ruby/2.6.0/bundler/definition.rb:226:in
`requested_specs'  
/usr/local/lib/ruby/2.6.0/bundler/runtime.rb:108:in `block in
definition_method'  
/usr/local/lib/ruby/2.6.0/bundler/runtime.rb:20:in `setup'  
/usr/local/lib/ruby/2.6.0/bundler.rb:107:in `setup'  
/usr/local/lib/ruby/2.6.0/bundler/setup.rb:20:in `<top (required)>'  
/usr/local/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in
`require'  
/usr/local/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in
`require'
  • root cd /usr/local/bundle/bin/rake; rake --trace redmine:send_reminders RAILS_ENV=production
rake aborted! No Rakefile found (looking for: rakefile, Rakefile,
rakefile.rb, Rakefile.rb)
/usr/local/lib/ruby/gems/2.6.0/gems/rake-12.3.2/lib/rake/application.rb:698:in
`raw_load_rakefile'
/usr/local/lib/ruby/gems/2.6.0/gems/rake-12.3.2/lib/rake/application.rb:104:in
`block in load_rakefile'
/usr/local/lib/ruby/gems/2.6.0/gems/rake-12.3.2/lib/rake/application.rb:186:in
`standard_exception_handling'
/usr/local/lib/ruby/gems/2.6.0/gems/rake-12.3.2/lib/rake/application.rb:103:in
`load_rakefile'
/usr/local/lib/ruby/gems/2.6.0/gems/rake-12.3.2/lib/rake/application.rb:82:in
`block in run'
/usr/local/lib/ruby/gems/2.6.0/gems/rake-12.3.2/lib/rake/application.rb:186:in
`standard_exception_handling'
/usr/local/lib/ruby/gems/2.6.0/gems/rake-12.3.2/lib/rake/application.rb:80:in
`run' /usr/local/lib/ruby/gems/2.6.0/gems/rake-12.3.2/exe/rake:27:in
`<top (required)>' /usr/local/bundle/bin/rake:23:in `load'
/usr/local/bundle/bin/rake:23:in `<main>'

這是裡面的東西/usr/src/redmine。存在一個名為的文件Gemfile

root@975094a2b06b:/usr/src/redmine# ls -l
total 140
-rw-rw-r--  1 redmine redmine  538 Dec 20  2019 CONTRIBUTING.md
-rw-rw-r--  1 redmine redmine 2937 Dec 20  2019 Gemfile
-rw-rw-rw-  1 redmine redmine 5665 Apr 21  2020 Gemfile.lock
-rw-r--r--  1 root    root    5491 Feb 27  2020 Gemfile.lock.mysql2
-rw-r--r--  1 root    root    5483 Feb 27  2020 Gemfile.lock.postgresql
-rw-r--r--  1 root    root    5493 Feb 27  2020 Gemfile.lock.sqlite3
-rw-r--r--  1 root    root    5645 Feb 27  2020 Gemfile.lock.sqlserver
-rw-rw-r--  1 redmine redmine  205 Dec 20  2019 README.rdoc
-rwxrwxr-x  1 redmine redmine  275 Dec 20  2019 Rakefile
drwxrwxr-x  6 redmine redmine 4096 Dec 20  2019 app
-rw-rw-r--  1 redmine redmine  863 Dec 20  2019 appveyor.yml
drwxrwxr-x  2 redmine redmine 4096 Dec 20  2019 bin
drwxr-xr-x  1 redmine redmine 4096 Mar 26  2020 config
-rw-r--r--  1 root    root     136 Mar 10  2020 config.ru
drwxrwxrwx  1 redmine redmine 4096 Mar 26  2020 db
drwxrwxr-x  2 redmine redmine 4096 Dec 20  2019 doc
drwxrwxr-x  5 redmine redmine 4096 Dec 20  2019 extra
drwxr-xr-x  3 redmine redmine 4096 Oct 19 10:16 files
drwxrwxr-x  6 redmine redmine 4096 Dec 20  2019 lib
drwxr-xr-x  2 redmine redmine 4096 Mar 18  2020 log
drwxr-xr-x  5 redmine redmine 4096 Mar 26  2020 plugins
drwxrwxr-x  1 redmine redmine 4096 Dec 20  2019 public
-rwxr-xr-x  1 root    root     125 Dec 17 16:24 remind.sh
drwxrwxrwx  2 redmine redmine 4096 Feb 27  2020 sqlite
drwxrwxr-x 14 redmine redmine 4096 Dec 20  2019 test
drwxr-xr-x  1 redmine redmine 4096 Feb 27  2020 tmp
drwxrwxr-x  2 redmine redmine 4096 Dec 20  2019 vendor

什麼是正確的命令?

事實證明,BusyBox中包含的crond可以毫無問題地執行命令。也許它處理環境變數的方式與標準 crond 不同?bundle exec rake...

我所要做的就是

apt-get install busybox-static
busybox crontab -e

然後將以下作業添加到 crontab 的列表中。

0 7 * * 1-5 cd /usr/src/redmine; bundle exec rake redmine:send_reminders RAILS_ENV=production

請注意,目前,這些步驟是在執行的 Redmine 容器中手動完成的。我稍後會添加這些Dockerfile

引用自:https://serverfault.com/questions/1047158