DEV Community

Wallace Maxters
Wallace Maxters

Posted on

Create a Laravel command to add your queue:work for a project as Systemd service in Linux

In your Laravel Project, create the file stubs/queue.service.stub with follow content:

[Unit]
Description=Laravel Queue Work

[Service]
User=www-data
Group=www-data
Restart=always
WorkingDirectory={{ $dir }}
ExecStart=/usr/bin/php artisan queue:work --sleep=3 --tries=3

[Install]
WantedBy=multi-user.target
Enter fullscreen mode Exit fullscreen mode

Now, in your routes/console.php add the follow code:

Artisan::command('app:install-queue-as-systemd', function () {
    $content = file_get_contents(base_path('stubs/queue.service.stub'));

    $content = strtr($content, ['{{ $dir }}' => base_path()]);

    file_put_contents('/etc/systemd/system/laravel-queue.service', $content);

    Process::run('systemctl daemon-reload');
    Process::run('systemctl enable laravel-queue.service');
    Process::run('systemctl start laravel-queue.service');
});
Enter fullscreen mode Exit fullscreen mode

Run php artisan app:install-queue-as-systemd to generate, install and start your queue:work as systemd service!

Note: You should be use admin permission to run this command.

Image of Datadog

Master Mobile Monitoring for iOS Apps

Monitor your app’s health with real-time insights into crash-free rates, start times, and more. Optimize performance and prevent user churn by addressing critical issues like app hangs, and ANRs. Learn how to keep your iOS app running smoothly across all devices by downloading this eBook.

Get The eBook

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay