Skip to content

Instantly share code, notes, and snippets.

@take-piro
Last active June 4, 2019 04:00
Show Gist options
  • Save take-piro/06e54b3dfd09e8c182fc768612520f73 to your computer and use it in GitHub Desktop.
Save take-piro/06e54b3dfd09e8c182fc768612520f73 to your computer and use it in GitHub Desktop.
How to use cron in MacOS

Maccron を使うにはどうしたらいいかをざっくりまとめる.

cronと同じ挙動をする launchd

あいにくMacにはcronデーモンがない.
しかし,launchdがcronと同じ挙動をするのでそれを使う.

plist にスケジュールを書く

定期的に実行するにはplistファイルを書き,launchdにロードする必要がある.
plistはsample.plistを参照.

設定方法

plistファイルの置き場所

plistファイルを書いたら,launchdが認識するディレクトリへファイルを入れる必要がある.
また,ディレクトリによって実行権限が異なるらしい.
ユーザー単位での処理ならば,~/Library/LaunchAgentsに入れる.
直接ファイルを置いてもいいが,シンボリックリンクのほうが個人的にはいいと思う.

launchdへ登録

$ launchctl load hoge.plist

plistファイルを更新したら,一度解除した上で再度登録する必要がある.

$ launchctl unload hoge.plist && launchctl load hoge.plist

StandardCalendarInterval の設定の仕方

cronと同様に分,時,日,月,曜日を指定してやることで任意のタイミングで実行できる.
分,時,日,月,曜日を指定する順番に特に決まりはない?
分かりやすいように書けばいいだろう.

Minute <integer>
The minute on which this job will be run.

Hour <integer>
The hour on which this job will be run.

Day <integer>
The day on which this job will be run.

Weekday <integer>
The weekday on which this job will be run (0 and 7 are Sunday).

Month <integer>
The month on which this job will be run.
  • キーが無い場合にはcrontabの'*'と同じくワイルドカード扱い
  • 値は整数限定で'*/2'などの表記は不可
    • 毎分実行するならStartIntervalキーを使う
      • StartInterval は秒間隔

0時,4時,6時,20時それぞれの正時を指定する場合.

<key>StandardCalendarInterval</key>
<array>
  <dict>
    <key>Hour</key>
    <integer>0</integer>
    <key>Minute</key>
    <integer>0</integer>
  </dict>
  <dict>
    <key>Hour</key>
    <integer>4</integer>
    <key>Minute</key>
    <integer>0</integer>
  </dict>
  <dict>
    <key>Hour</key>
    <integer>6</integer>
    <key>Minute</key>
    <integer>0</integer>
  </dict>
  <dict>
    <key>Hour</key>
    <integer>20</integer>
    <key>Minute</key>
    <integer>0</integer>
  </dict>
</array>

時と分を各々dictの中に一つだけ入れておくと,指定した分と指定した時の各分で起動してしまう.
↑の例は正しい(はず)

注意点

  • 指定した時刻のときにスリープモードだった場合,起動時にスクリプトが実行される
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<!-- Label -->
<!-- ラベル名 -->
<key>Label</key>
<string>vim-undo-file-remove</string>
<!-- Script -->
<!-- 実行するスクリプト -->
<key>ProgramArguments</key>
<array>
<string>sh</string>
<string>/hoge/vimUndoFileRemove.sh</string>
</array>
<!-- launchctl load したタイミングで実行するか -->
<key>RunAtLoad</key>
<true/>
<!-- or <false/> -->
<!-- インターバル指定 秒単位 (例:30秒毎) -->
<!--
<key>StartInterval</key>
<integer>30</integer>
-->
<!-- StartInterval と StartCalendarInterval はどちらかを指定 -->
<!-- カレンダー指定 -->
<!-- 例: 毎日10時と20時30分 -->
<key>StartCalendarInterval</key>
<dict>
<key>Hour</key>
<integer>10</integer>
<key>Minute</key>
<integer>0</integer>
</dict>
<dict>
<key>Hour</key>
<integer>20</integer>
<key>Minute</key>
<integer>30</integer>
</dict>
<!-- ログ出力の指定 -->
<!-- 標準エラー -->
<key>StandardErrorPath</key>
<!-- 出力しない場合は /dev/null -->
<!-- <string>/dev/null</string> -->
<string>/hoge/logs/stderr.log</string>
<!-- 標準出力 -->
<key>StandardOutPath</key>
<string>/hoge/logs/stdout.log</string>
<!-- 出力しない場合は /dev/null -->
<!-- <string>/dev/null</string> -->
</dict>
</plist>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment