前提・実現したいこと
ここに質問の内容を詳しく書いてください。
PHP(laravel)で一定時間でチャットワークにコメントを送る機能を実装しようとしておりますが、
cronが機能しません。
下記のコードにどこか問題がありますでしょうか。
該当のソースコード
laravel
1// Kernel.php 2namespace App\Console; 3 4use Illuminate\Support\Facades\DB; 5use Illuminate\Console\Scheduling\Schedule; 6use Illuminate\Foundation\Console\Kernel as ConsoleKernel; 7 8class Kernel extends ConsoleKernel 9{ 10 protected function schedule(Schedule $schedule) 11 { 12 $schedule->command('sample-batch') 13 ->everyMinute(); 14 } 15}
laravel
1 2// SampleBatch.php 3namespace App\Console\Commands; 4 5use Carbon\Carbon; 6use GuzzleHttp\Client; 7use Illuminate\Console\Command; 8 9class SampleBatch extends Command 10{ 11 protected $signature = 'sample-batch'; 12 protected $description = 'チャットワークに通知する'; 13 private $apiToken = 'xxxx'; 14 private $myChatUrl = 'https://api.chatwork.com/v2/rooms/xxxx/messages'; 15 16 public function handle() 17 { 18 $client = new Client([ 19 \GuzzleHttp\RequestOptions::VERIFY => false, 20 ]); 21 22 $client->post($this->myChatUrl, [ 23 'form_params' => [ 24 'body' => '未完了のタスクがあります', 25 ], 26 'headers' => [ 27 'X-ChatWorkToken' => $this->apiToken, 28 ], 29 ]); 30 } 31}
Ubuntu
1# Edit this file to introduce tasks to be run by cron. 2# 3# Each task to run has to be defined through a single line 4# indicating with different fields when the task will be run 5# and what command to run for the task 6# 7# To define the time you can provide concrete values for 8# minute (m), hour (h), day of month (dom), month (mon), 9# and day of week (dow) or use '*' in these fields (for 'any'). 10# 11# Notice that tasks will be started based on the cron's system 12# daemon's notion of time and timezones. 13# 14# Output of the crontab jobs (including errors) is sent through 15# email to the user the crontab file belongs to (unless redirected). 16# 17# For example, you can run a backup of all your user accounts 18# at 5 a.m every week with: 19# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/ 20# 21# For more information see the manual pages of crontab(5) and cron(8) 22# 23# m h dom mon dow command 24* * * * * cd /c/xampp/... && php artisan schedule:run >> /dev/null 2>&1
試したこと
php artisan sample-batchのコマンドは問題なく動いています。
php artisan schedule:runを実行すると一回チャットワークにメッセージが入るだけです。
補足情報(FW/ツールのバージョンなど)
PHP 7.4.9
laravel 6.18.35
Composer 1.10.10
あなたの回答
tips
プレビュー