如何让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
Option 1: Systemd Service (Recommended for Ubuntu)
Create a systemd service file to manage the listener process and enable auto-start:
- Create the service file:
sudo nano /etc/systemd/system/laravel-mqtt-listener.service
- 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
- Enable and start the service:
sudo systemctl daemon-reload sudo systemctl enable laravel-mqtt-listener.service sudo systemctl start laravel-mqtt-listener.service
- Check service status:
sudo systemctl status laravel-mqtt-listener.service
Option 2: Supervisor
Use Supervisor to manage the process and ensure auto-restart:
- Install Supervisor:
sudo apt update && sudo apt install supervisor
- Create a Supervisor config file:
sudo nano /etc/supervisor/conf.d/laravel-mqtt-listener.conf
- 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
- Update Supervisor and start the process:
sudo supervisorctl reread sudo supervisorctl update sudo supervisorctl start laravel-mqtt-listener
- Check process status:
sudo supervisorctl status
内容的提问来源于stack exchange,提问作者Joe Ijam
相关产品推荐
相关产品推荐

