[英]Cron doesn't run Rails Puma server automatically in Ubuntu
我使用 Capistrano 在 Digital Ocean Ubuntu Server 中部署了一個 Rails 應用程序。 由於某種原因,美洲獅服務器突然停止。 我不確定是什么原因。
這就是為什么我創建了一個腳本/home/deploy/startup-script.sh該腳本包含在守護進程中啟動 Rails 應用程序的命令
#!/bin/bash
echo "Running Puma Server"
touch hello.txt
cd /srv/www/apps/app/current
bundle exec puma -C /srv/www/apps/app/shared/puma.rb --daemon
然后我每分鍾使用 Cron 運行這個腳本。 我的crontab -e包含
* * * * * ./startup-script.sh
1 分鍾后,我注意到它創建了一個 hello.txt 文件,但我的 puma 服務器沒有運行。 但是當我使用./startup-script.sh
手動運行它時,它將運行服務器。
puma_error.log
Puma starting in single mode...
* Version 4.3.1 (ruby 2.6.0-p0), codename: Mysterious Traveller
* Min threads: 0, max threads: 8
* Environment: production
* Daemonizing...
=== puma startup: 2020-09-21 05:16:28 +0000 ===
=== puma startup: 2020-09-21 05:17:39 +0000 ===
* Listening on tcp://0.0.0.0:9292
=== puma startup: 2020-09-21 06:11:35 +0000 ===
- Gracefully stopping, waiting for requests to finish
=== puma shutdown: 2020-09-21 06:11:35 +0000 ===
生產日志
Item Load (0.3ms) SELECT "items".* FROM "items" WHERE "items"."id" = $1 LIMIT $2 [["id", 208], ["LIMIT", 1]]
CACHE Unit Load (0.0ms) SELECT "units".* FROM "units" WHERE "units"."id" = $1 LIMIT $2 [["id", 4], ["LIMIT", 1]]
Item Load (0.3ms) SELECT "items".* FROM "items" WHERE "items"."id" = $1 LIMIT $2 [["id", 215], ["LIMIT", 1]]
CACHE Unit Load (0.0ms) SELECT "units".* FROM "units" WHERE "units"."id" = $1 LIMIT $2 [["id", 4], ["LIMIT", 1]]
Item Load (0.2ms) SELECT "items".* FROM "items" WHERE "items"."id" = $1 LIMIT $2 [["id", 198], ["LIMIT", 1]]
CACHE Unit Load (0.0ms) SELECT "units".* FROM "units" WHERE "units"."id" = $1 LIMIT $2 [["id", 10], ["LIMIT", 1]]
Rendering text template
Rendered text template (0.0ms)
Sent data Delivery Receipt 12-2020-001.pdf (0.5ms)
Completed 200 OK in 4326ms (Views: 0.3ms | ActiveRecord: 37.4ms)
puma_access.log
=== puma startup: 2020-12-01 01:07:13 +0000 ===
啟動startup-script.sh
腳本時,我得到了這個
Running Puma Server
Puma starting in single mode...
* Version 4.3.1 (ruby 2.6.0-p0), codename: Mysterious Traveller
* Min threads: 0, max threads: 8
* Environment: production
* Daemonizing...
我的 droplet 仍在運行,但是在端口 9292 上運行的 puma 在崩潰時消失了,所以我懷疑這是 puma 錯誤。
我不認為美洲獅會自己停下來。 也許它沒有作為守護進程正常運行? 我建議弄清楚發生了什么。
話雖如此,為了確保某個守護進程始終啟動並運行,您應該使用像 supervisord 這樣的工具: http : //supervisord.org/
cronjob 不是正確的工具。
根據您的 Puma 版本,可能不推薦使用--daemon
選項。 使用&
代替:
bundle exec puma -C /srv/www/apps/app/shared/puma.rb &
但是,您的問題不在於 Cron 或 Puma。 除非發生其他事情,否則 Puma 不會自行退出。 我建議您查看錯誤日志並找出真正的問題。
要回答您的評論:
我建議您將 Puma 更新為最新的 4.x 版本(當前為 4.3.6)。 如果您可以切換到 Puma 5.x(目前為 5.1),那就更好了。 一直有錯誤修復和改進。 也許您的問題與已解決的錯誤有關。
如果您更新到 5.x 版本系列,則需要將--daemon
替換為&
如上所述。
旁注:
但是,我仍然相信有一個您沒有注意到您的工具集未能記錄的錯誤報告。
我不知道你是否看過Capistrano 日志以及 Rails/Puma 日志和 DigitalOcean 服務器日志。
如果您使用--debug
運行 Puma,一些 Puma 消息必須出現在日志中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.