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)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay