How to setup Supervisor for Laravel Queue in Live Server
Supervisor is a process control system for Unix-like operating systems. It is a client/server system that allows you to monitor and control processes on a Unix system.
To setup Supervisor in Ubuntu based Live server you can follow these steps
Step 1: Install Supervisor
Open Terminal in Live server or connect to your server using SSH.
sudo apt-get install supervisor
Step 2: Create a Supervisor configuration file
Create a configuration file for your process. Here we are creating a file for the queue process.
sudo nano /etc/supervisor/conf.d/laravel-queue.conf
Here we created 'laravel-queue' file. This file will open in a nano text editor in your terminal.
Step 3: Configure Supervisor configuration file
[program:laravel-queue]
process_name=%(program_name)s_%(process_num)02d
command=php /project_path/project_name/artisan queue:work --sleep=3 --tries=3
autostart=true
autorestart=true
user=your_username
numprocs=4
redirect_stderr=true
'user': Change 'your_username' to your server's username.
'autorestart': You can specify whether the program should be automatically restarted if it crashes.
Step 4: Save and Close the configuration file
You can save file by 'Ctrl + X' then press 'Y' and then press 'Enter'. Now your file is saved.
Step 5: Reload Supervisor and start your process
Run these command in your terminal to reload and start your process
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start laravel-queue
'reread' command will read the update from configuration files.
'update' command adds any new programs defined in the configuration files.
'start' command will start your process here you have to give program name after 'start'
Note: If you are facing the below error while running the start command, it means you misconfigured the supervisor file. I also provided one solution here below.
// You should check 'process_name' in your configuration file. It should be 'program_name'
ERROR: CANT_REREAD: Format string '%(process_name)s_%(process_num)02d' for 'process_name' contains names ('process_name') which cannot be expanded.
Step 6: Varify your process's status
you can verify your process status by this command
sudo supervisorctl status
This command will give the list of all the processes with their current status whether they running, stop or failed.
Note: The supervisor log located in this path.
/var/log/supervisor/supervisord.log
Now your queue:work will run continuously.