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
- 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
$ 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
The content of the script can be simple as:
/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