Build custom systemctl service command

Standard

Let’s create a service command in linux to execute the Laravel queue listen command.

php artisan queue:listen

Lets start 🙂

STEP 1: Create bash script for Laravel php artisan queue:listen

Create a bash script queue.sh to any preferred location in server. I preferred to create in /root as a root user.

$ sudo su –

$ vim /root/queue.sh

 

In queue.sh file

#!/bin/bash

cd /path/to/laravel/project

php artisan queue:listen

 

Save the file and change the file mode executable.

$ chmod +x /root/queue.sh

STEP 2: Create .service

Create a service .service file named ays_queue.service. 

$ sudo vim  /etc/systemd/system/ays_queue.service

In ays_queue.service file

[Unit]
Description=AYS Laravel queue listen service

[Service]
ExecStart=/bin/sh /root/queue.sh

[Install]
WantedBy=multi-user.target

Here, ‘WantedBy=multi-user.target’ refers the service create can can be executed by any user in the system.

Now, once .service file created the system should reload the daemon.

$ systemctl daemon-reload

To verify, the system service working following commands can be executed

$ systemctl status ays_queue
$ systemctl start ays_queue
$ systemctl stop ays_queue
$ systemctl restart ays_queue

STEP 4: Check and re-start command

In advance, one more step to check the command and start the service in case of inactive.

Create a new bash file again in /root named check_ays_queue.sh

$ sudo vim /root/check_ays_queue.sh

In check_ays_queue.sh file

#!/bin/bash

CHECKQUEUE=$(systemctl is-active ays_queue)

if [ $CHECKQUEUE = ‘active’ ]; then
echo ‘active’
else
systemctl restart ays_queue
fi

Also, make check_queue.sh file executable.

$ sudo chmod +x /root/check_ays_queue.sh

You can add check_ays_queue.sh in crontabs to check the status and restart the service in case of inactive. In this way, your service will never stops.

 

Enjoy!!

 

Advertisements

How to use laravel php command in linux?

Standard

All you need is to place the ~/.composer/vendor/bin directory in your PATH so that laravel command is executable. To do show, here is here the process:

Firstly, create

nano ~/.bash_profile 

And paste

export PATH=~/.composer/vendor/bin:$PATH

Restart the terminal and it starts working.

 

REMINDER.

  • Make sure you have installed composer in you system