skip to Main Content

I’m trying to create a service on an Amazon Linux 2023 instance to run a mysql database queue for Laravel 10. For various reasons I cant use Supervisor so I’m using Systemd.

I’m upgrading the AMI, and from Laravel 9 to 10, so I know this has worked in the past (but its untested with SystemD).

I’ve created a service, laravel-worker.service, to execute the "artisan queue:work" command. After doing "sudo systemctl daemon-reload", "sudo systemctl start laravel-worker" it appears to start the worker and immediately finish it.

I’ve confirmed it’s not processing the queue. I can see the jobs are sitting in the database.

If I SSH in and type ‘artisan queue:work’ the jobs get processed.

Here’s the service

EDIT: I’ve tried Type=simple and oneshot with no difference.

[Unit]
Description=Laravel Queue Worker
After=network.target

[Service]
User=root
Group=root
#Type=simple
Type=oneshot
Restart=on-failure
RemainAfterExit=true
ExecStart=/usr/bin/nohup /usr/bin/php /var/www/html/artisan queue:work --sleep=3 --tries=3
TimeoutStopSec=infinity

[Install]
WantedBy=multi-user.target

Here’s the status response from ‘sudo systemctl status laravel-worker’

laravel-worker.service - Laravel Queue Worker
     Loaded: loaded (/etc/systemd/system/laravel-worker.service; enabled; preset: disabled)
     Active: active (exited) since Thu 2023-12-21 18:26:13 UTC; 1h 2min ago
   Main PID: 621553 (code=exited, status=0/SUCCESS)
        CPU: 53ms

Dec 21 18:26:13 ip-xxxxxxxxxx.us-east-2.compute.internal systemd[1]: Starting laravel-worker.service - Laravel Queue Worker...
Dec 21 18:26:13 ip-xxxxxxxxxx.us-east-2.compute.internal systemd[1]: Finished laravel-worker.service - Laravel Queue Worker.

2

Answers


  1. Chosen as BEST ANSWER

    Thanks to @Marcin Orlowski I got to the bottom of this. I needed to add -f after the command and set Type=simple.

    For anyone looking for a solution in the future, here was my final service

    [Unit]
    Description=Laravel Queue Worker
    After=network.target
    
    [Service]
    User=root
    Group=root
    Type=simple
    Restart=on-failure
    RemainAfterExit=true
    ExecStart=/usr/bin/nohup /usr/bin/php -f  /var/www/html/artisan queue:work --sleep=3 --tries=3
    TimeoutStopSec=infinity
    
    [Install]
    WantedBy=multi-user.target
    

  2. The Type=oneshot is typically used for scripts that perform a task and then exit which might not be suitable for a continuously running service like a worker, so Type=simple seems more appropriate for your case.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search