Krishnan's Personal Website

Home | About | Blog | Interesting Reads | Tutorials | Skills | Personal Projects | Books | Fun | Connect with me

Running a PHP script or Worker as a Systemd Service

Published On: Sep 24 2023
Written By: Krishnan Sethuraman
Category: Linux

Systemd is one of the best ways to run a PHP script as a service or to configure a worker. I have used it to configure multiple workers on the same server and it works seamlessly. You do not need to install, supervisor anymore to run your workers. 

In this article I have covered the step by step process to configure a service on Linux. 

Step 1: Choose a name for your service. Choose the name in relation to the application or the use case so that you remember it in the future. 

Step 2: Create a service file with the chosen name. 
sudo touch /etc/systemd/system/worker-name-service.service

Step 3: Edit the file and add the service information to it and save the file. 
sudo nano /etc/systemd/system/worker-name-service.service
Description=Team Tasks Notification Worker
ExecStart=/usr/bin/php artisan queue:work

Step 4: Reload the systemd daemon so that the new service is recognised. 
sudo systemctl daemon-reload

Step 5: Enable the new service. 
sudo systemctl enable worker-name-service

Step 6: Start the new service. 
sudo systemctl start worker-name-service

Step 7: Check the status of the service to ensure it is up and running. 
sudo systemctl status worker-name-service

If you see the green light then you have successfully configured your worker or php script as a systemd service.