Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

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

Reply
17 replies

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.

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

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.

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

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

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.

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.

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)

Jun 25, 2008 11:36 AM in response to V.K.

Well that is different. We have two different AppleScripts.

I was using
do script

You were using
do shell script

They both work depending on what you need.

running shell script at startup (before login)

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.