Skip to content

Instantly share code, notes, and snippets.

@takeyuweb
Last active December 16, 2015 03:18
Show Gist options
  • Save takeyuweb/5368460 to your computer and use it in GitHub Desktop.
Save takeyuweb/5368460 to your computer and use it in GitHub Desktop.
/etc/init.d/god (with rbenv) & samples イベント監視を使う場合は http://blog.takeyu-web.com/entry/2013/02/05/013112 (CentOS)
# vi /etc/init.d/god
# mkdir /etc/god
# vi /etc/god/mt.god
# vi /etc/god/mt-task.god
# service god start
/usr/local/rbenv/shims/god -l /var/log/god.log -P /var/run/god.pid
god: loading /etc/god/mt.god ...
Sending 'load' command with action 'leave'
The following tasks were affected:
movabletype
god: loading /etc/god/mt-task.god ...
Sending 'load' command with action 'leave'
The following tasks were affected:
mt run-periodic-tasks
# chkconfig god on
# service god cmd restart mt
RAILS_ROOT = "/var/www/myapp"
RBENV_VERSION = File.read(File.expand_path(File.join(RAILS_ROOT, ".ruby-version"))).chomp
env = {
'RAILS_ROOT' => RAILS_ROOT,
'RBENV_VERSION' => RBENV_VERSION,
}
uid = "www"
gid = "www"
cmd = "/usr/local/rbenv/shims/ruby"
5.times do |num|
God.watch do |w|
w.name = "delayed_job-#{num}"
w.group = 'delayed_job'
w.interval = 60.seconds
w.uid = uid
w.gid = gid
w.dir = RAILS_ROOT
w.env = env
w.start = "#{cmd} "+File.join(RAILS_ROOT, 'script/delayed_job')+" --identifier=#{num} run"
w.start_grace = 10.seconds
w.restart_grace = 10.seconds
w.behavior(:clean_pid_file)
w.keepalive
w.start_if do |start|
start.condition(:process_running) do |c|
c.running = false
end
end
end
end
#!/bin/bash
#
# God
#
# chkconfig: - 99 1
# description: start, stop, restart God (bet you feel powerful)
#
# source function library
. /etc/rc.d/init.d/functions
RBENV_ROOT=/usr/local/rbenv
RBENV_VERSION="2.0.0-p0"
export RBENV_VERSION
PATH=/usr/local/rbenv/shims:/usr/local/rbenv/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
RUBY_PATH=$RBENV_ROOT/shims/ruby
DAEMON=$RBENV_ROOT/shims/god
PIDFILE=/var/run/god.pid
LOGFILE=/var/log/god.log
SCRIPTNAME=/etc/init.d/god
CONFIGFILEDIR=/etc/god
#DEBUG_OPTIONS="--log-level debug"
DEBUG_OPTIONS=""
# Gracefully exit if 'god' gem is not available.
test -x $DAEMON || exit 0
RETVAL=0
god_start() {
start_cmd="$DAEMON -l $LOGFILE -P $PIDFILE $DEBUG_OPTIONS"
#stop_cmd="kill -QUIT `cat $PIDFILE`"
echo $start_cmd
$start_cmd || echo -en "god already running"
RETVAL=$?
if [ "$RETVAL" == '0' ]; then
sleep 2 # wait for server to load before loading config files
if [ -d $CONFIGFILEDIR ]; then
for file in `ls -1 $CONFIGFILEDIR/*.god`; do
echo "god: loading $file ..."
$DAEMON load $file
done
fi
fi
return $RETVAL
}
god_stop() {
stop_cmd="god terminate"
echo $stop_cmd
$stop_cmd || echo -en "god not running"
}
case "$1" in
start)
god_start
RETVAL=$?
;;
stop)
god_stop
RETVAL=$?
;;
restart)
god_stop
god_start
RETVAL=$?
;;
status)
$DAEMON status
RETVAL=$?
;;
cmd)
shift
$DAEMON $*
RETVAL=$?
;;
*)
echo "Usage: god {start|stop|restart|status|cmd <command> <argument>}"
exit 1
;;
esac
exit $RETVAL
# MovableType run-periodic-tasks
# /etc/god/mt-task.god
appname = "mt-task"
mt_home = "/var/www/mt"
env = {
'MT_HOME' => mt_home,
}
uid = "www"
gid = "www"
cmd = "/usr/bin/perl tools/run-periodic-tasks"
God.watch do |w|
w.name = appname
w.interval = 5.seconds
w.env = env
w.dir = mt_home
w.start = "#{cmd} --daemon --sleep 60"
w.uid = uid
w.gid = gid
w.keepalive
w.start_if do |start|
start.condition(:process_running) do |c|
c.running = false
end
end
end
# MovableType 5.2 + Starman
# /etc/god/mt.god
appname = "movabletype"
mt_home = "/var/www/mt"
env = {
'MT_HOME' => mt_home,
}
workers = 10
uid = "www"
gid = "www"
cmd = "/usr/local/bin/starman"
pid = "/var/run/starman/mt.pid"
socket = "/var/run/starman/mt.sock"
log = "/var/log/starman/mt.log"
God.watch do |w|
w.name = appname
w.interval = 5.seconds
w.env = env
w.dir = mt_home
w.start = "#{cmd} --workers #{workers} --listen #{socket} --error-log #{log} --pid #{pid} #{mt_home}/mt.psgi"
w.uid = uid
w.gid = gid
w.keepalive
w.start_if do |start|
start.condition(:process_running) do |c|
c.running = false
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment