Thursday , 22 October 2020
Stackode
Home > Laravel > How to automatically execute shell script at startup boot on systemd Linux ( Laravel php artisan queue: work )

How to automatically execute shell script at startup boot on systemd Linux ( Laravel php artisan queue: work )

The following config will discuss a basic example on how to execute shell script during a boot time on systemd Linux. There maybe various reason why you might want to execute shell script during Linux startup like for example to start a particular custom service, check disk space, create a backup etc.

The following example below will serve as a basic template to be later modified to suit your specific needs. In the example below we will run “php artisan queue:work” laravel.

Systemd service unit

First, we need to create a systemd startup script eg.laravel_artisan_queue.serviceand place it into /etc/systemd/system/ directory. You can find the example of such systemd startup script below:

[Unit]
After=network.service

[Service]
ExecStart=/usr/share/nginx/project/laravel_artisan_queue.sh

[Install]
WantedBy=default.target
  • After: Instructs systemd on when the script should be run. In our case the script will run after network has started. Other example could be mysql.service, nginx.service etc.
  • ExecStart: This field provides a full path the actual script to be execute
  • WantedBy: Into what boot target the systemd unit should be installed

The above is an absolute minimum that our systemd service unit should contain in order to execute our script at the boot time. For more information and options to be used see systemd.servicemanual page:

$ man systemd.service

Configure and Enable the systemd service unit

Next, we create our custom shell script to be executed during systemd startup. The location and script name is already defined by service unit as

/usr/share/nginx/project/laravel_artisan_queue.sh.

The content of the script can be simple as:

#!/bin/bash
/usr/bin/php /usr/share/nginx/project/artisan queue:work redis --sleep=5 --queue=default:emails

$ chmod +x /usr/share/nginx/project/laravel_artisan_queue.sh

1. Reload the systemd process to consider newly created sample.service OR every time when sample.service gets modified.

$ systemctl daemon-reload

2. Enable this service to start after reboot automatically.

$ systemctl enable laravel_artisan_queue.service

3. Start the service.

$ systemctl start laravel_artisan_queue.service

4. Reboot the host to verify whether the scripts are starting as expected during system boot ( if needed ).

$ systemctl reboot

Source :

https://linuxconfig.org/how-to-automatically-execute-shell-script-at-startup-boot-on-systemd-linux

and

https://www.thegeekdiary.com/centos-rhel-7-how-to-make-custom-script-to-run-automatically-during-boot/

About Whendy

Whendy Blog mengumpulkan beberapa Tutorial Website, PHP, AngularJS, JQuery, Laravel, Codeigniter dan lain-lain. ( jika sempat ) :)

Check Also

Form dan HTML Laravel

Installation Opening A Form CSRF Protection Form Model Binding Labels Text, Text Area, Password & ...

Leave a Reply

Your email address will not be published. Required fields are marked *