1
3

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 1 year has passed since last update.

【Mac】launchdでスクリプトを定期実行する方法

Last updated at Posted at 2022-09-20

設定方法

用途ごとに設定ファイルを以下の場所に保存します。

場所 用途
~/Library/LaunchAgents/<Label>.plist ユーザー単位のエージェント
/Library/LaunchAgents/<Label>.plist 管理者が提供するユーザーごとのエージェント
/Library/LaunchDaemons/<Label>.plist 管理者によって提供されるシステム全体のデーモン

以下のコマンドを実行すると定期実行されるようになります。

ターミナル
launchctl load /path/to/your.plist

以下のコマンドを実行すると定期実行されなくなります。

ターミナル
launchctl unload /path/to/your.plist

サンプル

30秒ごとにsample.shを実行

~/Library/LaunchAgents/sample.plist
<?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>
  <key>Label</key>
  <string>sample</string>
  <key>Program</key>
  <string>/Users/mofu/script/sample.sh</string>
  <key>StartInterval</key>
  <integer>10</integer>
  <key>StandardOutPath</key>
  <string>/Users/user_name/script/sample.out</string>
  <key>StandardErrorPath</key>
  <string>/Users/user_name/script/sample.err</string>
</dict>
</plist>

毎日3時にsample.shを実行

~/Library/LaunchAgents/sample.plist
<?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>
  <key>Label</key>
  <string>sample</string>
  <key>Program</key>
  <string>/Users/user_name/script/sample.sh</string>
  <key>StartCalendarInterval</key>
  <dict>
      <key>Minute</key>
      <integer>0</integer>
      <key>Hour</key>
      <integer>3</integer>
  </dict>
  <key>StandardOutPath</key>
  <string>/Users/user_name/script/sample.out</string>
  <key>StandardErrorPath</key>
  <string>/Users/user_name/script/sample.err</string>
</dict>
</plist>
1
3
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
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?