LoginSignup
3
6

More than 3 years have passed since last update.

CoreOSで定期的にジョブを実行する

Last updated at Posted at 2019-06-28

はじめに

CoreOSは、CentOSやUbuntu等のLinuxと違い、cronの仕組みがありません。
本記事は、CoreOSなどcronがない場合に、定期的にジョブを実行するための方法について記載しています。

設定手順

CoreOSは、Sytemdでサービスに対するタイマーを設定することで、cronのように定期的にジョブを実行することができます。

本記事では、任意のスクリプトを1分毎に、実行する場合の手順について記載します。
はじめに、serviceファイルとtimerファイルを作成します。

  • serviceの作成
    sudo vim /etc/systemd/system/<スクリプト>.service
[Unit]
Description=<スクリプトの説明>

[Service]
Type=oneshot
ExecStart=<スクリプトのフルパス>
  • timerの作成
    sudo vim /etc/systemd/system/<スクリプト名>.timer
[Unit]
Description=<スクリプト名>.service timer

[Timer]
OnCalendar=*:0/1

[Install]
WantedBy=multi-user.target
  • タイマーの有効化
    sudo systemctl enable <スクリプト名>.timer
  • タイマーの起動
    sudo systemctl start <スクリプト名>.timer

動作確認

設定が完了したので、動作確認を行います。
systemctlコマンドでタイマーの状態を確認し、journalctlコマンドでログを確認します。

  • 時間確認
    systemctl list-timers
NEXT                         LEFT     LAST                         PASSED       UNIT                         ACTIVATES
Fri 2019-06-28 14:57:07 UTC  10h left Fri 2019-06-28 02:57:07 UTC  1h 35min ago rkt-gc.timer                 rkt-gc.service
Sat 2019-06-29 00:00:00 UTC  19h left Fri 2019-06-28 00:00:00 UTC  4h 32min ago logrotate.timer              logrotate.service
Sat 2019-06-29 01:56:43 UTC  21h left Fri 2019-06-28 01:56:43 UTC  2h 35min ago systemd-tmpfiles-clean.timer systemd-tmpfiles-clean.service
n/a                          n/a      Fri 2019-06-28 04:32:12 UTC  7ms ago      <スクリプト>.timer            <スクリプト>.service

4 timers listed.
Pass --all to see loaded but inactive timers, too.
  • ログ確認
    journalctl -f -u <スクリプト>.service
-- Logs begin at Wed 2019-05-08 23:58:13 UTC. --
Jun 28 04:29:09 docker-CoreOS systemd[1]: Started <スクリプト>.
Jun 28 04:30:25 docker-CoreOS systemd[1]: Starting <スクリプト>...
Jun 28 04:30:25 docker-CoreOS systemd[1]: <スクリプト>.service: Succeeded.

おわりに

CoreOSにcronが実装されなかったのには、理由があると思うので調べてみるのも面白いと思います。

3
6
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
3
6