How to Use the Supervisor Command in Linux: A Practical Guide

Managing processes in Linux often requires robust tools to ensure stability and automation. One such tool is supervisord
, a process control system that helps manage and monitor processes in a reliable and organized manner. Here’s a comprehensive guide on how to use supervisord
effectively, along with a practical demo.
Installing Supervisor
Before diving into usage, ensure supervisord
is installed on your system. You can typically install it using your package manager. For example, on Ubuntu, you can use:
sudo apt-get install supervisor
Configuring Supervisor
Once installed, supervisord
needs a configuration file where you define the programs and processes it will manage. The main configuration file is usually located at /etc/supervisor/supervisord.conf
. You can edit this file directly or create separate configuration files in the /etc/supervisor/conf.d/
directory.
Here’s a basic example of a supervisord.conf
file:
[program:myprogram]
command=/path/to/myprogram
autostart=true
autorestart=true
stderr_logfile=/var/log/myprogram.err.log
stdout_logfile=/var/log/myprogram.out.log
Understanding Configuration Options
program: This defines the program or process you want supervisord
to manage.
command: Specifies the command to start your program.
autostart: Controls whether the program starts automatically when supervisord
starts.
autorestart: Determines if the program should automatically restart if it exits.
stderr_logfile, stdout_logfile: Path to error and standard output log files.
Starting Supervisor
Once configured, start supervisord
using: sudo systemctl start supervisor
To ensure supervisord
starts automatically on boot: sudo systemctl enable supervisor
Managing Processes with Supervisor
After supervisord
is running, you can manage processes using the supervisorctl
command-line tool.
Checking Status: supervisorctl status
Starting a Process: supervisorctl start <program_name>
Stopping a Process: supervisorctl stop <program_name>
Restarting a Process: supervisorctl restart <program_name>
Reloading Configuration: supervisorctl reread
supervisorctl update
Demo: Managing a Sample Application
Let’s demonstrate by managing a simple Python web server using supervisord
.
Create a Python HTTP Server Script: Save the following Python script as server.py
:
from http.server import HTTPServer, BaseHTTPRequestHandler
class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.end_headers()
self.wfile.write(b'Hello, world!')
httpd = HTTPServer(('localhost', 8000), SimpleHTTPRequestHandler)
httpd.serve_forever()
Create a Supervisor Configuration File: Create a file named myapp.conf
in /etc/supervisor/conf.d/
:
[program:myapp]
command=python3 /path/to/server.py
autostart=true
autorestart=true
stderr_logfile=/var/log/myapp.err.log
stdout_logfile=/var/log/myapp.out.log
Reload Supervisor and Start the Application:
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start myapp
Manage the Application:
Check status: supervisorctl status
Restart: supervisorctl restart myapp
Stop: supervisorctl stop myapp
Conclusion
supervisord
simplifies process management in Linux environments, providing robust control and monitoring capabilities. By following this guide and exploring further options in supervisord
's documentation, you can effectively streamline the management of various processes on your system. Start using supervisord
today to enhance your Linux server administration experience!