0

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.

3
  • It seems the open command doesn't work as root - I can probably get the terminal GUI to open, but of course thats not my intention. I could probably get the script to run without the terminal window - combining the two seems to be nearly impossible. Maybe bsexc could be used somewhere, but i'm not entirely sure how. – realdannys Jan 20 '16 at 0:10
  • Do you need the script to run at boot time (even if no one's logged in), or do you need the script to run when you log into your user account? If you need it to run at boot time even if no one's logged in, you can't use an interactive terminal because GUI apps can only run when a user is logged into the GUI. Also, for the issue of wanting to sudo without having to enter a password, did you already edit your /etc/sudoers file to allow that? Because by default, sudo requires a password the first time after login; then there's a timeout where you don't need to re-enter the password. – Spiff Jan 20 '16 at 1:02
  • Hey Spiff, I basically want it to run hands off - and it'll have to be after my deployment admin has logged in, simply to give me the GUI of terminal where I can press any button to (and there's another GUI app Applescript interacts with) the issue I was getting was opening Terminal and having it run the command as root (it was giving me GUI issues trying to open as root) – realdannys Jan 20 '16 at 1:04
1

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>
8
  • Hi Kent, thanks i didn't try it quite this simply. Do you think i'll need to up admin in the UserName key or will this open as root ok? The script that it runs has a number of sudo commands in it, so the idea is having root run it so that there is no manual user intervention to enter the password - do you think this method will both launch the terminal gui allowing me to press any key to stop it (if required) and execute all the sudo commands? – realdannys Jan 20 '16 at 1:06
  • I didn't try in the launchd script; but, when run as a su'd root user, the command would invoke a second instance of Terminal.app in the dock, and with the standard default colors and terminal size, etc; so, I know it is using the root preferences. The only thing I can think of during launchd is that it might try to start Terminal before the window server is ready for it. – Kent Jan 20 '16 at 1:51
  • I tried it as a LaunchDaemon and all it did was run the script in the background as it normally would - no terminal window and no way to interact. I could try it as a LaunchAgent I guess, i'm just not sure if everything will happily run as sudo that way. – realdannys Jan 20 '16 at 2:58
  • That's what I was afraid of... FWIW, sudo in the script should not be an issue, because those processes run as root AFAIK. (The permissions on the /Library/LaunchDaemons and /Library/LaunchAgents directories are quite strict). I'll try to think of something else in the meantime. – Kent Jan 20 '16 at 3:12
  • Well I tried it as LaunchAgent and it had some funky permission errors but ultimately it did the same thing, ran it all in the background without a terminal window. I did add the .command script to the sudoers file but that does nothing at all - If i execute the script normally as admin it fails on the sudo required things with no permission, If I put sudo back into the script it stops and asks for password. Of course if I run the script as sudo anyway, it will do everything as needed without sudo lines just like Launchd is (except without GUI) so sudoers is doing nothing – realdannys Jan 20 '16 at 3:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.