Start Unicorn on Osx Startup

Start unicorn on OSX Startup

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

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

Unicorn fails to start with Could not find multi_json-1.8.0 in any of the sources (Bundler::GemNotFound)

The problem was solved by executing: bundle install --no-deployment through ssh and then starting unicorn with bundle exec: bundle exec unicorn -c /home/rails/current/config/unicorn.rb -E production -D

Why do I get an error when starting unicorn when the system boots?

There are a few ways to make it work:

  1. Following your code:

    PATH=/home/josue/.rvm/gems/ruby-1.9.3-p0/bin:/home/josue/.rvm/gems/ruby-1.9.3-p0@global/bin:/home/josue/.rvm/rubies/ruby-1.9.3-p0/bin:/home/josue/.rvm/bin:$PATH
    GEM_HOME=/home/josue/.rvm/gems/ruby-1.9.3-p0
    GEM_PATH=/home/josue/.rvm/gems/ruby-1.9.3-p0:/home/josue/.rvm/gems/ruby-1.9.3-p0@global
  2. Using rvm wrappers: https://rvm.io/integration/init-d/

  3. Or others: https://rvm.io/integration/cron/

Nginx and Unicorn) Cannot access local server

Problem solved when I moved the gem 'unicorn' under production environment. After this, I run 'rails s' command and Puma booted up, finally I can access local server.

Thank you so much!

Unicorn Freezes on Start

checking manually from the command line should show you some output. for example, I see:

I, [2015-05-16T16:36:12.487217 #63958]  INFO -- : listening on addr=0.0.0.0:8080 fd=9
I, [2015-05-16T16:36:12.487321 #63958] INFO -- : worker=0 spawning...
I, [2015-05-16T16:36:12.488381 #63958] INFO -- : master process ready
I, [2015-05-16T16:36:12.489353 #63959] INFO -- : worker=0 spawned pid=63959
I, [2015-05-16T16:36:12.489799 #63959] INFO -- : Refreshing Gem list
I, [2015-05-16T16:36:16.198329 #63959] INFO -- : worker=0 ready

Do you see anything using this method or in unicorn.sterr.log? Please post your unicorn config file.

SOLUTION: it appears that the pid filepath was not writable by the unicorn process. Changing the permission to allow for access by the owner of the process seems to have worked.

How to auto-load MySQL on startup on OS X Yosemite / El Capitan

This is what fixed it:

First, create a new file: /Library/LaunchDaemons/com.mysql.mysql.plist

<?xml version="1.0" encoding="UTF-8"?>
<plist version="1.0">
<dict>
<key>KeepAlive</key>
<true />
<key>Label</key>
<string>com.mysql.mysqld</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/mysql/bin/mysqld_safe</string>
<string>--user=mysql</string>
</array>
</dict>
</plist>

Then update permissions and add it to launchctl:

sudo chown root:wheel /Library/LaunchDaemons/com.mysql.mysql.plist
sudo chmod 644 /Library/LaunchDaemons/com.mysql.mysql.plist
sudo launchctl load -w /Library/LaunchDaemons/com.mysql.mysql.plist

Unicorn restarting in old release directory

Had a reference to "Rails.root" in one of my Rake tasks which was causing the symlink to resolve. Replaced that with a hardcoded reference to the shared directory and it worked.



Related Topics



Leave a reply



Submit