Ubuntu下Laravel定时任务后台持久运行问题求助
Hey there, let's clear up a key misunderstanding first—php artisan schedule:run is a one-time command, not a long-running process. It checks if any scheduled tasks are due right now, runs them, and then exits immediately. That's why your nohup process gets terminated eventually (it's already done its job after the first run, and the lingering nohup process might get cleaned up by system policies after a few days).
Here are two reliable, permanent solutions tailored to your needs:
Option 1: Use System Cron (Official & Recommended)
Laravel's scheduler is designed to work with system cron, which triggers schedule:run every minute. This is the most stable and standard approach.
- Open your user's crontab editor:
crontab -e - Add this line at the end (replace
/path-to-your-laravel-projectwith your actual project directory):* * * * * cd /path-to-your-laravel-project && php artisan schedule:run >> /dev/null 2>&1* * * * *tells cron to run the command every minutecd /path-to-your-laravel-projectensures we're in the correct directory for artisan- The output redirect (
>> /dev/null 2>&1) silences logs (you can change this to a log file path if you want to keep records)
- Save and exit the editor—cron will automatically apply the changes.
This setup ensures your scheduler runs reliably every minute without needing to manually manage background processes.
Option 2: Use Supervisor with schedule:work (For Long-Running Process)
If you prefer not to use cron, Laravel 8+ provides the schedule:work command, which runs continuously and checks for scheduled tasks every minute. You can use Supervisor to monitor this process and restart it if it crashes.
Step 1: Install Supervisor
sudo apt update && sudo apt install supervisor
Step 2: Create a Supervisor Configuration File
Create a new config file in /etc/supervisor/conf.d/ (e.g., laravel-schedule.conf):
[program:laravel-schedule] process_name=%(program_name)s_%(process_num)02d command=php /path-to-your-laravel-project/artisan schedule:work autostart=true autorestart=true user=your-server-username # Replace with your actual server user (e.g., www-data, ubuntu) redirect_stderr=true stdout_logfile=/path-to-your-laravel-project/storage/logs/schedule-work.log
autostart=true: Starts the process automatically when Supervisor bootsautorestart=true: Restarts the process if it exits unexpectedlystdout_logfile: Logs output to help debug issues later
Step 3: Reload Supervisor & Start the Process
sudo supervisorctl reread sudo supervisorctl update sudo supervisorctl start laravel-schedule:*
Why Your Earlier Supervisor Setup Failed
You were using schedule:run with Supervisor, which exits immediately after running. Supervisor interprets this as a crash and keeps restarting the command repeatedly. schedule:work is the long-running alternative that stays active, making it perfect for Supervisor.
Final Recommendation
Stick with the cron option—it's the official Laravel approach, integrates seamlessly with system tools, and avoids overcomplicating things with process managers unless you have a specific need for schedule:work.
内容的提问来源于stack exchange,提问作者Raz Biswas

