Install supervisor on CentOS 7 and setup multiple processes for web apps
First of all, update your server.
To install supervisor, you first need to install easy_install and then supervisor.
After loading the configuration files, we need to create a service in operating system so that we can control supervisor with systemctl. For that create a new file named supervisord.service in /usr/lib/systemd/system folder and you can copy all the codes from https://github.com/Supervisor/initscripts/blob/master/centos-systemd-etcs. You can also find init scripts for other operating systems in the repository.
If you created the service for supervisor, start it with
look for supervisor.conf file in /etc folder. If it's there you successfully installed supervisor. Now we will add a few new processes and to keep our configuration files clean, I suggest that you keep them in a path like /etc/supervisord/conf.d
Now, create supervisord/conf.d folder that will contain your processes and we can start to write one program for supervisor
In supervisor, a seperate program should be defined for each process. For example if you need to run a webserver, a redis instance and a sidekiq instance, you should define 3 programs.
Once you define your programs, you need to list them in a group, then you can start your processes.
Now consider a small Ruby on Rails app running with two processes, webserver and sidekiq. For this app, a simple configuration would be something like below. To see all available options and descriptions, check supervisor.conf file in /etc folder
command=/usr/local/rvm/gems/ruby-2.3.1/wrappers/bundle exec unicorn -c config/unicorn.rb
command=/usr/local/rvm/gems/ruby-2.3.1/wrappers/bundle exec sidekiq -e production -C config/sidekiq.yml
After having a set of defined programs and a group for you app, save demo.conf file and exit.
Now since we added a new program configurations, we need to run supervisor update command. If we would only change some configurations instead of having new ones, we could use supervisor reread command
To relaod the configuration files
# make sure supervisor is running
$ ps aux | grep supervisord
# Reload config and add/remove as necessary
$ sudo supervisorctl update
# Reload configuration files for the running deamon's
$ sudo supervisorctl reread
# See https://github.com/Supervisor/supervisor/issues/714
# for any confusion
# Enter supervisor control
$ sudo supervisorctl
# Now in supervisor
demo_app:demo_app-web-1 RUNNING pid 47089, uptime 1 day, 14:37:34
demo_app:demo_app-sidekiq-1 RUNNING pid 47085, uptime 1 day, 14:37:34
# If your app is not running, you can check the log file that you have defined in demo.conf
# To see the status of one specific app you can
supervisor> status demo_app:*
supervisor> status demo_app:demo_app-web-1
# You can restart/start/stop your apps same way like
supervisor> restart demo_app:*
supervisor> stop demo_app:*
supervisor> start demo_app:*