Question

Database wakes up asleep?

Every morning my database connected to my droplet wakes up asleep, I need to enter the droplet console and run pm2 restart 0, how do I stop it from sleeping


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
August 10, 2024

Hi there,

This is quite interesting as the Droplets do not have that concept of sleeping like some serverless platforms.

Your droplet might be running out of memory or CPU, causing the database to be terminated or put into a killed state by the operating system. What I could suggest here is to check your resource usage with commands like top, htop, or free -m.

And also check the DigitalOcean monitoring graphs to see if this is indeed the case:

https://docs.digitalocean.com/products/monitoring/

If this is indeed the case, you might want to upgrade your Droplet to add some extra RAM and CPU power:

https://docs.digitalocean.com/products/droplets/how-to/resize/

Alternatively, you could also add a SWAP file to have some extra memory buffer:

https://www.digitalocean.com/community/tutorial-collections/how-to-add-swap-space

As a temporary workaround, what you could do is to create a simple cron job that could run every morning to restart the database service:

0 6 * * * pm2 restart 0

Let me know how it goes!

- Bobby

KFSys
Site Moderator
Site Moderator badge
August 12, 2024

Heya @robertomedinack,

It’s possible something is killing your proccess. You can try using the metrics agent that DigitalOcean offers to see the exact reason behind this:

https://docs.digitalocean.com/products/monitoring/how-to/install-agent/

Another alternative would be to check your Droplet’s logs and see if it says about killing proccesses.

Other Solutions might be

Check Application Logs

  • Review your application logs (usually found in /var/log/ or specific to your application) to see if there are any errors or warnings before it crashes or becomes unresponsive.
  • Logs might give you clues about why the application is going to sleep or crashing.

Keep the Application Alive with pm2

  • pm2 has a built-in feature to automatically restart an application if it crashes. Ensure your process is being monitored correctly by pm2:
pm2 start your_app.js --name "your-app" --watch
  • The --watch flag ensures that pm2 will watch your files and restart the process if any file changes. This might help if your application is crashing or going to sleep due to file changes or other issues.

4. Configure pm2 to Restart on Failure

  • Ensure pm2 is set up to restart your application on failure:
pm2 start your_app.js --name "your-app" --restart-delay 10000 --max-restarts 10
  • This configuration attempts to restart your application with a 10-second delay between restarts, up to 10 times if it fails.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.