Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > macOS > Leopard Startup (and Shutdown) Scripts

Leopard Startup (and Shutdown) Scripts
Thread Tools
l008com
Addicted to MacNN
Join Date: Jan 2000
Location: Stoneham, MA, USA
Status: Offline
Reply With Quote
Dec 24, 2009, 02:10 PM
 
That age old question, how do you make a startup script in OS X? I also need to make a shutdown script. I know you used to use StartupItems, then they switched it to LaunchAgents. But I've looked into LaunchAgents, and they really look like they're for launching apps more than running commands. And the syntax seems complicated. Beyond that, I haven't been able to find a thing about shutdown scripts.
     
larkost
Mac Elite
Join Date: Oct 1999
Location: San Jose, Ca
Status: Offline
Reply With Quote
Dec 24, 2009, 03:37 PM
 
This is a complicated space, and depending on what you want to do some combination of the following would be useful:

1) launchagents or launchdaemons are probably the best general contenders. There is a lot of power available in that system, and it is the one that Apple has anointed as the "future". Because of all the options available it can seem a little daunting, but most of the plists come down to:
Code:
<plist><dict> <key>Label</key> <string>The.Name.of.the.File.Minus.the.plist</sting> <key>ProgramArguments</key> <array> <string>/absolute/path/to/program</string> <string>argument_one</string> </array> <key>RunAtLoad</key> <true/> </dict></plist>
LaunchDaemons run right at boot (right when networking is setting up, and before the GUI, so be careful to wait for the resources you need) and run as the root user. They are good for low-level things, and things that need root privileges. LaunchAgents launch when someone logs in (autologin counts), and so are more appropriate for things like launching programs.

2) LoginItems are still a valid way of running things like programs that you want launched when the user logs in. And you can make them system-wide and not changeable by an individual user.

3) Login and logout hooks are also a nice way of running things. This can nicely catch the logout event (assuming that it is clean).

No matter what way you do this you are going to find that Apple has not included straightforward ways of running things at shutdown. The reason for this is that they have realized that this is a complicated thing, and the assumption that you can catch that and go on is a bad one. Rather they are pushing people to handle the more complicated cases (computer put to sleep and moves network locations while asleep, power goes out, etc).

The best way of catching when the computer is going down is to run something as a LaunchDaemon, and then have is sleep indefinitely once it has done its setup (sleep some insane amount of time, with a loop around that line), and then catch the TERM signal that gets sent durring shutdown. This is a nice first step to also starting to catch things like sleep, and that leads to you down the path to catch things like network change events.
     
l008com  (op)
Addicted to MacNN
Join Date: Jan 2000
Location: Stoneham, MA, USA
Status: Offline
Reply With Quote
Dec 27, 2009, 03:20 AM
 
So it turns out, shutdown is pretty easy. just make a file /etc/rc.shutdown.local
Put commands in there, and they run at shutdown. So I put test commands in there and in /etc/rc.common and they both worked. But my scripts still have some bugs I'm working out. My scripts are relatively simple. I just want to create a ram disk on boot and copy data to it. Then at shutdown, I want to copy that data back to the disk. And yes, I will also periodically backup the ram disk too so I'll be ok in the rare event of an unexpected hard shutdown. Make a launch agent seems too complicated. Or at least, more complicated than I need.
     
l008com  (op)
Addicted to MacNN
Join Date: Jan 2000
Location: Stoneham, MA, USA
Status: Offline
Reply With Quote
Dec 27, 2009, 05:26 PM
 
OK New problem. My shutdown script and startup script do run. But the Shutdown script fails. It fails because it is run AFTER the ram disk is unmounted.

AND the startup script fails too. I made a launchdeamon for it. But it doesn't work. When I run the script from the terminal, as myself or as root, it works fine and creates my ram disk. When launchd runs it, it seems to 'half' make the ram disk. There is an item in /Volumes with the right name, but it has the wrong icon. It doesn't appear on my desktop. And while I can navigate through it in the finder, Apache can't seem to read anything in it, despite anything I do to permissions in that folder.
     
Hal Itosis
Grizzled Veteran
Join Date: Mar 2004
Status: Offline
Reply With Quote
Dec 28, 2009, 02:40 AM
 
Post the plist for starters.

Also, i think the /Library/LaunchAgents folder would be a more appropriate place for it (and it needs to be root-owned:wheel-grouped, which you knew... right?).
-HI-
     
l008com  (op)
Addicted to MacNN
Join Date: Jan 2000
Location: Stoneham, MA, USA
Status: Offline
Reply With Quote
Dec 30, 2009, 02:53 AM
 
Agents are only run when someone logs in, which you knew... right?

I don't have the plist, I deleted it when it wasn't working. But it is being run, its just not working as it should. So wouldn't that point to the script itself, not the launchdeamon plist?
     
Hal Itosis
Grizzled Veteran
Join Date: Mar 2004
Status: Offline
Reply With Quote
Dec 30, 2009, 12:33 PM
 
Originally Posted by l008com View Post
Agents are only run when someone logs in, which you knew... right?
So what? I have several. They can still be periodic via StartCalendarInterval (which mine are), and oh BTW... mine work.

Originally Posted by l008com View Post
I don't have the plist, I deleted it when it wasn't working. But it is being run, its just not working as it should. So wouldn't that point to the script itself, not the launchdeamon plist?
What are folks who haven't seen either the script or the plist (or a listing of them showing ownership, etc.) supposed to do? Guess? Okay... i guess you made a mistake in one (or both) of them.
-HI-
     
l008com  (op)
Addicted to MacNN
Join Date: Jan 2000
Location: Stoneham, MA, USA
Status: Offline
Reply With Quote
Jan 1, 2010, 12:32 AM
 
Here is my script for making the ramdisk (needs to be a startup item):
Code:
#!/bin/bash ## make a ram disk that is approximaly 1GB in size /usr/sbin/diskutil erasevolume HFS+ "web-ram" `/usr/bin/hdiutil attach -nomount ram://2200000` ## copy contents of the ramdisk backup to the ramdisk cp -R /webfolder/web-ram-backup/* /Volumes/web-ram/
Here is my script for backing up the ramdisk (needs to be a shutdown item):
Code:
#!/bin/bash ## empty ram disk backup folder rm -R /webfolder/web-ram-backup/* ## copy contents of the ramdisk to the ramdisk backup cp -R /Volumes/web-ram/* /webfolder/web-ram-backup/
I deleted my Launch Daemon days ago when it didn't work right. So rather than me trying to post what I think I had, why don't you just tell me what a proper plist should look like for the startup script, I'll make the script that way, and we can then try to figure out why it fails.

To recap:
  • Startup script works when you call it manually from the terminal, but doesn't work properly when launched from launchd.
  • Shutdown script (when ran from rc.shutdown.local) doesn't work because the ram disk is ejected before the script runs.
  • Even if you SSH in and make the ram disk from the terminal, when you log into the GUI, the ram disk is ejected when you log out. Pretty good chance this problem IS the problem the shutdown script is having. If there is a workaround here, I can put the shutdown script back in rc.shutdown.local, then it will just be a matter of making the startup script work, either via rc.whatever, or via launchd. The periodic running of the backup script is not something we need to address here.
     
l008com  (op)
Addicted to MacNN
Join Date: Jan 2000
Location: Stoneham, MA, USA
Status: Offline
Reply With Quote
Jan 1, 2010, 04:21 AM
 
So, adding a 10 second sleep to the startup script seems to have done the trick for that. I'd prefer a way to make the script wait specifically for whatever it needs, rather than just indirectly waiting for 10 seconds and hoping that remains long enough.

But even though I get a ram disk boot, if I log into my account, then log out, the ram disk is ejected.
     
l008com  (op)
Addicted to MacNN
Join Date: Jan 2000
Location: Stoneham, MA, USA
Status: Offline
Reply With Quote
Jan 1, 2010, 04:41 AM
 
. . . AAAAAAAAAAND `-notremovable` this flag makes a mounted disk image (my ram disk) look and act like a hard drive, meaning no unmount at logout. At this point I may actually be good to go. Further testing required.
     
Hal Itosis
Grizzled Veteran
Join Date: Mar 2004
Status: Offline
Reply With Quote
Jan 1, 2010, 03:40 PM
 
Just to be safe, use full pathnames to cp and rm (as you did with diskutil and hdiutil):

/bin/cp
/bin/rm

Instead of adding a delay, perhaps try putting the plist in your home library...
/Users/you/Library/LaunchAgents/scriptname


-if not already there, that "LaunchAgents" folder will need to be created.
-the plist posted by larkost looks perfect to me.
-HI-
     
l008com  (op)
Addicted to MacNN
Join Date: Jan 2000
Location: Stoneham, MA, USA
Status: Offline
Reply With Quote
Jan 1, 2010, 11:30 PM
 
if i put it in /Users/you/Library/LaunchAgents/scriptname, when will it be launched?
     
Hal Itosis
Grizzled Veteran
Join Date: Mar 2004
Status: Offline
Reply With Quote
Jan 2, 2010, 03:29 AM
 
When you login.
-HI-
     
l008com  (op)
Addicted to MacNN
Join Date: Jan 2000
Location: Stoneham, MA, USA
Status: Offline
Reply With Quote
Jan 2, 2010, 07:28 AM
 
ok, i thought i was already clear, but that does not help me at all. I need startup and shutdown (which i have, the scripts are working perfectly now) not login and logout.
     
   
Thread Tools
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 12:45 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,