Supervisor: A Process Control System & Setup for Laravel application

Supervisor: A Process Control System & Setup for Laravel application

In software development, it's common to have multiple processes running simultaneously to keep a system functional. A process control system is a useful tool to manage these processes. One such system is Supervisor, a popular tool for process control on Linux systems.

In this article, we will discuss Supervisor, its features, and how to get started with it on Linux.

What is Supervisor?

Supervisor is a process control system that allows the user to monitor and control several processes on a Linux system. It can start, stop, and restart these processes when necessary.

One of the main advantages of using Supervisor is its ability to manage the processes automatically. It can ensure that a process is running, and if it stops unexpectedly, Supervisor will restart it. This feature ensures that the system is always functional.

Supervisor Features

Supervisor comes with several features that make it a powerful tool for managing processes. Some of these features include:

  1. Automatic Process Restart: As mentioned earlier, Supervisor can automatically restart a process if it stops unexpectedly.

  2. Process Group Management: Supervisor allows the user to manage a group of processes as a single unit.

  3. Process Priority: The user can set a priority for a process, which will determine the order in which processes are started and stopped.

  4. Logging: Supervisor provides detailed logging of processes, which can be useful for debugging.

  5. Web Interface: Supervisor has a web interface that allows the user to monitor and control processes from a web browser.

How to Get Started with Supervisor

Now that we've discussed what Supervisor is and some of its features, let's get started with using it on a Linux system.

Installation

Supervisor is available on most Linux distributions, and can be installed using the package manager.

On Ubuntu or Debian, run the following command:

sudo apt-get install supervisor

On CentOS or RHEL, run the following command:

sudo yum install supervisor

Configuration

Make log file

sudo touch /var/www/html/storage/logs/worker.log

Create a conf file in /etc/supervisor/conf.d

sudo touch laravel-worker.conf

Now edit the laravel-worker.conf

sdo nano /etc/supervisor/conf.d/laravel-worker.conf

Here is an example of a Laravel worker configuration file:

[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/html/artisan queue:work --sleep=3 --tries=3 --max-time=3600
user=www-data
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
numprocs=2
redirect_stderr=true
stdout_logfile=/var/www/html/storage/logs/worker.log
stopwaitsecs=3600

Note: Do not forget to add the correct path for artisan and worker.log

Explanation

The first line defines the program name as "laravel-worker". This program_name is used as a reference for other parameters.

The "process_name" parameter sets the name for the process that is spawned by supervisor. The "%(program_name)s" specifies that the name should be the same as the program name, and the "%(process_num)02d" is a format string that pads the process number with zeros.

The "command" parameter specifies the command that should be run for this program. In this case, it runs the "queue:work" command of the Laravel framework using the "php" binary, with options to sleep for 3 seconds between jobs, try each job 3 times, and stop after 1 hour (3600 seconds).

The "user" parameter specifies the user under which this program should be run. In this case, it is run as the "www-data" user.

The "autostart" parameter specifies whether the program should be automatically started when supervisor starts. In this case, it is set to true.

The "autorestart" parameter specifies whether the program should be automatically restarted if it exits. In this case, it is set to true.

The "stopasgroup" parameter specifies whether the process should be stopped as a group. In this case, it is set to true.

The "killasgroup" parameter specifies whether the process should be killed as a group. In this case, it is set to true.

The "numprocs" parameter specifies how many instances of this program should be run. In this case, it is set to 2.

The "redirect_stderr" parameter specifies whether the program's standard error output should be redirected to the supervisor log. In this case, it is set to true.

The "stdout_logfile" parameter specifies where the program's standard output should be logged. In this case, it is set to "/var/www/html/storage/logs/worker.log".

The "stopwaitsecs" parameter specifies how many seconds supervisor should wait for the process to stop before forcibly killing it. In this case, it is set to 3600 seconds (1 hour).

Starting Supervisor

After editing the configuration file, Supervisor needs to be started or restarted for the changes to take effect.

On Ubuntu or Debian, run the following command:

sudo service supervisor restart

On CentOS or RHEL, run the following command:

sudo systemctl restart supervisord

Using the Web Interface

Supervisor's web interface can be accessed from a web browser. The default port for the web interface is 9001.

To access the web interface, enter the following URL in your web browser:

http://localhost:9001

The web interface displays a list of managed processes and their current status. The user can start, stop, or restart a process from the web interface.

Checking the status

On Ubuntu or Debian, run the following command:

sudo service supervisor status

On CentOS or RHEL, run the following command:

sudo systemctl status supervisord

Conclusion

Supervisor is a powerful process control system that can be used to manage multiple processes on a Linux system. Its features, such as automatic process restart and process group management, make it an ideal tool for keeping a system functional.