项目 调度 服务器,102-任务调度-Ubuntu16.04服务器

1. 启动调度器

打开crontab(定时任务)使用如下命令:

vim /etc/crontab

注意:这里不能直接使用crontab -e!

底下是唯一一个需要加入到服务器的 Cron 项目:

* * * * * 执行用户 php /path/to/artisan schedule:run >> /dev/null 2>&1

/path/to是你的项目目录,artisan执行目录!

自定义命令默认存储在app/Console/Commands目录中。

自定义命令:

php artisan make:console getNews --command=get:news

执行后会看到getNews.php命令文件

/**

* Create a new command instance.

*

* @return void

*/

public function __construct(/* 这里支持依赖注入 */){

parent::__construct();

...

}

/**

* Execute the console command.

*

* @return mixed

*/

public function handle()

{

// 这里执行你的业务

...

}

3. 调度定时执行

调度定义在 app/Console/Kernel.php 文件中

加入命令:

/**

* The Artisan commands provided by your application.

*

* @var array

*/

protected $commands = [

Commands\getNews::class,

];

schedule 方法定时执行

/**

* Define the application's command schedule.

*

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

* @return void

*/

protected function schedule(Schedule $schedule)

{

// 每10分钟执行 获取新闻

$schedule->command('get:news')->everyTenMinutes();

}

以上这三步执行完成就可以定时执行任务了,并且支持依赖注入!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值