Perforce Public Knowledge Base - Starting Perforce on Mac OS X
Reset Search
 

 

Article

Starting Perforce on Mac OS X

« Go Back

Information

 
Problem

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 mechanism (Versions 10.4 "Tiger" and later) for handling various launching services that were previously handled by other approaches, including (x)inetdrc, 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/". This file needs to be set to belong to the "wheel" group and owned by "root" as below:

cd /Library/LaunchDaemons/
chown root:wheel com.perforce.plist
ls com.perforce.plist
  -rw-r--r--  1 root  wheel  797 11 Nov 11:01 com.perforce.plist

The following is an example of a Perforce launchd plist file. At the minimum, the following attributes needs to be changed to reflect your environment before installing to your system:

  • path-to-p4d
  • port-number
  • path-to-p4log
  • path-to-p4root

A property list editor like the one that comes with Apple's Xcode can be useful to edit the plist file through a graphical user interface.

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>
      <key>P4ZEROCONF-disabled</key>
      <string>Rename this variable to "P4ZEROCONF" to enable zeroconf</string> 
      <key>P4NAME</key>
      <string>MyPerforceServer</string> 
      <key>P4DESCRIPTION</key>
      <string>Connect here to for SCM goodness.</string> 
    </dict>
    <key>RunAtLoad</key>
    <true/>
    <key>ServiceDescription</key>
    <string>Launches Perforce Server</string>
    <key>StartInterval</key>
    <integer>180</integer> 
    <key>KeepAlive</key>
    <true/>
  </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.

2008.2 and later Perforce server revisions can be configured to broadcast zeroconf information. You need to edit the appropriate section in the plist file above to enable the Zeroconf support. Please refer to the KB article Perforce Zeroconf Support for more details.

See the Perforce Server (P4D) Reference for details on options and environment variables.

The StartInterval key, combined with KeepAlive can be useful to restart the server automatically after a timeout. For example with StartInterval set to 180, KeepAlive set to true and you stop the server with p4 admin stop, the server will get restarted after 180 seconds. To restart the server without a timeout, set KeepAlive to true without setting StartInterval.

In the above example, the StartInterval and KeepAlive key are active. You can add comment tags if you want the StartInterval key to be ignored.

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

After entering your admin password, the launchd item unloads 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.plist

These launchctl commands are useful for testing the launchd items without rebooting.

This same approach can be used for other Perforce products that may need to be started at boot time on Mac OS X as well. For example, P4Web may have a .plist file similar to:

<?xml version="1.0" encoding="us-ascii"?>
<!DOCTYPE plist PUBLIC "-//Apple//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-p4web/p4web</string>
    </array>
    <key>EnvironmentVariables</key>
    <dict>
      <key>P4WEBLOG</key>
      <string>/var/logs/p4web.log</string>
      <key>P4PORT</key>
      <string>perforce:1666/string>
      <key>P4CLIENT</key>
      <string>web_client</string>
      <key>P4USER</key>
      <string>a_user</string>
      <key>P4WEBPORT</key>
      <string>8090</string>
      <key>P4WEBSERVICEFLAGS</key>
      <string>-b</string>
    </dict>
    <key>RunAtLoad</key>
    <true/>
    <key>ServiceDescription</key>
    <string>Launches Perforce Web Client</string>
    <key>StartInterval</key>
    <integer>180</integer>
    <key>KeepAlive</key>
    <true/>
  </dict>
</plist>
 
This would be saved in the same way, with a unique name (such as com.p4web.plist) and can loaded using sysctl:
 
sudo launchctl load /Library/LaunchDaemons/com.p4web.plist
 
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.

Briefly, to create an OS X startup item:

  1. Create a StartupItem directory with a name that includes the executable name. For example:
    mkdir /StartupItems/p4d
    
  2. Add the executable file to the directory you created in Step #1. For example:
    /StartupItems/p4d/p4d
  3. Create a script to start/stop/restart the program. A startup script template can be found here:
    /etc/rc.common
  4. 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.

Related Links

Feedback

 

Was this article helpful?


   

Feedback

Please tell us how we can make this article more useful.

Characters Remaining: 255