0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Macの.app(バンドル)は、実行ファイルではないので、launchctlの対象にとれないし、設定誤りでエラーも出ない

Last updated at Posted at 2025-11-24

何かエラーメッセージを出してくれればいいのに、何も出してくれないためハマったのでメモを残しておく。

TL;DR

  • タイトルの通り
  • CLIで ls -F してみるとハッキリわかるが、Macの「 ○○.app 」の実体はディレクトリーであり、Finderなり open コマンドなりがそれを解釈して「アプリケーションとして扱う」ことをしている
  • なので、定期実行やデーモンを扱う、UNIXの文脈だと、「実行可能バイナリー」ではないため、 launchd ( launchctl )は実行に失敗する
  • さらに困ったことに、この問題が起きていても、 launchd は何もログを出さない。plistで指定したエラーログにも、何も出ない
  • 回避策は、 osascript (apple script)経由でラップして open コマンドで実行する。(by Gemini)

詳細

プロンプト

Automatorで作成した .app を、plistで指定して launchctl load したのですが、うまく動作していません。どういう理由が考えられますか?
「SMB監視.app」をFinderから直接ダブルクリックすると、正常に動作することは確認済です。

実行したコマンド

$ launchctl load ./check-smb-mounts.plist
$ launchctl start ./check-smb-mounts.plist

~/Library/LaunchAgents/smb-monitor.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>SMB monitor</string>
    <key>Program</key>
    <string>/Users/私の名前/Library/Mobile Documents/com~apple~Automator/Documents/SMB監視.app</string>
    <key>RunAtLoad</key>
    <true/>
    <key>StartInterval</key>
    <integer>300</integer>
    <key>StandardOutPath</key>
    <string>/Users/私の名前/tmp/smb-checker.out</string>
    <key>StandardErrorPath</key>
    <string>/Users/私の名前/tmp/smb-checker.err</string>
  </dict>
</plist>

Geminiの回答

launchctl を使って Automator アプリケーションを実行しようとしているが、Finder からの実行時とは異なり、うまく動作しないのですね。

この問題の最も一般的な原因と、確認すべき点を以下にまとめます。特に、Automator アプリの実行方法環境の違いがポイントになります。


1. 実行方法の誤りまたは不適切なパスの指定

🍎 Programキーの誤り

Program キーは、実行可能なバイナリファイル自体のパスを指定する必要があります。Automator で作成した .app バンドルを指定した場合、launchctl はそのバンドル内の実行ファイルを見つけられず、通常はエラーになります。

  • 確認点:

    • Program キーの値は、.app バンドル自体ではなく、バンドル内部の実行可能なスクリプトやバイナリを指定する必要があります。
    • Automator アプリの場合、通常はバンドル内の /Contents/MacOS/Application Stub または類似のパスを指定する必要がありますが、これは Automator が想定する実行環境と異なるため、さらに問題を引き起こす可能性があります。
  • 正しい方法(推奨):
    Program の代わりに、Automator アプリを起動するための osascript または open コマンドを使用します。これにより、macOSがアプリケーションを起動する際の正しいコンテキストが提供されます。

    • Program キーを削除し、代わりに ProgramArguments キーを使用します。
    <key>ProgramArguments</key>
    <array>
      <string>/usr/bin/open</string>
      <string>/Users/私の名前/Library/Mobile Documents/com~apple~Automator/Documents/SMB監視.app</string>
      <string>--wait-apps</string>
    </array>
    
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?