0
0
Raspberry Piprogramming~5 mins

Process management with supervisor in Raspberry Pi

Choose your learning style9 modes available
Introduction

Supervisor helps you keep programs running on your Raspberry Pi. It restarts them if they stop and makes managing many programs easy.

You want a program to start automatically when your Raspberry Pi boots.
You need to keep a script running all the time without manually restarting it.
You want to easily start, stop, or check the status of background programs.
You have multiple programs that should run together and be managed in one place.
Syntax
Raspberry Pi
[program:your_program_name]
command=python3 /path/to/your_script.py
autostart=true
autorestart=true
stderr_logfile=/var/log/your_program.err.log
stdout_logfile=/var/log/your_program.out.log

program: This names your program in Supervisor.

command: The command to run your program.

Examples
This runs a Python script called hello.py automatically and restarts it if it crashes.
Raspberry Pi
[program:hello_world]
command=python3 /home/pi/hello.py
autostart=true
autorestart=true
stderr_logfile=/var/log/hello.err.log
stdout_logfile=/var/log/hello.out.log
This manages a Node.js web server script, keeping it running all the time.
Raspberry Pi
[program:web_server]
command=/usr/bin/node /home/pi/server.js
autostart=true
autorestart=true
stderr_logfile=/var/log/web_server.err.log
stdout_logfile=/var/log/web_server.out.log
Sample Program

This example shows how to set up Supervisor to manage a Python script that blinks an LED on your Raspberry Pi. It will start automatically and restart if it stops.

Raspberry Pi
[program:blink_led]
command=python3 /home/pi/blink_led.py
autostart=true
autorestart=true
stderr_logfile=/var/log/blink_led.err.log
stdout_logfile=/var/log/blink_led.out.log

# To use this:
# 1. Save this config as /etc/supervisor/conf.d/blink_led.conf
# 2. Run 'sudo supervisorctl reread' and 'sudo supervisorctl update'
# 3. Start the program with 'sudo supervisorctl start blink_led'
# 4. Check status with 'sudo supervisorctl status blink_led'
OutputSuccess
Important Notes

After adding or changing Supervisor configs, always run sudo supervisorctl reread and sudo supervisorctl update to apply changes.

Logs help you see what your program is doing or why it stopped. Check the log files if something goes wrong.

You can control programs with sudo supervisorctl start|stop|restart program_name.

Summary

Supervisor keeps your Raspberry Pi programs running smoothly by restarting them if they stop.

You write simple config files to tell Supervisor what to run and how.

Use Supervisor commands to manage your programs easily from the terminal.