nFoldMonOnly Startup Automation

Once you have testing your nFoldMonOnly configuration manually, you will probably want to automate its startup so that it is always running. There are a number of ways to do this. Here are two suggestions: one easy and a bit ugly, one hard and elegant.

Easy: Login Item

The easy way to start something automatically is, of course, to set it up as a "Login Item" startup:

Unfortunately, only Applications can be made into login Startup Items, and nFoldMonOnly is not an application, it is a unix process. The solution is to have a little mini-application that does nothing except run nFoldMonOnly.

Such a mini-application is provided with nFoldMonOnly. It is a text file called "nFoldManLoginItem.command". The ".command" suffix tells Mac OS X that it is a script written in Unix shell script language, that should be run when it is double-clicked.

To set up a Login Item using this shell:

  1. Edit the "nFoldManLoginItem.command" file with a text editor and change the string "PATH" to the absolute path name of the directory containing the nFoldMonOnly program.
  2. Open your System Preferences, Accounts panel, and use the Startup Items tab to add nFoldManLoginItem.command as a Startup Item:

When you log in, the command will run, and will leave nFoldMonOnly running.

Unfortunately, running a command this way opens a Terminal window and leaves it open, which is a bit messy. See below for a neater technique.

Harder: StartupItem Script

A neater way to automate the startup of a Unix task is to set it up as a system StartupItems script. This technique has advantages and disadvantages:

Advantages
Disadvantages
  • Can run when you log on, or as soon as system boots (even without a user logged on).
  • Invisible - no terminal windows open to clutter the screen.
  • Invisible - no way to see console messages, so if the program is trying to tell you something, you miss it.
  • Difficult to set up. If you make a configuration error, you get no error messages or feedback, it just doesn't work.
  • Potentially dangerous - processes run this way run as "root" by default. (My instructions below show you how to prevent this.)
  • Requires familiarity with Unix commands, and Administrator status to set up.

Introduction

StartupItems are a built-in feature of Mac OS X. They are used by the system and by commercial software to launch various background processes at system boot time or user login time. If you're not already familiar with the concepts behind StartupItems, there are a number of good introductory articles on the Internet. One good one is here at MacDevCenter .

Location

A StartupItem is a set of files in a directory. The directory can be in one of 3 locations:

your-user-id/Library/StartupItems StartupItems placed here will be run when you log in.
/Library/StartupItems StartupItems placed here will be run when the system boots, even before anyone logs in (a great way to keep your folding task active all the time).
/System/Library/StartupItems Like /Library/StartupItems but reserved for System use - may not survive an OS upgrade.

I suggest putting your StartupItem in /Library/StartupItems so it will run even when no one is logged in, unless you really want to restrict the action to just one of the authorized users. For the rest of this discussion, we'll use /Library/StartupItems as our location.

File Structure

Your StartupItem will consist of a directory inside the /Library/StartupItems directory. Let's assume the directory name will be "nFoldMonOnly". Inside that directory there must be two text files, one called "StartupParameters.plist" and one called exactly the same name as the directory -- "nFoldMonOnly" in this case.

PList File

The "StartupParameters.plist" file tells the Mac startup management system some basic information about the task you are defining, such as what to call it in the login status window. More important it tells it when in the Mac boot sequence this startup should be run.

The sample file that came with nFoldMonOnly looks like this:

{
Description = "nFoldMan Monitor Only";
Provides = ("Folding");
Requires = ("Network", "Disks");
OrderPreference = "Last";
}

The "Requires" and "OrderPreference" lines say that this process should be among the last things started during the boot process, and that it must not be started until the network and disk systems are available.

Startup Script File

The other file in the directory must have the same name as the directory -- "nFoldMonOnly" in this example. It is an executable script that will be run during the system startup sequence.

The sample file that came with nFoldMonOnly (which you will need to edit) looks like this:

# The following line runs the standard startup script stuff supplied by Mac OS

. /etc/rc.common

# Put a message in the startup window
ConsoleMessage "Starting nFoldMonOnly monitor"
# Leave the following 2 commands on one line # Replace PATH with the absolute path to the directory containing the nFoldMonOnly program # Replace YOURUSER with the userid under which you want to run your folding. This will normally # be your primary userid. Don't use "root" -- you don't want nFoldMonOnly, or the folding # client, running as root. It's not good practice to let any software you got from a # stranger (like me) run as root on your system.
cd PATH; su YOURUSER -c "./nFoldMonOnly ./textControlFile.txt" >console &

(Actually the file is somewhat longer than this, as it has more comments in it, but this will give you the idea.)

You should edit this file and make 3 changes:

  1. Change "PATH" to the absolute path name to your nFoldMonOnly program, where ever it is located. This would be "/fah" in the example we're using here.
  2. Change "YOURUSER" to the userid with which you log on to Mac OS X. (The short unix-style name, not your full name.)
  3. Change "textControlFile.txt" to whatever you named your control file in that directory, if you didn't use that name.

Protecting Yourself From Root Access

Warning: StartupItems are executed by the system during startup, so they run in root status. It is very dangerous to let software written by a stranger run in root mode on your system. You shouldn't trust me or the authors at Stanford to do this.

That's what the "su" command at the bottom of the script above is for. It runs the nFoldMonOnly program as your regular userid, not as root, so it won't have any special privileges. And when it launches the folding client, it also will run as you, without special privileges.

File Permissions

Now some finicky details. The system tries to protect you from authoring StartupItem scripts if you don't know what you're doing. So, the StartupItem will not run unless several conditions are met. (I'm going to assume you know how to use the Unix commands mentioned below. If not, look them up in the "man" command, or see the StartupItems tutorial mentioned above.)

  1. The directory you created in /Library/StartupItems must be owned by "root" and group "admin". Use the "chown" and "chgrp" commands from Terminal to do this.
  2. The two files inside the directory must both be owned by "root" and group "admin".
  3. The script file must be named exactly the same as the directory.
  4. The script file must be readable and executable by root -- use "chmod" to do this.

Ok, Test It

Restart your computer. Carefully watch the startup window and you should see the messages listed above go by. After your startup completes, you should find (using Activity Monitor) that nFoldMonOnly is running in the background, owned by your normal userid. If it isn't, something in the above steps wasn't done exactly right -- go through the instructions carefully.