Operating System Ubuntu

How to Install and Configure Supervisor on Ubuntu 26.04 LTS

Install Supervisor from the Ubuntu package, put a worker under its control, manage it with supervisorctl, enable the web interface safely, and see when a systemd unit is simpler.

Supervisor starts long-running programs, restarts them when they crash, captures their output and lets you control them with one command. This guide installs it on Ubuntu 26.04 LTS from the distribution package, puts a worker process under its control, shows the supervisorctl commands you will use every day, and enables the optional web interface safely. The steps are the same on Ubuntu 24.04 LTS, which ships Supervisor 4.2.5 instead of 4.3.0.

Ubuntu 16.04 is long out of standard support, and tutorials from that time install Supervisor with easy_install and write their own init script. Neither is needed today: the supervisor package comes with a systemd service that starts the daemon at boot.

Requirements

  • Ubuntu 26.04 LTS or 24.04 LTS.
  • A user with sudo rights. The examples run the managed program as the unprivileged user deploy.
  • A program to supervise. It must run in the foreground: Supervisor manages the processes it starts itself, so anything that forks into the background and detaches is lost to it. If your program has a "daemonize" option, turn it off.

Step 1: Install Supervisor

sudo apt update
sudo apt install supervisor

The package starts the service and enables it at boot. Confirm that:

systemctl status supervisor --no-pager
sudo supervisorctl version

Supervisor has two parts. supervisord is the daemon that starts and watches your programs; supervisorctl is the client that talks to it over a UNIX socket. On Ubuntu the files are laid out like this:

PathPurpose
/etc/supervisor/supervisord.confMain configuration of the daemon
/etc/supervisor/conf.d/*.confOne file per program or group; included by the main file
/var/log/supervisor/Daemon log and, by default, the output of managed programs

The upstream project also publishes Supervisor on PyPI. On a current Ubuntu, pip refuses to install into the system Python, so use the package unless you need a newer release and are prepared to run it from a virtual environment with your own service unit.

Step 2: Create a program to supervise

If you already have a worker, skip to step 3 and use its command. Otherwise create a small test script that prints a line every few seconds:

sudo mkdir -p /opt/worker
sudo tee /opt/worker/worker.py > /dev/null <<'EOF'
import os, time

while True:
    print("worker", os.getpid(), "alive, APP_ENV =", os.environ.get("APP_ENV"), flush=True)
    time.sleep(5)
EOF
sudo chown -R deploy:deploy /opt/worker

Step 3: Add the program to Supervisor

Create a configuration file for it. The file name is up to you; the extension must be .conf.

sudo nano /etc/supervisor/conf.d/worker.conf
[program:worker]
command=/usr/bin/python3 /opt/worker/worker.py
directory=/opt/worker
user=deploy
autostart=true
autorestart=true
startsecs=5
startretries=3
stopsignal=TERM
stopwaitsecs=30
environment=APP_ENV="production"
stdout_logfile=/var/log/supervisor/worker.out.log
stderr_logfile=/var/log/supervisor/worker.err.log
stdout_logfile_maxbytes=20MB

What the options do:

  • command — the only required option. Use absolute paths; Supervisor does not run the command through a shell, so pipes and redirections do not work here.
  • user — the account the process runs as. Switching users works because supervisord itself runs as root.
  • autostart — start the program when supervisord starts (the default).
  • autorestart — true restarts the process whenever it exits; the default unexpected restarts it only when the exit code is not one of the expected ones; false never restarts it.
  • startsecs and startretries — the process must stay up for startsecs seconds to count as started. After startretries failed attempts in a row it is marked FATAL and left alone.
  • stopsignal and stopwaitsecs — the signal sent on stop, and how long Supervisor waits before it sends SIGKILL. Give workers enough time to finish their current job.
  • environment — extra environment variables as KEY="value",KEY2="value2".

Load the new file. reread reports what changed on disk; update applies it and starts the program:

sudo supervisorctl reread
sudo supervisorctl update

Step 4: Verify and control the program

sudo supervisorctl status

After a few seconds the worker should be listed as RUNNING with a PID and an uptime. Follow its output:

sudo supervisorctl tail -f worker

Press Ctrl+C to stop following. To see the automatic restart work, kill the process and check the status again; it comes back with a new PID:

sudo kill $(sudo supervisorctl pid worker)
sleep 2
sudo supervisorctl status worker

The commands you will use most:

CommandEffect
supervisorctl statusState of all programs
supervisorctl start|stop|restart workerControl one program; use all for every program
supervisorctl rereadRead the configuration files and list changes without applying them
supervisorctl updateApply configuration changes: add, remove and restart only the affected programs
supervisorctl tail -f worker stderrFollow the error output of a program
supervisorctl reloadRestart supervisord itself, and with it every program

One trap: restart does not reread configuration files. After editing a file in conf.d, always run reread and update.

Running several copies of a program

Queue workers usually run as several identical processes. Set numprocs and give every copy its own name with process_name, which is mandatory as soon as numprocs is greater than 1:

[program:worker]
command=/usr/bin/python3 /opt/worker/worker.py
process_name=%(program_name)s_%(process_num)02d
numprocs=4
user=deploy
autorestart=true
stdout_logfile=/var/log/supervisor/%(program_name)s_%(process_num)02d.out.log
redirect_stderr=true

After reread and update, the copies appear as worker:worker_00 to worker:worker_03. Address all of them at once as a group:

sudo supervisorctl restart worker:*

If your program starts child processes of its own, add stopasgroup=true and killasgroup=true so that stop signals reach the whole process group and no orphans are left behind.

Optional: the web interface

Supervisor includes a small web page that shows process states and has start, stop and log buttons. It is switched off by default. It has no encryption, and the Supervisor documentation warns against exposing it to the internet, so bind it to the loopback address only and reach it through an SSH tunnel.

Create a separate configuration file for it:

sudo nano /etc/supervisor/conf.d/http.conf
[inet_http_server]
port=127.0.0.1:9001
username=admin
password=replace-with-a-long-random-password

The file contains a password in clear text, so make it readable by root only. A change to the daemon's own sections needs a restart of the service, which also restarts every managed program:

sudo chmod 600 /etc/supervisor/conf.d/http.conf
sudo systemctl restart supervisor

On your local computer, open a tunnel and then browse to http://127.0.0.1:9001:

ssh -L 9001:127.0.0.1:9001 deploy@203.0.113.10

When a systemd unit is the simpler choice

Ubuntu already runs a process supervisor as PID 1. For a single service, a systemd unit does everything shown above without an extra daemon in between:

[Unit]
Description=Example worker
After=network-online.target
Wants=network-online.target

[Service]
User=deploy
WorkingDirectory=/opt/worker
ExecStart=/usr/bin/python3 /opt/worker/worker.py
Environment=APP_ENV=production
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target

Saved as /etc/systemd/system/worker.service, it is started with sudo systemctl daemon-reload followed by sudo systemctl enable --now worker, and its output lands in the journal (journalctl -u worker -f). systemd also gives you dependency ordering, resource limits and sandboxing options that Supervisor does not have.

Supervisor still earns its place when:

  • you manage many similar worker processes per application and want to scale them with one numprocs line and restart them as a group;
  • the application's documentation and deployment tooling are written around it, as is the case for several queue systems;
  • you want the same process configuration to work inside containers or on systems without systemd.

For one or two daemons on a VPS, prefer systemd.

Troubleshooting

  • FATAL or BACKOFF in the status output. The program exits before startsecs has passed. Read its error output with sudo supervisorctl tail worker stderr and run the exact command by hand as the configured user: sudo -u deploy /usr/bin/python3 /opt/worker/worker.py.
  • unix:///var/run/supervisor.sock no such file. The daemon is not running. Check systemctl status supervisor and /var/log/supervisor/supervisord.log; a syntax error in a file under conf.d prevents it from starting.
  • Permission denied when running supervisorctl. The control socket is only accessible to root on Ubuntu; use sudo.
  • A program keeps running after stop. It daemonized itself or started children. Run it in the foreground and set stopasgroup=true.

Frequently asked questions

Does Supervisor start my programs after a reboot?

Yes. systemd starts supervisord at boot because the package enables supervisor.service, and supervisord starts every program that has autostart=true, which is the default.

How do I remove a program?

Delete its file from /etc/supervisor/conf.d/ and run sudo supervisorctl reread followed by sudo supervisorctl update. Supervisor stops the process and drops it from its list.

Can I run a program through a shell, for example to use a pipe?

Wrap it: command=/bin/sh -c "exec /opt/app/run.sh 2>&1". The exec matters, because it replaces the shell with your program so that signals from Supervisor reach the program rather than the shell.

Why does my worker's output show up late in the log?

Many runtimes buffer standard output when it is not a terminal. Flush explicitly, or switch buffering off, for example with python3 -u or the environment variable PYTHONUNBUFFERED=1.

Share story:

Continue reading other guides on How VPS.