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

Ubuntu下Laravel定时任务后台持久运行问题求助

Fixing Laravel Scheduler Permanent Background Execution on Ubuntu

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:

Laravel's scheduler is designed to work with system cron, which triggers schedule:run every minute. This is the most stable and standard approach.

  1. Open your user's crontab editor:
    crontab -e
    
  2. Add this line at the end (replace /path-to-your-laravel-project with 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 minute
    • cd /path-to-your-laravel-project ensures 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)
  3. 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 boots
  • autorestart=true: Restarts the process if it exits unexpectedly
  • stdout_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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.07 13:57:35