LoginSignup
10

More than 3 years have passed since last update.

CentOS6以降 での cron の設定

Last updated at Posted at 2018-10-12

対象者

  • CentOS6以降使ってる
  • cronで時間帯指定して定期実行したい

環境

$ cat /etc/redhat-release
CentOS release 6.10 (Final)

多分 CentOS7 もほぼ同じ手順

手順

1. cronインストール

RHEL6からはデフォルトでanacronってのが入ってる。
このanacronをアンインストールして従来のcronをインストールする。
(anacron ... 負荷が掛からない様にOSが良い感じに時間帯を調整して実行してくれる。つまり自分で実行時間を指定できない)

$ sudo yum -y install cronie-noanacron      # noanacronインストール
$ sudo yum remove cronie-anacron            # anacron削除

※パッケージの依存関係の問題で、noanacronをインストールしてからanacronを削除する必要があることに注意

2. 設定ファイル編集

$ sudo vim /etc/crontab
crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=hogehoge@example.com
HOME=/

# For details see man 4 crontabs

# 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 su - [実行ユーザ] -lc "cd /home/[実行ユーザ]/[実行ファイルDIR] && [実行コマンド]"
  # bundleみたいに実行パスを考える必要があるなら↑の方法が安心ぽい

$ crontab -eで編集した方が好ましいかも

参照

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
10