How to ensure that a service is always running on Centos?

Asked

Viewed 611 times

12

I created a startup script for my application in Centos, so I can use the commands below to initialize/stop the application:

service django_app start
service django_app stop

How do I ensure this service is always running?

For example, if there is an error in the application and the service is terminated, there is some way to restart the service automatically in Centos?

  • One solution is to create a cronjob that runs every X minutes, and runs a script that checks and resets if needed.

  • A command of the type ps aux | grep "django_app" can help verify if the application is among the running processes.

3 answers

4


look for Monit, a simple and functional orchestrator:

example of config:

check process meuapp with pidfile /var/vcqescolhe/app.pid
start program = "/etc/init.d/app  start" uid 500 gid 499
stop program = "/etc/init.d/app stop" uid 500 gid 499
if failed host 127.0.0.1 port 8080 then restart
if failed host localhost port 8080
    protocol HTTP request "/meuapp/beheappy/smileinsignificante.jpg" with timeout     10 seconds then restart

this monitor can even send emails... is faster than a Nouagios or Zabbix

  • I forgot his website.. http://mmonit.com/monit/

2

A much simpler way than Monit, as commented by J. Bruni in his question, is to make a script.

vi /Xyz/django_app_verify.sh

#!/bin/bash
if [ ! "$(pidof django_app)" ] 
then
  /xyz/comandoParaIniciarSeu_django_app &
fi

Then add to crontab

* * * * * /xyz/django_app_verify.sh > /dev/null

So it runs every minute. This solution is more frequent than it seems.

1

Another alternative is the Supervisor!

pip install supervisor

Then you create a configuration file with the programs that will be monitored. A minimal example would be:

[program:foo]
command=/bin/cat

The official tutorial is on that page.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.