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 Bright Data

Maximize Data Efficiency – Store and process vast amounts efficiently.

Optimize your infrastructure with our solutions designed for high-volume data processing and storage.

Optimize Now

Top comments (0)

Heroku

This site is powered by Heroku

Heroku was created by developers, for developers. Get started today and find out why Heroku has been the platform of choice for brands like DEV for over a decade.

Sign Up

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay