Process Management with Supervisor on Raspberry Pi
📖 Scenario: You have a Raspberry Pi running several small programs. You want to make sure these programs start automatically and keep running even if they crash. Supervisor is a tool that helps manage these programs easily.
🎯 Goal: You will create a Supervisor configuration file to manage a simple Python script as a process. You will set up the configuration, start Supervisor, and check the process status.
📋 What You'll Learn
Create a Supervisor configuration file for a Python script
Set up a program section with the correct command and settings
Reload Supervisor to apply the configuration
Check the status of the managed process
💡 Why This Matters
🌍 Real World
Supervisor helps keep important programs running on devices like Raspberry Pi without manual restarts.
💼 Career
Understanding process management with Supervisor is useful for system administrators and developers managing services on Linux-based systems.
Progress0 / 4 steps
1
Create a simple Python script
Create a Python script called hello.py that prints "Hello from Supervisor!" and then waits for 60 seconds. Write exactly these two lines in the script.
Raspberry Pi
Hint
Use print() to show the message and time.sleep(60) to pause.
2
Create Supervisor configuration file
Create a Supervisor configuration file named hello.conf with a [program:hello] section. Set command=python3 /home/pi/hello.py, autostart=true, and autorestart=true exactly as shown.
Raspberry Pi
Hint
The file must start with [program:hello] and include the exact command and options.
3
Reload Supervisor to apply configuration
Run the command sudo supervisorctl reread followed by sudo supervisorctl update to reload Supervisor and apply the new hello.conf configuration.
Raspberry Pi
Hint
Use these two commands exactly to reload Supervisor's configuration.
4
Check the status of the managed process
Run the command sudo supervisorctl status hello to check if the hello program is running under Supervisor.
Raspberry Pi
Hint
This command shows if the program is running or stopped.
Practice
(1/5)
1. What is the main purpose of using supervisor on a Raspberry Pi?
easy
A. To update the Raspberry Pi operating system
B. To install new software packages
C. To manage network connections
D. To automatically restart programs if they stop unexpectedly
Solution
Step 1: Understand Supervisor's role
Supervisor is a tool designed to keep programs running by restarting them if they crash or stop.
Step 2: Compare options with Supervisor's function
Updating the operating system, managing network connections, or installing software packages are tasks Supervisor does not do.
Final Answer:
To automatically restart programs if they stop unexpectedly -> Option D
app1 should run automatically but NOT restart on crash (autorestart=false). app2 should run automatically and restart on crash (autorestart=true).
Step 2: Match config options to requirements
[program:app1]
command=python3 /home/pi/app1.py
autostart=true
autorestart=false
[program:app2]
command=python3 /home/pi/app2.py
autostart=true
autorestart=true sets autostart=true for both, autorestart=false for app1, and autorestart=true for app2, matching the requirements exactly.