使用laravel schedule cronjob更新数据库

kyvafyod  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(292)

我试着运行一个有说服力的模型来向数据库插入数据,我试着运行一个使用laravel schedule和cronjobs的测试运行,下面是我的代码
这是我的 App\Console\kernel.php ```
protected $commands = [
'App\Console\Commands\test',
];

/**

  • Define the application's command schedule.

  • @param \Illuminate\Console\Scheduling\Schedule $schedule

  • @return void
    */
    protected function schedule(Schedule $schedule)
    {

    $schedule->command('check:test')->everyMinute();

}

还有我的 `App\Console\Commands\test` ```
protected $signature = 'check:test';
public function handle()
{

        try {
            $test = new Test([
                "user_id" => 1,
                "data_one"=>321,
            ]);
            $test->save();
            return response()->json('successfully added');

        } catch (exception $e) {
            return response()->json('error', $e);

        }

}

还有我的 crontab -e 代码是,


* * * * * php /opt/lampp/htdocs/testProj/artisan schedule:run 1>>

/opt/lampp/htdocs/testProj/schedule.log 2>&1

我得到的日志是,
正在运行计划命令:“/usr/bin/php7.2”“artisan”check:test >'/dev/null'2>&1
数据库也没有变化。我做错了什么,如果有任何帮助都会很感激的

wwodge7n

wwodge7n1#

试着这样用在 crontab -e ```

          • php /opt/lampp/htdocs/testProj/artisan schedule:run >> /dev/null 2>&1

``` kernel.php 文件存储使用laravel登录日志文件

protected $commands = [
     Commands\test::class,
];

/**
 * Define the application's command schedule.
 *
 * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
 * @return void
 */
protected function schedule(Schedule $schedule)
{

    $schedule->command(Commands\test::class)->everyMinute()->appendOutputTo(storage_path('logs/scheduler.log'));

}

测试命令应该是这样的

protected $signature = 'check:test';
public function handle()
{
    try {
        $test = new Test()
        $test->user_id = 1;
        $test->data_one= 321;
        $test->save();
        return $this->info('successfully added');

    } catch (exception $e) {
       return $this->warning('successfully added');
    }
}

相关问题