简体   繁体   中英

Start unicorn on OSX Startup

i'm currently using rvm and unicorn for server management under osx lion. i also use a gemset.

so for starting my server i do following:

cd /xyz/project
unicorn -c /xyz/project/config/unicorn.rb -E production

now i want this server starting when my computer starts up. i read something about adding a plist file to ~/Library/LaunchAgents/ and activating it with launchctl but i have no idea what to write inside this plist file for starting my server.

any ideas? also i think it's difficult, because the gemset needs to get activated by cd'ing into this dir.

thanks for all help.

You probably want to run this as a LaunchDaemon, not a LaunchAgent. Daemons run in system context, and can therefore run at system startup, before anyone's logged in. Agents run inside login sessions, and therefore don't start until a user logs in (and run as the user not as root, and if two users log in at once with fast switching they'll run a copy for each user, and...). The .plist file itself is pretty much the same for daemons vs. agents, the difference is whether you put it in /Library/LaunchDaemons or /Library/LaunchAgents.

The file itself depends on a few things. I'm assuming it needs to be started at system boot. Does it daemonize itself (ie drop into the background)? launchd doesn't like the programs it launches to daemonize themselves, as it wants to be able to monitor them and maybe restart them if they crash/exit. If unicorn has an option to not daemonize, use that; if not, you need to change the .plist file a bit to adapt to it. First, here's a basic run-at-startup LaunchDaemon .plist file:

<?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>Disabled</key>
        <false/>
        <key>Label</key>
        <string>local.unicorn</string>
        <key>ProgramArguments</key>
        <array>
                <string>/full/path/to/unicorn</string>
                <string>-c</string>
                <string>/xyz/project/config/unicorn.rb</string>
                <string>-E</string>
                <string>production</string>
        </array>
        <key>WorkingDirectory</key>
        <string>/xyz/project</string>
        <key>RunAtLoad</key>
        <true/>
        <key>EnableTransactions</key>
        <false/>
</dict>
</plist>

If unicorn daemonizes itself, you'll need to add this (before the </dict> line):

        <key>KeepAlive</key>
        <false/>
        <key>AbandonProcessGroup</key>
        <true/>

If it doesn't daemonize (or you can get it to skip daemonizing by changing the ProgramArguments), you can optionally add this instead:

        <key>KeepAlive</key>
        <true/>

Name the file something like /Library/LaunchDaemons/local.unicorn.plist (the name should match the label), set the ownership to root:wheel, and the permissions to 600. You can activate it with sudo launchctl load /Library/LaunchDaemons/local.unicorn.plist , or by rebooting.

EDIT: for troubleshooting, you can add something like this to the .plist file:

        <key>StandardOutPath</key>
        <string>/tmp/unicorn.out</string>
        <key>StandardErrorPath</key>
        <string>/tmp/unicorn.err</string>
        <key>Debug</key>
        <true/>

Then unload ( sudo launchctl unload /Library/LaunchDaemons/local.unicorn.plist ) and reload it, and check /var/log/system.log, /tmp/unicorn.out, and /tmp/unicorn.err for hints about what's going wrong.

EDIT2: to run as a different user, add something like:

        <key>UserName</key>
        <string>choise</string>

EDIT3: I'm not very familiar with rvm and how it handles its configuration, but it sounds like you need to set some environment variables to set it up properly. Since you aren't cd'ing into the directory in a regular shell, the .rvmrc file never gets run. There are several ways to solve this.

First, you can figure out what environment variables need to be set, and add those to the .plist file with something like this:

        <key>EnvironmentVariables</key>
        <array>
                <key>ruby_string</key>
                <string>ruby-1.9.2-p136</string>
                <key>gemset_name</key>
                <string>unicorn</string>
        </array>

...but that may be a little fragile, especially if they ever change; you'd need to update both the .rvmrc and .plist files together for it to work consistently.

It might be better to have it actually open a shell and source all of the necessary setup files before launching unicorn. You could do this with a shell script, or just by including the necessary command sequence as a (single long) parameter to the shell. To do this, replace the ProgramArguments section with something like this:

        <key>ProgramArguments</key>
        <array>
                <string>/bin/bash</string>
                <string>-c</string>
                <string>source /etc/rvmrc; source /Users/server/.rvmrc; source .rvmrc; /Users/server/.rvm/gems/ruby-1.9.2-head@q/bin/unicorn -c /Users/server/Sites/Rails/q/config/unicorn.rb -E production</string>
        </array>

(but leave out sourceing any of the rvmrc files that don't exist.)

Create a bash script:

#!/bin/sh
cd /xyz/project
unicorn -c /xyz/project/config/unicorn.rb -E production

Save it and make it executable:

chmod +x scriptname.sh

Then, assuming you are running OS X, add this file in System Preferences > Accounts > Login Items.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM