You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让Laravel的MQTT监听器后台运行并实现开机自启?

Solutions for Laravel MQTT Listener Background Execution & Auto-Start

1. Run MQTT Listener as Background Process via Laravel Code

You can use Laravel's built-in Process facade to spawn the MQTT listener as a detached background process, avoiding terminal dependency.

Step 1: Create a Custom Artisan Command

Generate a new command to handle background spawning:

php artisan make:command StartMqttListenerBackground

Step 2: Implement the Command Logic

Edit the generated file at app/Console/Commands/StartMqttListenerBackground.php:

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Process;

class StartMqttListenerBackground extends Command
{
    protected $signature = 'start:mqtt-listener-background';
    protected $description = 'Start Laravel MQTT listener as a background process';

    public function handle()
    {
        $logPath = storage_path('logs/mqtt_listener.log');
        $errorLogPath = storage_path('logs/mqtt_listener_error.log');

        // Spawn detached process with log redirection
        Process::command('php artisan start.listening.mqtt')
            ->redirectOutput($logPath)
            ->redirectError($errorLogPath)
            ->detach()
            ->run();

        $this->info("MQTT listener started in background.");
        $this->info("Logs: {$logPath}");
        $this->info("Error logs: {$errorLogPath}");
    }
}

Step 3: Start the Background Listener

Run the new command to launch the listener in the background:

php artisan start:mqtt-listener-background

Verify the Process

Check if the listener is running:

ps aux | grep start.listening.mqtt

2. Auto-Start Listener on Server Reboot

Create a systemd service file to manage the listener process and enable auto-start:

  1. Create the service file:
sudo nano /etc/systemd/system/laravel-mqtt-listener.service
  1. Paste the following content (replace placeholders with your project path and user):
[Unit]
Description=Laravel MQTT Listener Service
After=network.target

[Service]
User=ubuntu
WorkingDirectory=/var/www/your-laravel-project
ExecStart=/usr/bin/php artisan start.listening.mqtt
Restart=always
RestartSec=5
StandardOutput=journal+console
StandardError=journal+console

[Install]
WantedBy=multi-user.target
  1. Enable and start the service:
sudo systemctl daemon-reload
sudo systemctl enable laravel-mqtt-listener.service
sudo systemctl start laravel-mqtt-listener.service
  1. Check service status:
sudo systemctl status laravel-mqtt-listener.service

Option 2: Supervisor

Use Supervisor to manage the process and ensure auto-restart:

  1. Install Supervisor:
sudo apt update && sudo apt install supervisor
  1. Create a Supervisor config file:
sudo nano /etc/supervisor/conf.d/laravel-mqtt-listener.conf
  1. Paste the following content (replace placeholders):
[program:laravel-mqtt-listener]
process_name=%(program_name)s
command=/usr/bin/php /var/www/your-laravel-project/artisan start.listening.mqtt
autostart=true
autorestart=true
user=ubuntu
numprocs=1
redirect_stderr=true
stdout_logfile=/var/www/your-laravel-project/storage/logs/mqtt_listener.log
  1. Update Supervisor and start the process:
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start laravel-mqtt-listener
  1. Check process status:
sudo supervisorctl status

内容的提问来源于stack exchange,提问作者Joe Ijam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 06:01:46