How to run script in macos with launchd without singe-user mode (with verbose mode)

59 views Asked by At

I'm trying to set up a script to automatically execute when the computer is shut down or restarted. The solution I found is to use a launchd in combination with bash script that will run on login and catch the SIGTERM signal indefinitely. But the launchd runs the script in single-user mode in which some of the commands are missing.

Is there a way to force the launchd to run the script in verbose mode, or any other way to auto-execute the script?

I tried to register launcher from the following places, the result is the same

  1. ~/Library/LaunchAgents/script.plist

  2. /Library/LaunchAgents/script.plist

  3. /Library/LaunchDaemons/script.plist

    script.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>
    
    

    script.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
    
    

    Errors

    1. diskutil
    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 (can not load libintl.8.dylib library )
    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)
    
0

There are 0 answers