W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
過去,開發(fā)者會(huì)對每個(gè)他們想要調(diào)用的命令行指令建立 Cron 對象。然而,這很令人頭痛。你的命令行指令調(diào)用不再包含在版本控制里面,并且你必須 SSH 進(jìn)入你的服務(wù)器以添加 Cron 對象。讓我們來讓生活變得更輕松。Laravel 命令調(diào)用器允許你順暢地且語義化地定義命令調(diào)用在 Laravel 里面,而且你的服務(wù)器只需要一個(gè) Cron 對象。
你的命令調(diào)用保存在 app/Console/Kernel.php
文件。你會(huì)在這個(gè)類里看到一個(gè) schedule
方法。為了幫助您開始,方法里面包含一個(gè)簡單的例子。你可以依照你需要的自由地添加任何數(shù)量的預(yù)定工作到 Schedule
對象。你只需要添加這個(gè) Cron 對象到服務(wù)器:
* * * * * php /path/to/artisan schedule:run 1>> /dev/null 2>&1
這個(gè) Cron 將會(huì)每分鐘調(diào)用 Laravel 命令調(diào)用器。接著,Laravel 評估你的預(yù)定工作并在時(shí)間到時(shí)執(zhí)行工作。這不能再更簡單了!
讓我們來多看幾個(gè)調(diào)用的例子:
$schedule->call(function()
{
// 執(zhí)行一些任務(wù)...
})->hourly();
$schedule->exec('composer self-update')->daily();
$schedule->command('foo')->cron('* * * * *');
$schedule->command('foo')->everyFiveMinutes();
$schedule->command('foo')->everyTenMinutes();
$schedule->command('foo')->everyThirtyMinutes();
$schedule->command('foo')->daily();
$schedule->command('foo')->dailyAt('15:00');
$schedule->command('foo')->twiceDaily();
$schedule->command('foo')->weekdays();
$schedule->command('foo')->weekly();
// 調(diào)用每周一次在特定的日子 (0-6) 和時(shí)間的工作...
$schedule->command('foo')->weeklyOn(1, '8:00');
$schedule->command('foo')->monthly();
$schedule->command('foo')->mondays();
$schedule->command('foo')->tuesdays();
$schedule->command('foo')->wednesdays();
$schedule->command('foo')->thursdays();
$schedule->command('foo')->fridays();
$schedule->command('foo')->saturdays();
$schedule->command('foo')->sundays();
By default, scheduled jobs will be run even if the previous instance of the job is still running. To prevent this, you may use the withoutOverlapping
method:
$schedule->command('foo')->withoutOverlapping();
In this example, the foo
command will be run every minute if it is not already running.
$schedule->command('foo')->monthly()->environments('production');
指定工作在當(dāng)應(yīng)用程序處于維護(hù)模式也應(yīng)該執(zhí)行
$schedule->command('foo')->monthly()->evenInMaintenanceMode();
$schedule->command('foo')->monthly()->when(function()
{
return true;
});
$schedule->command('foo')->sendOutputTo($filePath)->emailOutputTo('foo@example.com');
注意: 你必須先把輸出存到文件中才可以發(fā)送 email。
$schedule->command('foo')->sendOutputTo($filePath);
$schedule->command('foo')->thenPing($url);
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: