19
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Docker で /etc/cron.d を使って cron を実行する

Last updated at Posted at 2016-08-04

docker で /etc/cron.d 以下にファイルを置いて cron を実行した際にハマったのでメモ。

環境

docker バージョン :

shell
$ docker -v
Docker version 1.10.3, build 3cd164c

イメージのベースは、debian :

Dockerfile
FROM debian:wheezy

手順

1. cron をインストールする

Dockerfile
RUN apt-get update
RUN apt-get install -y cron

2. /etc/cron.d 以下に cron の設定ファイルを置く

simple-cron
* * * * * root /script.sh
# An empty line is required at the end of this file for a valid cron file.
Dockerfile
ADD simple-cron /etc/cron.d/simple-cron
RUN chmod 0644 /etc/cron.d/simple-cron

3. cron から呼び出されるスクリプトを置く

script.sh
echo "$(date): executed script" >> /var/log/cron.log 2>&1
Dockerfile
ADD script.sh /script.sh
RUN chmod +x /script.sh

4. cron を起動する

Dockerfile
CMD cron && touch /etc/cron.d/simple-cron && tail -f /dev/null

cron 起動後に、/etc/cron.d/simple-cron のタイムスタンプを更新しないと、実行されなかった。

使用例

参考

19
20
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
19
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?