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.

Top comments (0)