如何在没有单用户模式(详细模式)的情况下使用launchd在macos中运行脚本

问题描述 投票:0回答:0

我正在尝试设置一个脚本,以便在计算机关闭或重新启动时自动执行。我找到的解决方案是将 launchdbash 脚本结合使用,该脚本将在登录时运行并无限期地捕获 SIGTERM 信号。但是 launchd单用户 模式下运行脚本,其中缺少一些命令。

有没有办法强制 launchdverbose 模式运行脚本,或者任何其他方式自动执行脚本?

我尝试从以下地方注册启动器,结果是一样的

  1. ~/Library/LaunchAgents/script.plist

  2. /库/LaunchAgents/script.plist

  3. /库/LaunchDaemons/script.plist

    脚本.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>local.main.vaultctlstopper</string>
    
      <key>ProgramArguments</key>
      <array>
        <string>/path/to/my/bash/script</string>
      </array>
    
      <key>RunAtLoad</key>
      <true/>
    
      <key>StandardOutPath</key>
      <string>/path/to/log</string>
    
      <key>StandardErrorPath</key>
      <string>/path/to/error/log</string>
    </dict>
    </plist>
    
    

    脚本.sh

    #!/bin/bash
    
    function logout() {
      # here need to use diskutil and gpg, also to have root privileges
      exit
    }
    
    trap 'logout' SIGTERM
    
    while true; do
      :&
    done
    
    

    错误

    1. 磁盘工具
    Unable to run because unable to use the DiskManagement framework.
    Common reasons include, but are not limited to, the DiskArbitration
    framework being unavailable due to being booted in single-user mode.
    
    1. gpg(无法加载libintl.8.dylib库)
    dyld[90721]: Library not loaded: /usr/local/opt/gettext/lib/libintl.8.dylib
      Referenced from: <E3421065-D8FB-3482-925D-B5215101AE0B> /usr/local/Cellar/gnupg/2.4.3/bin/gpg
      Reason: tried: '/usr/local/opt/gettext/lib/libintl.8.dylib' (code signature in <C59C6AEC-6FF0-34F8-8328-93F7F2130CE1> '/usr/local/Cellar/gettext/0.21.1/lib/libintl.8.dylib' not valid for use in process: system is shutting down), '/System/Volumes/Preboot/Cryptexes/OS/usr/local/opt/gettext/lib/libintl.8.dylib' (no such file), '/usr/local/opt/gettext/lib/libintl.8.dylib' (code signature in <C59C6AEC-6FF0-34F8-8328-93F7F2130CE1> '/usr/local/Cellar/gettext/0.21.1/lib/libintl.8.dylib' not valid for use in process: system is shutting down), '/usr/local/lib/libintl.8.dylib' (code signature in <C59C6AEC-6FF0-34F8-8328-93F7F2130CE1> '/usr/local/Cellar/gettext/0.21.1/lib/libintl.8.dylib' not valid for use in process: system is shutting down), '/usr/lib/libintl.8.dylib' (no such file, not in dyld cache), '/usr/local/Cellar/gettext/0.21.1/lib/libintl.8.dylib' (code signature in <C59C6AEC-6FF0-34F8-8328-93F7F2130CE1> '/usr/local/Cellar/gettext/0.21.1/lib/libintl.8.dylib' not valid for use in process: system is shutting down), '/System/Volumes/Preboot/Cryptexes/OS/usr/local/Cellar/gettext/0.21.1/lib/libintl.8.dylib' (no such file), '/usr/local/Cellar/gettext/0.21.1/lib/libintl.8.dylib' (code signature in <C59C6AEC-6FF0-34F8-8328-93F7F2130CE1> '/usr/local/Cellar/gettext/0.21.1/lib/libintl.8.dylib' not valid for use in process: system is shutting down), '/usr/local/lib/libintl.8.dylib' (code signature in <C59C6AEC-6FF0-34F8-8328-93F7F2130CE1> '/usr/local/Cellar/gettext/0.21.1/lib/libintl.8.dylib' not valid for use in process: system is shutting down), '/usr/lib/libintl.8.dylib' (no such file, not in dyld cache)
    
macos launchd
© www.soinside.com 2019 - 2024. All rights reserved.