杰瑞科技汇

Python supervisord如何配置与管理进程?

Of course! Here is a comprehensive guide to using supervisord with Python, covering what it is, why you need it, how to install and configure it, and common use cases.

Python supervisord如何配置与管理进程?-图1
(图片来源网络,侵删)

What is Supervisord?

Supervisord is a client/server system that allows its users to monitor and control a number of processes on UNIX-like operating systems. It's a process control system, often referred to as a "process manager."

Think of it as a forever-runner for your applications. If your Python script crashes, burns, or exits for any reason, Supervisord will automatically restart it.

Key Features:

  • Process Management: Starts, stops, and restarts processes.
  • Automatic Restarts: Restarts processes that exit unexpectedly.
  • Logging: Captures stdout and stderr from managed processes into log files.
  • Concurrency Management: Can run multiple instances of a single program.
  • Configuration-Driven: All behavior is defined in simple INI-style configuration files.
  • Web-Based UI: Provides a simple web interface to monitor and control processes.

Why Use Supervisord for Python Applications?

You need a process manager like Supervisord for any long-running Python application that must be highly available. Common examples include:

Python supervisord如何配置与管理进程?-图2
(图片来源网络,侵删)
  1. Web Applications (Flask, Django, FastAPI): A production web server should never be down. If it crashes, it must come back online immediately.
  2. Background Workers (Celery, RQ, custom scripts): If your worker processing a background job dies, the job is lost. Supervisord ensures it's restarted and can pick up new jobs.
  3. API Services: Any long-running API service needs to be managed.
  4. Chatbots, Bots, or Scrapers: Scripts that need to run 24/7.

Without a process manager, you would have to manually ssh into your server, check if the process is running, and start it again if it's not. This is not scalable or reliable.


Step-by-Step Guide: Setting up Supervisord for a Python App

Let's walk through a complete example: running a simple Flask web application using Supervisord.

Step 1: Create a Simple Python Web Application

First, create a directory for your project and a simple app.py file.

mkdir my_flask_app
cd my_flask_app

app.py:

Python supervisord如何配置与管理进程?-图3
(图片来源网络,侵删)
from flask import Flask
import time
import logging
# Set up basic logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
app = Flask(__name__)
@app.route('/')
def hello():
    logging.info("Hello endpoint was called.")
    return "Hello from a Supervised Flask App!\n"
@app.route('/crash')
def crash():
    logging.info("Crash endpoint called. The application will now exit.")
    # Exit with a non-zero status code to signal an error
    exit(1) 
if __name__ == '__main__':
    # Use 0.0.0.0 to make it accessible from outside the container
    app.run(host='0.0.0.0', port=8000)

Step 2: Install Supervisord

On Debian/Ubuntu:

sudo apt-get update
sudo apt-get install supervisor

On CentOS/RHEL/Fedora:

sudo yum install supervisor
# Or on newer systems:
sudo dnf install supervisor

Using pip (for development/testing only): You can also install it in a virtual environment, but it's highly recommended to install it system-wide as a service.

pip install supervisor

Step 3: Create a Supervisor Configuration File

Supervisor's main configuration file is usually at /etc/supervisor/supervisord.conf. However, it's best practice to create a separate configuration file for each application you want to manage.

  1. Create a new config file for our app. A common place is /etc/supervisor/conf.d/.

    sudo nano /etc/supervisor/conf.d/my_flask_app.conf
  2. Add the following configuration to the file. This is the most important part.

/etc/supervisor/conf.d/my_flask_app.conf:

[program:my_flask_app]
command=/usr/bin/python3 /path/to/your/my_flask_app/app.py ; The command to start the app
directory=/path/to/your/my_flask_app              ; The working directory for the app
user=your_linux_username                        ; The user to run the process as
autostart=true                                   ; Start the program when supervisord starts
autorestart=true                                 ; Restart the program if it exits
startretries=3                                   ; Number of times to start the program if it fails
stderr_logfile=/var/log/supervisor/my_flask_app_err.log ; File for stderr log
stdout_logfile=/var/log/supervisor/my_flask_app_out.log ; File for stdout log
environment=PYTHONUNBUFFERED="1"                ; Important: Ensures logs are sent directly to the log files

Explanation of Key Directives:

  • [program:my_flask_app]: The name of your program as it will appear in Supervisor's UI and CLI.
  • command: The most critical directive. It's the exact command to run your application. Make sure the path to python3 and your app.py is correct.
  • directory: Sets the working directory for the process. This is useful if your app relies on relative file paths.
  • user: The user under which the process will run. This is a security best practice. Don't run your apps as root.
  • autostart=true: Ensures the program starts when supervisord boots.
  • autorestart=true: The core feature. Restarts the program if it exits for any reason.
  • startretries=3: Tries to restart the program 3 times if it fails to start initially.
  • stderr_logfile & stdout_logfile: Redirects standard error and standard output to these files. You can use these for debugging.
  • environment=PYTHONUNBUFFERED="1": Highly recommended for Python apps. It prevents Python's output from being buffered, so your logs appear in the log files immediately.

Note: You must create the log directory and set its permissions if it doesn't exist.

sudo mkdir -p /var/log/supervisor
sudo chown your_linux_username:your_linux_username /var/log/supervisor

Step 4: Tell Supervisord About Your New Configuration

Now that you've created the config file, you need to tell supervisord to read it and start managing your application.

  1. Reread the configuration files:

    sudo supervisorctl reread

    This command tells Supervisor to look for new or changed programs in its configuration files. You should see output like: my_flask_app: available

  2. Update the process group:

    sudo supervisorctl update

    This command adds any newly read programs to the Supervisor process control. You should see: my_flask_app: added process group

Step 5: Start and Manage Your Application

Your application is now configured and known to Supervisor, but it's not running yet. Let's start it.

  1. Start the application:

    sudo supervisorctl start my_flask_app

    Output: my_flask_app: started

  2. Check the status:

    sudo supervisorctl status

    Output should show your app is running:

    my_flask_app            RUNNING   pid 12345, uptime 0:00:15
  3. Test it!

    • Visit http://<your_server_ip>:8000 in your browser. You should see the "Hello..." message.
    • Visit http://<your_server_ip>:8000/crash. This will cause the app to exit.
    • Check the status again immediately:
      sudo supervisorctl status

      You will see that supervisorctl has already restarted it:

      my_flask_app            RUNNING   pid 12399, uptime 0:00:02
    • Check the log file to see the crash message:
      tail -f /var/log/supervisor/my_flask_app_err.log

Common supervisorctl Commands

  • sudo supervisorctl status: Check the status of all programs.
  • sudo supervisorctl start <program_name>: Start a program.
  • sudo supervisorctl stop <program_name>: Stop a program gracefully.
  • sudo supervisorctl restart <program_name>: Restart a program.
  • sudo supervisorctl reread: Reread configuration files.
  • sudo supervisorctl update: Add new/update changed programs.
  • sudo supervisorctl tail -f <program_name> stdout: Follow the log output of a program in real-time.

Advanced: Running Multiple Instances

What if you need to run 4 instances of your Flask app behind a load balancer? You can do this easily with numprocs.

Modify your my_flask_app.conf file:

[program:my_flask_app]
command=/usr/bin/python3 /path/to/your/my_flask_app/app.py
directory=/path/to/your/my_flask_app
user=your_linux_username
autostart=true
autorestart=true
startretries=3
stderr_logfile=/var/log/supervisor/my_flask_app_err.log
stdout_logfile=/var/log/supervisor/my_flask_app_out.log
environment=PYTHONUNBUFFERED="1"
; --- Add these lines ---
numprocs=4
numprocs_start=1
  • numprocs=4: Tells Supervisor to run 4 instances of this program.
  • numprocs_start=1: The first process will be my_flask_app_1, the second my_flask_app_2, and so on.

After updating the file, run sudo supervisorctl update and sudo supervisorctl status. You will now see all 4 processes running.

$ sudo supervisorctl status
my_flask_app_1           RUNNING   pid 12345, uptime 0:05:10
my_flask_app_2           RUNNING   pid 12346, uptime 0:05:10
my_flask_app_3           RUNNING   pid 12347, uptime 0:05:10
my_flask_app_4           RUNNING   pid 12348, uptime 0:05:10

The Web Interface (Supervisor HTML)

Supervisord comes with a simple web interface that allows you to monitor and control processes without using the command line.

  1. Enable it in the main config file: Open /etc/supervisor/supervisord.conf and find the [inet_http_server] section. Uncomment it and configure it.

    [inet_http_server]
    port=*:9001         ; The port to listen on (e.g., 9001)
    username=admin      ; The username for the UI
    password=strongpass ; The password for the UI
  2. Restart Supervisord:

    sudo systemctl restart supervisor
  3. Access it: Open your browser and navigate to http://<your_server_ip>:9001. You'll be prompted for the username and password you just set. Here you can see the status, start, stop, and view logs for all your programs.

分享:
扫描分享到社交APP
上一篇
下一篇