Laravel在共享主机上创建cron作业

时间:2020-01-15 17:14:32

标签: php laravel

我开始学习Laravel文档中的“计划任务”一章。在localhost xampp项目上,我创建了删除表

中最后插入的行的命令
class em extends Command
{
/**
 * The name and signature of the console command.
 *
 * @var string
 */
protected $signature = 'my:em';

/**
 * The console command description.
 *
 * @var string
 */
protected $description = 'Delete task';

/**
 * Create a new command instance.
 *
 * @return void
 */
public function __construct()
{
    parent::__construct();
}

/**
 * Execute the console command.
 *
 * @return mixed
 */
public function handle()
{

    DB::table('task')->orderBy('id', 'desc')->limit(1)->delete();
    echo 'done';
}

}

然后我将其添加到内核

class Kernel extends ConsoleKernel
{
/**
 * The Artisan commands provided by your application.
 *
 * @var array
 */
protected $commands = [
    //
];

/**
 * Define the application's command schedule.
 *
 * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
 * @return void
 */
protected function schedule(Schedule $schedule)
{
    $schedule->command('my:em')->everyMinute();


}

/**
 * Register the commands for the application.
 *
 * @return void
 */
protected function commands()
{
    $this->load(__DIR__.'/Commands');

    require base_path('routes/console.php');
}

}

然后,我使用 php artisan schedule:run 命令创建了.bat文件,并将其添加到Windows的任务管理器中。一切正常。

接下来,我将此项目上传到具有ssh访问和cron作业功能的共享服务器。我想使用 php artisan schedule:run ,但收到消息:尚无准备好的命令可以运行

另一方面,服务器具有cron作业的内置创建者。我可以创建任务。我必须设置日期和时间,并填写英文翻译为“ command”的字段(请参见下图)。我不知道该在这里输入什么。请帮忙(appson是我的域名)。

picture

1 个答案:

答案 0 :(得分:1)

要添加的cron条目可以在文档中找到

https://laravel.com/docs/6.x/scheduling#introduction

* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1

如果您无法访问类似crontab之类的内容,则只需将该条目添加到服务器的“ cron作业的内置创建者”中即可。