Ruby on rails Cron不';t在Ubuntu中自动运行Rails Puma服务器
我使用Capistrano在Digital Ocean Ubuntu服务器上部署了一个Rails应用程序。由于某种原因,puma服务器突然停止。我不知道原因是什么 这就是为什么我创建了一个脚本/home/deploy/startup script.sh 该脚本包含在守护进程中启动Rails应用程序的命令Ruby on rails Cron不';t在Ubuntu中自动运行Rails Puma服务器,ruby-on-rails,ubuntu,cron,puma,Ruby On Rails,Ubuntu,Cron,Puma,我使用Capistrano在Digital Ocean Ubuntu服务器上部署了一个Rails应用程序。由于某种原因,puma服务器突然停止。我不知道原因是什么 这就是为什么我创建了一个脚本/home/deploy/startup script.sh 该脚本包含在守护进程中启动Rails应用程序的命令 #!/bin/bash echo "Running Puma Server" touch hello.txt cd /srv/www/apps/app/current bu
#!/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_错误日志
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 startup: 2020-12-01 01:07:13 +0000 ===
生产日志
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 startup: 2020-12-01 01:07:13 +0000 ===
puma\u访问日志
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 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...
我的液滴仍在运行,但9292端口上运行的puma在崩溃时消失,因此我怀疑这是puma错误。我认为puma不会自动停止。也许它没有作为守护进程正常运行?我建议你弄清楚到底发生了什么 也就是说,为了确保某个守护进程始终处于启动和运行状态,您应该使用类似supervisord的工具:
cronjob不是合适的工具。根据Puma的版本,可能不推荐使用
--daemon
选项。改用和:
bundle exec puma -C /srv/www/apps/app/shared/puma.rb &
然而,你的问题不在于Cron或Puma。美洲狮不会自行退出,除非发生其他事情。我建议您查看一下错误日志,找出真正的问题所在
编辑:
要回答您的评论:
我建议您将Puma更新到最新的4.x版本(目前为4.3.6)。如果你能切换到Puma5.x(目前为5.1),那就更好了。一直都有bug修复和改进。也许您的问题与已解决的bug有关
如果您更新到5.x版本系列,则需要将--daemon
替换为上面提到的&
旁注:
然而,我仍然相信有一个错误报告,您没有注意到您的工具集未能记录
我不知道您是否查看了以及Rails/Puma日志和DigitalOcean服务器日志
如果使用--debug
运行Puma,则日志中必须显示一些Puma消息。我当前使用的是Puma 4.3.1。我检查了日志,没有发现任何错误。我的应用程序正在端口9292中运行。我不确定这次坠机的原因是什么。如果我的端口9292关闭了,我可以运行shell脚本吗?您可能看不到任何错误,但是日志仍然会改进您的问题,因为它们允许我们排除不相关的问题。此外,请确保发布所有不同日志的片段,用于部署、应用程序和服务器(droplet)启动/关闭。水滴正在重新启动,很高兴知道。我在问题中添加了日志。puma_error.log显示它已正常关闭。我的应用程序在部署后首先运行,但一段时间后我的应用程序崩溃。我检查了我的日志(puma_access.log、puma_error.log、production.log),没有发现任何错误。如果我的应用程序崩溃,有没有办法运行脚本。我的应用程序正在端口9292上运行。从评论中可以看出,Puma可能由于停止信号而停止(它收到了一个信号,指示它应该退出,这启动了正常的关机)。。。这可能是由于不同的原因造成的。您可能需要查看您的脚本/代码,以查看是否正在发送SIGINT
或SIGTERM
,将内存消耗限制在非常低的限制上,或指示脚本结束后必须退出所有已处理的子级。我检查了我的代码,但没有发送任何SIGINT
或SIGTERM
。如果我可以连续监听端口9292,如果它没有运行,那么运行我的startup-script.sh,还有其他方法吗?你的问题真的不是Puma。我想这只会在部署期间发生。。。?你看过Capistrano的记录了吗?在你的问题中我看不到他们。