LoginSignup
42
51

More than 5 years have passed since last update.

CentOS7でcronを指定日時に実行する

Last updated at Posted at 2017-04-01

備忘録代わりに書いておきます。

環境

CentOSのバージョン

$ cat /etc/redhat-release
CentOS Linux release 7.3.1611 (Core)

手順

CentOS7では初期設定でcronie-anacronがインストールされており、ジョブの実行時間に幅を持たせている。
指定日時ぴったりにジョブを実行したい場合、cronie-noanacronを入れる必要がある。

依存関係上先に、cronie-noanacronをインストールし、その後にcronie-anacronを削除する。

$ sudo yum -y install cronie-noanacron
$ sudo yum -y remove cronie-anacron

設定は/etc/cron.d/dailyjobsで行う。

設定の確認。

$ cat /etc/cron.d/dailyjobs

# Run the daily, weekly, and monthly jobs if cronie-anacron is not installed
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# run-parts
2 4 * * * root [ ! -f /etc/cron.hourly/0anacron ] && run-parts /etc/cron.daily
22 4 * * 0 root [ ! -f /etc/cron.hourly/0anacron ] && run-parts /etc/cron.weekly
42 4 1 * * root [ ! -f /etc/cron.hourly/0anacron ] && run-parts /etc/cron.monthly

cronie-anacronが削除されない場合は、実行されないようになっていることが分かる。

実行したいシェルは頻度に応じて/etc/cron.daily、/etc/cron.weekly、/etc/cron.monthlyに配置する。

配置後にcrondの再起動が必要。

$ sudo systemctl restart crond

ログは/var/log/cronに出力される
ログの確認。

$ sudo less /var/log/cron
42
51
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
42
51