LoginSignup
4
0

More than 3 years have passed since last update.

cronを使ってバッチ処理を実行

Posted at

バッチ処理とは

どこかのタイミングで処理をまとめて行う

cronとは

ジョブを自動実行するためのデーモンプロセス(バックグラウンドプロセス)

cronを設定するのがcronコマンド

初心者向けcronの使い方

【実例】ある時間に定期的にメールを送る

mb_send_mail関数
日本語などのマルチバイト文字を扱ってメールの送信が可能

mb_send_mail(string $受信者, string $タイトル, string $本文[, string $追加ヘッダ[, string $追加パラメータ ]])

cron.php
<?php
mb_language("Japanese");//日本語のメールを送る場合に必要
mb_internal_encoding("UTF-8");//日本語のメールを送る場合に必要

$to      = 'example@hello.com';//宛先
$subject = 'はじめてのcron';//件名
$message = 'ある時間になると定期的にメールを送るよ';//本文
$headers = 'From: example@world.com' . "\r\n";//追加ヘッダー

mb_send_mail($to, $subject, $message, $headers);

cronコマンド

0 10 * * * /usr/bin/php home/www/cron/cron.php

実行する時間を指定して、実行するPHPファイルを絶対パスで指定
PHPファイルを実行するには/usr/bin/phpもしくは/usr/local/bin/phpをファイル名の前に記載する。

4
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0