Linux – Needing to open OS X bash script as via launchd IN a terminal window

bashlaunchdlinuxmacosterminal

I've got a lengthy and complex bash script which works OK, but I'm trying to automate my deployment process and I'm flummoxed at this point.

I can get the script to run as a LaunchAgent no problem, but of course it happens in the background on boot. So I have a pause at the start of the script where I can press any button if I need to interrupt the script.

The problem with that is I need the script to open in a Terminal window so I can press the button to cancel or continue it. This is where my trouble starts.

The script has many sudo commands. I've created the below .plist and I can load it manually just fine. It works and loads the script and everything works. However, it does nothing at boot and just exits with error code 1 in Console.

I've tried putting it in /Library/LaunchAgents and /Library/LaunchDaemons. It basically needs to open the terminal window as root so that all the commands within the script will run as root without the need for me to manually enter the password.

<?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>com.deecies.first</string>
<key>ProcessType</key>
<string>Interactive</string>
<key>ProgramArguments</key>
<array>
    <string>open</string>
    <string>-a</string>
    <string>terminal</string>
    <string>/Users/admin/first-boot.command</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>

I tried specifying /usr/bin/open and the full path to Terminal.app too, but to no avail. I've tried adding sudo string first and /usr/bin/sudo but nothing either.

I did manage it to get a Terminal window to open when it was placed in /Library/LaunchAgents, however it doesn't appear to be opening either Terminal, or the script, as root, as I get "permission denied" for all the commands in the script that would require sudo.

Best Answer

Instead of open Terminal, you can invoke the application binary file directly (not the .app file). The following should work for your launchd. Note that Terminal will launch, run the script, and then exit. Depending on your Terminal.app preferences, the terminal window may disappear immediately, or just leave a dead window on the screen with the output of the script.

<array>
<string>/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal</string>
<string>/Users/admin/first-boot.command</string>
</array>
Related Question