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.

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

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