Supervisor: Difference between revisions

From Wildsong
Jump to navigationJump to search
Brian Wilson (talk | contribs)
Created page with "http://supervisord.org Q1. ''How do I set up a Python script so that I can easily start and stop it and monitor whether it's crashed?'' Q2. Can I start multiple instances of..."
 
Brian Wilson (talk | contribs)
mNo edit summary
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
http://supervisord.org
This page attempts to answer these questions.
 
Will Supervisor help me? http://supervisord.org


Q1. ''How do I set up a Python script so that I can easily start and stop it and monitor whether it's crashed?''
Q1. ''How do I set up a Python script so that I can easily start and stop it and monitor whether it's crashed?''
Line 5: Line 7:
Q2. Can I start multiple instances of the same script each with a different configuration?
Q2. Can I start multiple instances of the same script each with a different configuration?


Q3. Is '''Supervisor''' the answer?
== Installing "Supervisor" AKA "Supervisord" ==


== Installation ==
% '''sudo apt-get install supervisor'''
If you want the latest version next do
% '''sudo apt-get remove supervisor'''
% '''sudo easy_install supervisor'''
This trick will cause the Debian/Ubuntu startup files to be left around which you can then edit. Like /etc/init.d/supervisor
I added a user and group for "supervisor".


% '''sudo easy_install supervisor'''
== Configuring Supervisor ==


is better than
sudo mkdir /etc/supervisor
echo_supervisord_conf | sudo tee /etc/supervisor/supervisord.conf


% '''sudo apt-get install supervisor'''
/etc/defaults/supervisor:
DAEMON_OPTS="-c /etc/supervisor/supervisord.conf"


== Configuration ==
sudo mkdir /var/run/supervisor
sudo chown supervisor /var/run/supervisor


sudo mkdir /etc/supervisor
Things I changed in config file
echo_supervisord_conf | sudo tee /etc/supervisor/supervisord.conf
# Change location of logfile to /var/log/supervisord.log
# Change pidfile to /var/run/supervisor/supervisord.pid
# Enable inet_http_server section with for example, port=*.9001
# Set user to 'supervisor'
# Enable reading files from /etc/supervisor/conf.d/ (see 'include')


=== Do nothing test script ===
=== Do nothing test script ===
Line 24: Line 38:
Here is a script that will log to the system log,  
Here is a script that will log to the system log,  
and sleep for 10 minutes. It handles control-C exit.
and sleep for 10 minutes. It handles control-C exit.
It optionally accepts an arg that can be anything and writes it to the log.
This is to simulate running multiple instances with different configurations.


  #/usr/bin/python
  #/usr/bin/python
  import time, logging, logging.handlers
  import time, logging, logging.handlers
instance = 'default'
try:
  instance = sys.argv[1]
except:
  pass
  logging.basicConfig(level=logging.INFO)
  logging.basicConfig(level=logging.INFO)
  log = logging.getLogger(__name__)
  log = logging.getLogger(__name__)
Line 35: Line 56:
  try:
  try:
     while forever:
     while forever:
       log.info("MARK")
       log.info("MARK " + instance)
       time.sleep(60 * 10)  
       time.sleep(60 * 10)  
  except KeyboardInterrupt:
  except KeyboardInterrupt:

Latest revision as of 22:51, 31 March 2014

This page attempts to answer these questions.

Will Supervisor help me? http://supervisord.org

Q1. How do I set up a Python script so that I can easily start and stop it and monitor whether it's crashed?

Q2. Can I start multiple instances of the same script each with a different configuration?

Installing "Supervisor" AKA "Supervisord"

% sudo apt-get install supervisor

If you want the latest version next do

% sudo apt-get remove supervisor
% sudo easy_install supervisor

This trick will cause the Debian/Ubuntu startup files to be left around which you can then edit. Like /etc/init.d/supervisor I added a user and group for "supervisor".

Configuring Supervisor

sudo mkdir /etc/supervisor echo_supervisord_conf | sudo tee /etc/supervisor/supervisord.conf

/etc/defaults/supervisor: DAEMON_OPTS="-c /etc/supervisor/supervisord.conf"

sudo mkdir /var/run/supervisor sudo chown supervisor /var/run/supervisor

Things I changed in config file

  1. Change location of logfile to /var/log/supervisord.log
  2. Change pidfile to /var/run/supervisor/supervisord.pid
  3. Enable inet_http_server section with for example, port=*.9001
  4. Set user to 'supervisor'
  5. Enable reading files from /etc/supervisor/conf.d/ (see 'include')

Do nothing test script

Here is a script that will log to the system log, and sleep for 10 minutes. It handles control-C exit. It optionally accepts an arg that can be anything and writes it to the log. This is to simulate running multiple instances with different configurations.

#/usr/bin/python
import time, logging, logging.handlers
instance = 'default'
try:
  instance = sys.argv[1]
except:
  pass
logging.basicConfig(level=logging.INFO)
log = logging.getLogger(__name__)
handler = logging.handlers.SysLogHandler(address='/dev/log')
log.addHandler(handler)
log.info("Start doing nothing.")
forever = True
try:
   while forever:
      log.info("MARK " + instance)
      time.sleep(60 * 10) 
except KeyboardInterrupt:
   log.info("Interrupt detected.")
log.info("Completed successfully.")
exit(0)