Use Supervisor to manage the services in Linux

Bhavnesh Sharma
3 min readJun 26, 2024

--

Supervisor is a powerful process control system that can be used to manage and monitor processes on Unix-like operating systems. This guide walks you through the installation and configuration of Supervisor on a CentOS system, ensuring your services run smoothly and reliably.

Step 1: Installing Supervisor.

To install Supervisor on a CentOS system, you can use the yum package manager. Follow these steps:

1. Update Your Package Index: This ensures you’re installing the latest version of Supervisor.

yum update -y 

2. Install Supervisor:

yum install supervisor

Step 2: Create a Directory for Supervisor Service Configurations.

Creating a dedicated directory for your service configuration files helps keep your configurations organized and easily accessible.

mkdir /etc/supervisor/supervisor_services

Step 3: Create a Service Configuration File.

Next, create a configuration file for your service and place it in the directory you just created. For example, let’s create a configuration file named example.com.conf:

vim /etc/supervisor/supervisor_services/example.com.conf

In this file, define the configuration for your service. Here’s an example configuration:

[program:example.com]
command=php /home/examplecom/public_html/artisan queue:work --tries=3
autostart=true
autorestart=true
stderr_logfile=/var/log/example.com.err.log
stdout_logfile=/var/log/example.com.out.log

Step 4: Include the Directory in the Main Supervisor Configuration File.

Open the Supervisor main configuration file (‘supervisord.conf’) for editing:

vim /etc/supervisord.conf

Add the following line under the [include] section to include the supervisor_services directory:

[include]
files = /etc/supervisor/supervisor_services/*.conf

Save the file and exit the editor.

Step 5: Start Supervisor.

Now, let’s start Supervisor to apply the changes:

systemctl start supervisor
systemctl status supervisor

With this setup, Supervisor will include all configuration files (*.conf) from the supervisor_services directory, keeping your configuration organized and separate from the main supervisord.conf file.

Step 6: Verifying Configuration and Managing Services.

To ensure everything is set up correctly and manage your services effectively, follow these steps:

Check Supervisor’s Status

Verify if the Supervisor is running without any errors:

systemctl status supervisor

Manage Your Service Using Supervisor CLI

  • Check the Service Status:
supervisorctl status

This command displays the status of all programs managed by Supervisor, including your service.

  • Start or Restart Your Service:
supervisorctl start example.com
supervisorctl restart example.com

These commands start or restart your service according to the defined configuration.

  • Check the Log Files:
tail -f /var/log/example.com.err.log
tail -f /var/log/example.com.out.log

Check the specified log files to monitor any output or errors generated by your script.

  • Stopping a Service:

To stop a running service managed by Supervisor, you can use the ‘supervisorctl’ command-line tool:

supervisorctl

Once inside the interface, stop the service by specifying its name:

> stop example.com
> status
> exit

By following these steps, you can effectively organize, manage, and monitor your services using Supervisor, ensuring the seamless operation of your system.

Adding Additional Processes to Supervisor

Once you have set up Supervisor and configured your initial processes, you may find the need to add more processes as your system grows. Here’s how you can do that:

Create a New Configuration File

Similar to the initial setup, create a new configuration file for your additional process. For example, let’s add a process for handling email notifications. Create a configuration file named ‘email_notifications.conf’:

vim /etc/supervisor/supervisor_services/email_notifications.conf

In this file, define the configuration for your new process. Here’s an example:

[program:email_notifications]
command=/usr/bin/python3 /path/to/email_notification_script.py
autostart=true
autorestart=true
stderr_logfile=/var/log/email_notifications.err.log
stdout_logfile=/var/log/email_notifications.out.log

Adjust the command and file paths according to your specific requirements.

Reread and Reload Supervisor

After adding the new configuration file, reread and reload Supervisor to apply the changes:

supervisorctl reread
supervisorctl update

Verify and Manage the New Process

Use the Supervisor’s command-line interface to verify the status of the new process and manage it as needed:

supervisorctl status

Additional Supervisor Commands

Here are some additional commands that can help you manage Supervisor and its processes more effectively:

  • Shutdown Supervisor:
supervisorctl shutdown

This command stops all managed processes and terminates Supervisor itself.

  • Start All Processes:
supervisorctl start all

This command starts all processes listed in the Supervisor configuration.

  • Stop All Processes:
supervisorctl stop all

This command stops all currently running processes managed by Supervisor.

These additional commands provide further insight and control over Supervisor and its managed processes.

By following this comprehensive guide, you can ensure that your services are well-managed, monitored, and maintained, providing a reliable and efficient environment for your applications.

--

--