Search code examples
ruby-on-railsubuntucronpuma

Cron doesn't run Rails Puma server automatically in Ubuntu


I deployed a Rails application in Digital Ocean Ubuntu Server using Capistrano. For some reason the puma server suddenly stops. I'm not sure what's the reason.

That's why I created a script /home/deploy/startup-script.sh the script contains the commands to startup the Rails application in daemon

#!/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

then I run this script every minute using Cron. My crontab -e contains

* * * * * ./startup-script.sh

After 1 minute I noticed that it created a hello.txt file but my puma server is not running. But when I run it manually using ./startup-script.sh it will run the server.

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 ===

production.log

 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 ===

When starting the startup-script.sh script I'm getting this

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...

My droplet is still running but the puma running on port 9292 disappears when crashes, so I suspect this is puma error.


Solution

  • Depending on your version of Puma, the --daemon option might be deprecated. Use & instead:

    bundle exec puma -C /srv/www/apps/app/shared/puma.rb &
    

    However, your issue isn't with Cron or with Puma. Puma doesn't exit on its own unless something else happens. I recommend that you have a look at the error logs and figure out the real issue.

    Edit:

    To answer your comment:

    1. I recommend that you update Puma to version the latest 4.x release (currently 4.3.6). If you can switch to Puma 5.x (currently 5.1) that would be even better. There are bug fixes and improvements all the time. Maybe your issue is related to a solved bug.

    2. If you update to the 5.x version family, you will need to replace the --daemon with a & as mentioned above.

    Side Note:

    However, I still believe there's an error report you didn't notice of that your toolset failed to log.

    I don't know if you looked at the Capistrano logs as well as the Rails / Puma logs and the DigitalOcean server logs.

    If you're running Puma with --debug, some Puma messages must appear in the log.