runit 

Send to Kindle
home » snippets » daemon » runit



Snippets

Running stuff / etc

# Run a service from a directory
runsv /path/to/service
sudo runsv /etc/service/chirayu_services
sudo runsv /opt/local/var/service/chirayu_services

# Restart a running service
sudo sv restart /etc/service/chirayu_services
sv restart ~/service/srv_running_as_current_user

# Status
sudo sv status /etc/service/*
sudo sv status /opt/local/var/service/*
sv status ~/service/*

Installing on Linux

sudo apt-get install -y runit
# Services reside in /etc/service

Installing on Mac OS X

via MacPorts.

# Install runit.
sudo port install runit

# Install the startup helper item.
sudo port load runit

Note:  If you inspect the launchctl plist file at /opt/local/etc/LaunchDaemons/org.macports.runit/org.macports.runit.plist, you'll notice that the service is Disabled. Yet, it loads at system startup!  This is because, sudo launchctl load -w stores an override value for the Disabled key in the file /private/var/db/launchd.db/com.apple.launchd/overrides.plist.  This tidbit isn't obvious from man launchctl but it does tell you that it stores it "elsewhere on disk".  For non-sudo load -w, the file is /var/db/launchd.db/com.apple.launchd.peruser.UID/overrides.plist (Replace UID with your user id – e.g. from id -u) Sadly, LingOn does not use this information when it shows you the Disabled status for a plist.

See SO and launchd.info

# The startup item supervises services in
# /opt/local/var/service by default.  So create it.

sudo mkdir /opt/local/var/service

# Also create a parallel directory.  We'll actually
# create the service directories under this tree and
# symlink the ones we want to actually have running to
# under /opt/local/var/service.
sudo mkdir /opt/local/var/service_all

# Allow my user to manage my own services.
sudo mkdir /opt/local/var/service_all/chirayu_services

# Remember to symlink this in after we create the run
# script, the log folder and the log/run script.
#
#   sudo ln -s /opt/local/var/service{_all,}/chirayu_services

# Add a run script.
sudo vim /opt/local/var/service_all/chirayu_services/run

Contents of /opt/local/var/service_all/chirayu_services/run

#!/bin/sh
# Should only write to stdout.
exec 2>&1
#  TODO TODO TODO TODO -*-*-*-*-
export PATH=...  # Paste current PATH here.

# the ...... represent placeholders.  The process will print to
# stdout/err every 5 seconds for each . placeholder you use.
sudo -H -u chirayu runsvdir -P /Users/chirayu/service 'log:...................................................................................................................................'

Back to our setup.

sudo chmod 700 /opt/local/var/service_all/chirayu_services/run
sudo chmod 700 /opt/local/var/service_all/chirayu_services/log/run

# Add logging.
# Logs will be in this directory.
# Note:  You may choose to log to /var/log/... or
# somewhere else instead.
mkdir -p /Users/chirayu/logs/chirayu_services

# The run script.
sudo vim /opt/local/var/service_all/chirayu_services/log/run

/opt/local/var/service_all/chirayu_services/log/run

#!/bin/sh
# cd to the logging directory so that when chpst
# switches to the chirayu user, PWD is accessible by chirayu.
LOG_DIR=/Users/chirayu/logs/chirayu_services
cd $LOG_DIR
exec chpst -uchirayu svlogd -tt $LOG_DIR

Now for the symlink.

sudo ln -s /opt/local/var/service{_all,}/chirayu_services

This should now have a runsvdir watching and running services for user chirayu under /Users/chirayu/service.  Now I can create a subdirectory (and associated log/run) for each service I want to manage as chirayu.