cronについて


{success} cronの設定内容とcron関連で行った作業内容について記載しています。


cron設定ファイル

/etc/cron.d/production_◯◯

cron設定内容

# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
HOME=/var/www/html/production/◯◯

# Example of job definition:
# .---------------- minute (0 - 59)
# | .-------------- hour (0 - 23)
# | | .------------ day of month (1 - 31)
# | | | .---------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .-------- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
  * * * * * root      php artisan schedule:run >> /dev/null 2>&1
#

cronでコマンドを実行する場所の設定

HOME=/var/www/html/production/◯◯

Laravelのルートディレクトリでartisanコマンドを実行したいので、上記パスを設定しています。

cronで実行するコマンドの設定

* * * * * root php artisan schedule:run >> /dev/null 2>&1

毎分、rootユーザーでLaravelのスケジュール実行コマンドを実行するよう設定しています。
毎分スケジュール実行コマンドするのはLaravelのデフォルトのやり方です。


cron作業内容

{primary} 本番サーバーでcron関連で行った作業についてまとめています。

概要

  • 大抵、Linuxにもとからcronはインストールされているので、インストール作業はありません。
  • cronの設定には、crontabコマンドにて行う場合と、cron.dディレクトリに設定ファイルを作成する場合がありますが、crontabコマンドの場合はcrontab -rコマンドを実行するとcrontabの設定が消えてしまい事故りやすいので、cron.dディレクトリに設定ファイルを作成する方法を推奨。

手順

  1. sudo -i等でrootユーザーになります。
  2. /etc/cron.d/配下にcronの設定ファイルを作成します。
  3. service cron restartで追加した設定の反映を行います。

参考文献