Rolling restart of process group in monit - monitoring

Does anyone have any suggestions of how I might go about achieving a rolling restart of a process group using monit?
Thanks in advance,
fturtle

I'm not sure for which server you are talking about. But I can provide you an example for thin which supports rolling restart itself. (option onebyone: true)
So for monit you can use something like,
if ... then exec '/path/to/thin_restart.sh'
And thin_restart.sh would be something like,
source /path/to/scripts/rvm
rvm use your_gemset#some_ruby
thin -C thin.yml restart
And contents of thin.yml would look like,
port: 1337
pid: tmp/pids/thin.pid
rackup: /path/to/config.ru
daemonize: true
servers: 2
onebyone: true
There are other ways to fine tune this restarts based on pid. You can monitor files with pids and restart only those thin process based on conditions.
e.g.
check process app-1337
with pid /path/to/app.1337.pid
start = 'thin -d -p 1337 start'
stop = 'thin -d -p 1337 -P /path/to/thin.1337.pid stop'
if cpu usage > 50% then restart
check process app-1338
with pid /path/to/app.1338.pid
start = 'thin -d -p 1338 start'
stop = 'thin -d -p 1338 -P /path/to/thin.1338.pid stop'
if cpu usage > 50% then restart
The other way would be of using groups which monit provides.
Extending above example.
check process app-1337
with pid /path/to/app.1337.pid
group thin
group thin-odd
start = 'thin -d -p 1337 start'
stop = 'thin -d -p 1337 -P /path/to/thin.1337.pid stop'
if cpu usage > 50% then restart
check process app-1338
with pid /path/to/app.1338.pid
group thin
group thin-even
start = 'thin -d -p 1338 start'
stop = 'thin -d -p 1338 -P /path/to/thin.1338.pid stop'
if cpu usage > 50% then restart
check process app-1337
with pid /path/to/app.1339.pid
group thin
group thin-odd
start = 'thin -d -p 1339 start'
stop = 'thin -d -p 1339 -P /path/to/thin.1339.pid stop'
if cpu usage > 50% then restart
check process app-1340
with pid /path/to/app.1340.pid
group thin
group thin-even
start = 'thin -d -p 1340 start'
stop = 'thin -d -p 1340 -P /path/to/thin.1340.pid stop'
if cpu usage > 50% then restart
So now you can do following to restart all:
monit -g thin restart
or to achieve sort of rolling restart, restart odd ones then even.
To restart only odd ones:
monit -g thin-odd restart
and to restart even:
monit -g thin-even restart

Related

reboot script to start rails server

My rails server is running under a deployer user and I normally start it like this
#restart.sh
#!/bin/bash
sudo -u deployer -H bash -l
cd /var/www/html/cms/
./server -e staging start
So when I login over SSH i run ./restart.sh and that works great.
I'm trying to do this automatically after a reboot so I've added a reboot_site.service in
/etc/systemd/system/reboot_site.service
# /etc/systemd/system/reboot_site.service
[Unit]
Description=reboot_site
After=syslog.target network.target
[Service]
Type=simple
WorkingDirectory=/var/www/html/cms
# If you use rbenv:
# ExecStart=/bin/bash -lc '/home/deploy/.rbenv/shims/bundle exec sidekiq -e production'
# If you use the system's ruby:
ExecStart=/home/ec2-user/restart.sh
[Install]
WantedBy=multi-user.target
The service is enabled using sudo systemctl enable reboot_site.service
However when I now reboot the server the script stops execution after the following line
sudo -u deployer -H bash -l
Server is an AWS EC2 AIM instance
What's wrong with my configuration to make this work and how can I fix this?
It stops on sudo -u deployer -H bash -l because your script just changes user and will not continue until you logout as this user. So the remaining commands in your script are not executed.
I would suggest the following version which uses runuser:
#!/bin/bash
runuser -l deployer -c 'cd /var/www/html/cms/ && ./server -e staging start'
if you just need to run a script on startup you can configure a crontab for this
#crontab -e
#reboot /home/ec2-user/restart.sh

Restart monit automatically after it has been killed by the system

I am using monit to monitor my WEB service. Everything went OK until monit process was killed by the system. There is a part of monit log file showing the issue
To have monit automatically launched at startup and restarted on failure I added upstart config for monit (I am running Ubuntu 14.04), that looks like this:
# This is an upstart script to keep monit running.
# To install disable the old way of doing things:
#
# /etc/init.d/monit stop && update-rc.d -f monit remove
#
# then put this script here:
#
# /etc/init/monit.conf
#
# and reload upstart configuration:
#
# initctl reload-configuration
#
# You can manually start and stop monit like this:
#
# start monit
# stop monit
#
description "Monit service manager"
limit core unlimited unlimited
start on runlevel [2345]
stop on starting rc RUNLEVEL=[016]
expect daemon
respawn
exec /usr/bin/monit -c /etc/monitrc
pre-stop exec /usr/bin/monit -c /etc/monitrc quit
When I reboot the system monit is not running.
sudo monit status
$ monit: Status not available -- the monit daemon is not running
How can I configure upstart to keep monit running and monitored?
I had the wrong path to the config file in the start monit command. The correct commands are
exec /usr/bin/monit -c /etc/monit/monitrc
pre-stop exec /usr/bin/monit -c /etc/monit/monitrc quit
It starts monit as daemon and restarts when I kill it:
ps aux | grep monit
root 2173 0.0 0.1 104348 1332 ? Sl 04:13 0:00 /usr/bin/monit -c /etc/monit/monitrc
sudo kill -9 2173
ps aux | grep monit
root 2184 0.0 0.1 104348 1380 ? Sl 04:13 0:00 /usr/bin/monit -c /etc/monit/monitrc

Starting multiple services in docker entrypoint

I am trying to start a few services in my container when it stars.
This is my entry_point script:
#!/bin/bash
set -e
mkdir -p /app/log
tail -n 0 -f /var/log/*.log &
tail -n 0 -f ./log/current.log &
# Start Gunicorn processes
#echo Starting Nginx.
#exec /etc/init.d/nginx start
echo Starting Gunicorn.
exec gunicorn app.main:app \
--name price_service \
-c config/gunicorn.conf \
"$#"
What i would like todo is to uncomment this line :
#exec /etc/init.d/nginx start
But at startup the container just hangs here.
Any solutions ?
You should read about http://man7.org/linux/man-pages/man3/exec.3.html to see what it is doing.
What you need is to either to background the process using & (not recommended), or use a process manager or init system (see Can I run multiple programs in a Docker container?, also not really recommended).
Or you can run multiple containers, and use docker-compose to manage them (recommended).

Shut down rails server once it's been daemonized?

In Ubuntu, I can run a rails server in the background as a daemon by passing in the --daemon option;
bundle exec puma -e production -b unix:///var/run/my_app.sock --daemon
However, how do I gracefully shut this daemonized process down? It's not a simple matter of crtl + c anymore :)
It is better to use puma control pumactl, it processes monitor and controller.
and then you can use it like this to stop
bundle exec pumactl -P /var/run/puma.pid stop
OR
pumactl -C unix://var/run/my_app_pumactl.sock [status|restart|halt|stop]

Can't start thin server from bash script

I'm trying to start my Rails app with thin from the shell script. If I run the commands manually, everything works fine, but if I run the script, it just won't work.
#!/bin/bash
cd /path/to/my/project_1
thin -e production -p 3000 --daemonize -s 10 start
cd /path/to/my/project_2
thin -e production -p 3010 --daemonize -s 10 start
What am I doing wrong? Thanks.
This question was solved in the comments, but for posterity’s sake:
Make sure you have execute permission on the script. chmod 755 my_awesome_script ought to do it.

Resources