W3Cschool
恭喜您成為首批注冊用戶
獲得88經驗值獎勵
過去,開發(fā)者會對每個他們想要調用的命令行指令建立 Cron 對象。然而,這很令人頭痛。你的命令行指令調用不再包含在版本控制里面,并且你必須 SSH 進入你的服務器以添加 Cron 對象。讓我們來讓生活變得更輕松。Laravel 命令調用器允許你順暢地且語義化地定義命令調用在 Laravel 里面,而且你的服務器只需要一個 Cron 對象。
你的命令調用保存在 app/Console/Kernel.php
文件。你會在這個類里看到一個 schedule
方法。為了幫助您開始,方法里面包含一個簡單的例子。你可以依照你需要的自由地添加任何數量的預定工作到 Schedule
對象。你只需要添加這個 Cron 對象到服務器:
* * * * * php /path/to/artisan schedule:run 1>> /dev/null 2>&1
這個 Cron 將會每分鐘調用 Laravel 命令調用器。接著,Laravel 評估你的預定工作并在時間到時執(zhí)行工作。這不能再更簡單了!
讓我們來多看幾個調用的例子:
$schedule->call(function()
{
// 執(zhí)行一些任務...
})->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();
// 調用每周一次在特定的日子 (0-6) 和時間的工作...
$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');
指定工作在當應用程序處于維護模式也應該執(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號-3|閩公網安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: