This discussion is locked
mister woofie

Q: running shell script at startup (before login)

Objective: on the Mac side of a dual-boot Intel, I want to automate running a shell script at startup that will unmount the "Untitled" Windows partition. As a single line command, the following runs correctly when invoked manually from Terminal:

/usr/sbin/diskutil unmount force disk0s3

How can I include this script in the startup sequence?

iMac Intel Core 2 Duo, white, 1.83 GHz, Mac OS X (10.5.3)

Posted on Jun 25, 2008 7:44 AM

Close

Q: running shell script at startup (before login)

  • All replies
  • Helpful answers

Page 1 Next
  • by V.K.,

    V.K. V.K. Jun 25, 2008 8:12 AM in response to mister woofie
    Level 9 (56,110 points)
    Jun 25, 2008 8:12 AM in response to mister woofie
    why do you need to do this before login? it's much easier to add this shell script to your login items so that it's executed as soon as you log in.

    You should be able to set up a launch daemon to do it on start up but I'm not sure of the details.

    Use [lingon|http://lingon.sourceforge.net> to create a Users Daemon that runs your command on start up. I don't know at what point in the start up sequence it will actually run.
  • by macwiz1220,

    macwiz1220 macwiz1220 Jun 25, 2008 8:19 AM in response to V.K.
    Level 4 (1,940 points)
    Jun 25, 2008 8:19 AM in response to V.K.
    What about an AppleScript saved as an Application that opens and quits on Login:

    tell application "Terminal"
    do script "/usr/sbin/diskutil unmount force disk0s3"
    quit
    end tell
  • by gbullman,

    gbullman gbullman Jun 25, 2008 8:20 AM in response to mister woofie
    Level 2 (301 points)
    Jun 25, 2008 8:20 AM in response to mister woofie
    I'm assuming that you are fairly comfortable with Terminal, and can relate how running this example command during startup can be modified for your purposes. This link describes how to set up a local DNS server and have it start during machine startup. Basically you need to translate the information in steps 3 & 4 for your purposes (steps are to be run as root which is logged into in step 1). If you use the /etc/hostconfig approach described in this link you can easily turn this script on and off.

    [Creating DNS Server Startup Item|http://www.macosxhints.com/article.php?story=20050420025219402]

    I followed these steps on a Tiger machine and it all worked as expected. Hopefully you are comfortable with one of the available UNIX editors.

    Hope this helps.
  • by V.K.,

    V.K. V.K. Jun 25, 2008 8:21 AM in response to macwiz1220
    Level 9 (56,110 points)
    Jun 25, 2008 8:21 AM in response to macwiz1220
    as I said, that's what you would do if you want to run it on login. the OP wants to run it on startup prior to login.
  • by Barney-15E,

    Barney-15E Barney-15E Jun 25, 2008 8:25 AM in response to mister woofie
    Level 9 (50,854 points)
    Mac OS X
    Jun 25, 2008 8:25 AM in response to mister woofie
    Download Lingon and set it to run as a daemon. Lingon does not run the script, but sets it up as a launchd process built into the OS.
  • by gbullman,

    gbullman gbullman Jun 25, 2008 8:30 AM in response to gbullman
    Level 2 (301 points)
    Jun 25, 2008 8:30 AM in response to gbullman
    Another piece of information is that MacOS may not always assign the same device name to the same partition during startup (at least that is what implied by this article). Speaking of disk0s3 in your initial post.

    This approach for moving the swap file to another drive uses a different approach for running the process (entry in the /etc/rc file). It also has information for verifying you are always dealing with the same disk partition, which may or may not be necessary in your case.

    [Moving swap file at Startup|http://www.math.columbia.edu/~bayer/OSX/swapfile>

    Hope this helps
  • by V.K.,

    V.K. V.K. Jun 25, 2008 8:32 AM in response to gbullman
    Level 9 (56,110 points)
    Jun 25, 2008 8:32 AM in response to gbullman
    the OP is clearly talking about an internal bootcamp partition. Its device name is fixed.
  • by macwiz1220,

    macwiz1220 macwiz1220 Jun 25, 2008 8:32 AM in response to gbullman
    Level 4 (1,940 points)
    Jun 25, 2008 8:32 AM in response to gbullman
    So why not unmount it my mount point. Instead of disk0s3, try /Volumes/Windows

    The mount point should always be the same.
  • by mister woofie,

    mister woofie mister woofie Jun 25, 2008 9:47 AM in response to mister woofie
    Level 1 (0 points)
    Jun 25, 2008 9:47 AM in response to mister woofie
    V.K. asks: "why do you need to do this before login?" And macwiz1220 suggests including in Login Items an app created by AppleScript that launches Terminal and runs the diskutil command.

    When I create and run such an app at login, I encounter problems with Terminal, which is why I thought to run an .sh script at startup. But if we can debug the problems with Terminal, then I won't have to venture into the more complex solutions offered by gbullman and Barney-15E. Terminal problems:

    (1) When Terminal prefs for Settings>Shell are set this way:
    (a) When the shell exits: Close the window.
    (b) Prompt before closing: Never
    then the result is: Terminal is launched and exits quickly, but the "Untitled" volume is not unmounted.

    (2) When Terminal prefs for Settings>Shell are set this way:
    (a) When the shell exits: Close the window if the shell exited cleanly
    (b) Prompt before closing: Never
    then again the result is: Terminal is launched and exits quickly, but the "Untitled" volume is not unmounted.

    (3) When Terminal prefs for Settings>Shell are set this way:
    (a) When the shell exits: Don't close the window
    (b) Prompt before closing: Always
    then after invoking the script, Terminal properly unmounts "Untitled" and presents a dialog: "You have 2 windows with running processes. Do you want to review these windows before quitting?" Here are the two Terminal windows:
    an echo of the cmd "/usr/sbin/diskutil unmount force disk0s3" followed by a report on return: "Volume on disk0s3 force unmounted." I don't want the user to have to see this and close the window.
    (ii) a fresh Terminal session, no commands entered. I also don't want the user to have to deal with this.

    How can I get Terminal to properly unmount the object, and shut down without demanding user interaction?

    P.S. what is an O.P.?
  • by V.K.,

    V.K. V.K. Jun 25, 2008 9:54 AM in response to mister woofie
    Level 9 (56,110 points)
    Jun 25, 2008 9:54 AM in response to mister woofie
    you don't have to deal with terminal at all.

    paste the following one-line script into Script Editor and save it as an application. Add the application to your login items

    --------------
    do shell script "diskutil unmount force disk0s3"
    ------------

    That's it. Personally, I don't unmount my bootcamp partition. I just added a dot to the front of its name so that it became invisible. I can still access it via sidebar but it doesn't occupy space on the desktop.

    P.S. OP = original poster

    Message was edited by: V.K.
  • by macwiz1220,

    macwiz1220 macwiz1220 Jun 25, 2008 10:16 AM in response to V.K.
    Level 4 (1,940 points)
    Jun 25, 2008 10:16 AM in response to V.K.
    Um, not. You need to do it with:

    tell application"Terminal"
    do script "...."
    quit
    end tell


    AppleScript needs to define the application to run the script.
  • by V.K.,

    V.K. V.K. Jun 25, 2008 10:28 AM in response to macwiz1220
    Level 9 (56,110 points)
    Jun 25, 2008 10:28 AM in response to macwiz1220
    macwiz1220 wrote:
    Um, not. You need to do it with:

    tell application"Terminal"
    do script "...."
    quit
    end tell


    AppleScript needs to define the application to run the script.



    no, it doesn't. besides your syntax is incorrect.
  • by macwiz1220,

    macwiz1220 macwiz1220 Jun 25, 2008 11:12 AM in response to V.K.
    Level 4 (1,940 points)
    Jun 25, 2008 11:12 AM in response to V.K.
    OK. Oops. I need a space. +tell application "Terminal"+

    Besides, the syntax is correct, I just ran it (with space, it's called a typo)
  • by V.K.,

    V.K. V.K. Jun 25, 2008 11:23 AM in response to macwiz1220
    Level 9 (56,110 points)
    Jun 25, 2008 11:23 AM in response to macwiz1220
    semantics, semantics. the larger point is that you don't need "tell application" block to run "do shell script command".
Page 1 Next