我在主机中添加了cron作业:
* * * * * cd /home/mydomain/public_html/ && php artisan schedule:run >> /dev/null 2>&1
和内核文件:
protected function schedule(Schedule $schedule)
{
file_put_contents(base_path('public/temp/test.txt'),'test_string');
}
但是它不会创建文件,当我将其添加到cron作业中时,它会每分钟创建一个文件夹。
* * * * * cd /home/mydomain/public_html/ && mkdir test_folder >> /dev/null 2>&1
当我在终端php artisan schdule:run
中运行此命令时,它将创建test.txt
文件。
答案 0 :(得分:3)
因为您没有通过$schedule
来调用它,
检查以下内容:https://laravel.com/docs/5.8/scheduling#defining-schedules
您的代码必须像这样:
protected function schedule(Schedule $schedule)
{
$schedule->call(function () {
if (!file_exists(public_path('temp'))) {
mkdir(public_path('temp') , 0777);
}
file_put_contents(base_path('public/temp/tset.txt'),'test_string');
})->dailyAt('2:10');
}
如果您使用php artisan schedule:run
,它将自动触发事件:"schedule:run source code"
您没有任何事件,它将显示No scheduled commands are ready to run.
。
但是,创建该文件的原因是因为您覆盖了schedule函数,它最初支持为空。 schedule
答案 1 :(得分:1)
用$schedule
$schedule->call(function () {
file_put_contents(base_path('public/temp/tset.txt'),'test_string');
})->everyFiveMinutes();
或者制作command
class并完成