Supervisor: A Process Control System & Setup for Laravel application

WhoAmI => notes.sohag.pro/author
Search for a command to run...

WhoAmI => notes.sohag.pro/author
No comments yet. Be the first to comment.
Introduction: In today's web application landscape, managing high traffic loads and ensuring optimal performance, availability, and reliability is critical. Load balancing is a crucial technique that can help achieve these goals. In this blog post, w...
I had a solid list of reasons my life wasn't moving faster, until a grainy old lecture pointed out the one name missing from it

The finale isn't a victory lap. It's the story of the control I shipped that did nothing, the footgun still sitting in my demo, and the handful of things I'd keep exactly as they are.

How do you hold a large payment for a second pair of eyes without ever letting the unapproved money touch a balance, and how do you stream that decision to the outside world without standing up a broker?

How do you show the total under a parent account when the whole system refuses to store a balance? A recursive query, a trigger that refuses to draw a circle, and a rule about what actually has to sum to zero.

Every time I wanted to change an FX rate I had to edit a file on the server and restart the app. So I moved rates and markup into a live admin API, and then audited it hard enough to find the bug that quietly undid the whole thing.

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.
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 comes with several features that make it a powerful tool for managing processes. Some of these features include:
Automatic Process Restart: As mentioned earlier, Supervisor can automatically restart a process if it stops unexpectedly.
Process Group Management: Supervisor allows the user to manage a group of processes as a single unit.
Process Priority: The user can set a priority for a process, which will determine the order in which processes are started and stopped.
Logging: Supervisor provides detailed logging of processes, which can be useful for debugging.
Web Interface: Supervisor has a web interface that allows the user to monitor and control processes from a web browser.
Now that we've discussed what Supervisor is and some of its features, let's get started with using it on a Linux system.
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
Make log file
sudo touch /var/www/html/storage/logs/worker.log
Create a conf file in /etc/supervisor/conf.d
sudo touch /etc/supervisor/conf.d/laravel-worker.conf
Now edit the laravel-worker.conf
sudo 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 artisanand worker.log
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).
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
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.
On Ubuntu or Debian, run the following command:
sudo service supervisor status
On CentOS or RHEL, run the following command:
sudo systemctl status supervisord
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.