在OSX启动上启动独角兽 [英] Start unicorn on OSX Startup

查看:116
本文介绍了在OSX启动上启动独角兽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在osx lion下使用rvm和unicorn进行服务器管理. 我也使用宝石.

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

现在,我希望在计算机启动时启动该服务器. 我读了一些有关将plist文件添加到~/Library/LaunchAgents/并用launchctl激活它的信息,但是我不知道在该plist文件中写什么来启动服务器.

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.

有什么想法吗?我也认为这很困难,因为需要通过cd'ing进入这个目录来激活gemset.

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

感谢所有帮助.

推荐答案

您可能希望将其作为LaunchDaemon(而不是LaunchAgent)运行.守护程序在系统上下文中运行,因此可以在任何人登录之前在系统启动时运行.代理在登录会话中运行,因此只有在用户登录后才能启动(并以用户身份而不是以root用户身份运行,如果有两个用户,则运行快速切换一次登录,他们将为每个用户运行一个副本,然后...). .plist文件本身对于守护程序与代理来说几乎是相同的,不同之处在于是将其放在/Library/LaunchDaemons还是/Library/LaunchAgents中.

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.

文件本身取决于几件事.我假设它需要在系统启动时启动.它会自己守护(即掉入后台)吗? launchd不喜欢它启动的程序来守护自己,因为它希望能够监视它们,并可能在崩溃/退出时重新启动它们.如果独角兽可以选择不守护进程,请使用它;如果不是,则需要稍微更改.plist文件以适应它.首先,这是一个基本的启动时启动LaunchDaemon .plist文件:

The file itself depends on a few things. I'm assuming it needs to be started at system boot. Does it daemonize itself (i.e. 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>

如果unicorn自己守护,则需要添加它(在</dict>行之前):

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

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

如果它不守护进程(或者您可以通过更改ProgramArguments使其跳过守护进程),则可以选择添加它:

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/>

将该文件命名为/Library/LaunchDaemons/local.unicorn.plist(名称应与标签匹配),将所有权设置为root:wheel,并将权限设置为600.您可以使用sudo launchctl load /Library/LaunchDaemons/local.unicorn.plist激活它,或通过重启.

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.

要进行故障排除,您可以向.plist文件中添加以下内容:

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/>

然后卸载(sudo launchctl unload /Library/LaunchDaemons/local.unicorn.plist)并重新加载它,并检查/var/log/system.log、/tmp/unicorn.out和/tmp/unicorn.err,以获取有关发生问题的提示.

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.

要以其他用户身份运行,请添加以下内容:

to run as a different user, add something like:

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

我对rvm及其如何处理其配置不是很熟悉,但是听起来您需要设置一些环境变量来正确设置它.由于您不在常规外壳程序中进入该目录,因此.rvmrc文件将永远不会运行.有几种解决方法.

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.

首先,您可以弄清楚需要设置哪些环境变量,并使用类似以下内容将它们添加到.plist文件中:

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>

...但是那可能有点脆弱,尤其是当它们改变时;您需要同时更新.rvmrc和.plist文件,以使其始终如一地工作.

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

在启动独角兽之前,最好实际上打开它的外壳并source所有必要的安装文件.您可以使用Shell脚本执行此操作,也可以仅通过将必要的命令序列作为(单个)参数添加到Shell中来执行此操作.为此,将ProgramArguments部分替换为以下内容:

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>

(但要排除任何不存在的rvmrc文件的来源.)

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

这篇关于在OSX启动上启动独角兽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆