Starting Perforce on Mac OS X
Two methods to start Perforce on Mac OS X at boot time.
TASK
Automatically start your Perforce server at boot time on Mac OS X
SOLUTION
There are two different solutions to automatically starting your Perforce Server at boot time.- For users running OS X version 10.4 and higher, Apple's recommended approach is to use the launchd mechanism.
- For users of Mac OS X prior to 10.4, the recommended approach is to write a Startup Item.
Using Launchd to start Perforce (OS X 10.4 and higher)
launchd is Apple's new mechanism (Versions 10.4 "Tiger" and later) for handling various launching services that were previously handled by other approaches, including (x)inetd, rc, and cron.
The launchd service uses the XML-based plist file format to store application preferences and configuration information. The plist files are editable using the Apple Property List Editor, or any text editor. There are also third party utilities designed to create the plist files using a graphical interface, both for launchd and other applications that use plist files.To launch Perforce on startup, create a text file named com.perforce.plist, and place it in '/Library/LaunchDaemons/'. The following is an example of a Perforce launchd plist file. This is a sample script only. As always, it is highly recommended that you test this script first before installing it on a production server.
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Debug</key> <false/> <key>Label</key> <string>com.perforce</string> <key>OnDemand</key> <false/> <key>ProgramArguments</key> <array> <string>/<path-to-p4d>/p4d</string> </array> <key>EnvironmentVariables</key> <dict> <key>P4LOG</key> <string>/<path-to-p4log>/p4log.log</string> <key>P4PORT</key> <string><port-number></string> <key>P4ROOT</key> <string>/<path-to-p4root>/p4root</string> </dict> <key>RunAtLoad</key> <true/> <key>ServiceDescription</key> <string>Launches Perforce Server</string> </dict> </plist>
Keys under EnvironmentVariables are Perforce variables that must be set for the user shell. These variables also must be set for the user shell with a .profile file (or equivalent) if you are running a command-line client on the OS X server.
The StartInterval key avoids one of the weaknesses of launchd: the lack of dependencies. When the plist is loaded at system boot, it will start regardless of whether the network is running. If the network is not ready, the Perforce server will fail to load. The StartInterval attempts to launch Perforce every 30 seconds until it succeeds.
The only drawback to the StartInterval is that, if you stop the server, either using p4 admin stop or killing its process, it will restart 30 seconds later. The workaround for this problem is to replace the ProgramArguments key with the path to an executable shell script that inserts a delay before running the same Perforce server startup command.
In some cases, keeping the server running in such a way is a desirable side effect of the StartInterval key. To unload the launchd item (and thus keep Perforce from endlessly restarting while you are trying to address a problem on the server), you must issue a command similar to this one:
sudo launchctl unload /Library/LaunchDaemons/com.perforce.plistAfter entering your admin password, the launchd item will unload and the Perforce server shuts down. To restart it without rebooting the server machine, issue the "launchctl load" command with the same syntax:
sudo launchctl load /Library/LaunchDaemons/com.perforce.plistThese launchctl commands are useful for testing the launchd items without rebooting.
Using StartupItems (pre OS X 10.4)
To configure your Perforce Server to start when OS X boots you must create a StartupItem for the p4d executable. The process for creating an OS X StartupItem is covered in detail on the Apple developer website here:Briefly, to create an OS X startup item:
- Create a StartupItem directory with a name that includes the executable name. For example:
mkdir /StartupItems/p4d
- Add the executable file to the directory you created in Step #1. For example:
/StartupItems/p4d/p4d
- Create a script to start/stop/restart the program. A startup script template can be found here:
/etc/rc.common
- Create a Preferences List (plist) file for p4d and save it in the startup items directory. For example:
/StartupItems/p4d/p4d.plist
Please see the Apple knowledge base link above for more extensive details about configuring startup items.
