45395 - シコウサクゴ -

Mac launchdの使い方:plistの書き方から定期実行まで

2024-09-16
プログラミング
launchd
Mac
定期実行
バッチ処理
plist
launchctl
Last updated:2026-05-07
3 Minutes
585 Words

macOSでcronのような定期実行をするには、launchdを使います。 この記事では、Mac(macOS)でlaunchdを使ってPythonスクリプトを定期実行する方法を、plistの書き方からlaunchctlコマンドの使い方まで順を追って解説します。 ハマりがちなプログラムの配置場所や権限まわりの注意点も紹介します。

前提条件

  • Mac M1(AppleSilicon)

ゴール

1分ごとにhellowworld.txtに実行日時とhelloworldを追記する

1.launchdで実行するプログラムを用意する

プログラムは何でも良いですが、今回はpythonで用意しました。 プログラムは**「/Users/(ユーザー名)」**の直下に置きます。 「Documents」や外部SSDなどの「/Volumes/〜」などではうまく動きませんでした。

1
from datetime import datetime
2
3
# 現在の日付と時刻を取得し、yymmddhhmmss形式にフォーマットする
4
current_time = datetime.now().strftime('%y-%m-%d %H:%M:%S')
5
6
# 書き出す内容を作成
7
text_to_write = f"{current_time} helloworld"
8
9
# ファイルに書き出し(ファイルパスを絶対パスにする)
10
with open("/Users/(ユーザー名)/helloworld.txt", "a") as file:
11
file.write(text_to_write)

2.バッチファイルを用意する

anaconda3を使用しており、環境をアクティベイトしていますが、必要なければ割愛してOKです。 「python 絶対パス」で実行

1
#!/bin/zsh
2
3
source /Users/(ユーザー名)/anaconda3/etc/profile.d/conda.sh
4
conda activate (仮想環境名)
5
python /Users/(ユーザー名)/helloworld.py

3.バッチファイルに実行権限を付与する

バッチファイルに実行権限を与えます。

1
chmod +x /Users/(ユーザー名)/helloworld.sh

4.launchd用のplist(プロパティリスト)を作成する

下記のサイトを参考にplistを用意します。 Launched - A Plist Generator

Webサービスで Mac OS X の launchd (plist) を作成してワンライナーでインストールする #plists - Qiita

1
<?xml version="1.0" encoding="UTF-8"?>
2
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3
<plist version="1.0">
4
<dict>
5
<key>Label</key>
6
<string>com.example.helloworld</string>
7
8
<!-- 1分ごとに実行 -->
9
<key>StartInterval</key>
10
<integer>60</integer>
11
12
<!-- システム再起動後もタスクを起動 -->
13
<key>RunAtLoad</key>
14
<true/>
15
15 collapsed lines
16
<!-- 実行するスクリプト -->
17
<key>ProgramArguments</key>
18
<array>
19
<string>/Users/(ユーザー名)/helloworld.sh</string>
20
</array>
21
22
<!-- 標準出力(成功したときのログ) -->
23
<key>StandardOutPath</key>
24
<string>/Users/(ユーザー名)/helloworld_success.log</string>
25
26
<!-- 標準エラー(失敗したときのログ) -->
27
<key>StandardErrorPath</key>
28
<string>/Users/(ユーザー名)/helloworld_error.log</string>
29
</dict>
30
</plist>

5.plistをLibrary/LaunchAgentsに配置し、権限を設定する

1
cp /Users/(ユーザー名)/com.example.helloworld.plist ~/Library/LaunchAgents/com.example.helloworld.plist
2
sudo chown root:wheel ~/Library/LaunchAgents/com.example.helloworld.plist
3
sudo chmod 644 ~/Library/LaunchAgents/com.example.helloworld.plist

6.launchctlでplistをロードし、定期実行を開始する

1
launchctl load ~/Library/LaunchAgents/com.example.helloworld.plist

7.launchctl listでロード状態を確認する

1
launchctl list | grep com.example.helloworld

8.launchctlでアンロードし、定期実行を停止する

1
launchctl unload ~/Library/LaunchAgents/com.example.helloworld.plist

9.launchctl startで手動実行(テスト用)

1
launchctl start com.example.helloworld.plist

10.plistの構文エラーを確認する(plutil)

1
plutil start ~/Library/LaunchAgents/com.example.helloworld.plist

より詳細のエラー表示

1
launchctl bootstrap system ~/Library/LaunchAgents/com.example.helloworld.plist
Article title:Mac launchdの使い方:plistの書き方から定期実行まで
Article author:45395
Release time:2024-09-16

記事へのご質問・ご感想をお聞かせください

フィードバックを送る