stream_input: small cleanups and fixes
[unicorn.git] / examples / init.sh
blob1f0e0354c685a853294bd58ce57ebdeab2ac9c5f
1 #!/bin/sh
2 set -e
3 # Example init script, this can be used with nginx, too,
4 # since nginx and unicorn accept the same signals
6 # Feel free to change any of the following variables for your app:
7 TIMEOUT=${TIMEOUT-60}
8 APP_ROOT=/home/x/my_app/current
9 PID=$APP_ROOT/tmp/pids/unicorn.pid
10 CMD="/usr/bin/unicorn -D -c $APP_ROOT/config/unicorn.rb"
11 INIT_CONF=$APP_ROOT/config/init.conf
12 action="$1"
13 set -u
15 test -f "$INIT_CONF" && . $INIT_CONF
17 old_pid="$PID.oldbin"
19 cd $APP_ROOT || exit 1
21 sig () {
22 test -s "$PID" && kill -$1 `cat $PID`
25 oldsig () {
26 test -s $old_pid && kill -$1 `cat $old_pid`
29 case $action in
30 start)
31 sig 0 && echo >&2 "Already running" && exit 0
32 $CMD
34 stop)
35 sig QUIT && exit 0
36 echo >&2 "Not running"
38 force-stop)
39 sig TERM && exit 0
40 echo >&2 "Not running"
42 restart|reload)
43 sig HUP && echo reloaded OK && exit 0
44 echo >&2 "Couldn't reload, starting '$CMD' instead"
45 $CMD
47 upgrade)
48 if sig USR2 && sleep 2 && sig 0 && oldsig QUIT
49 then
50 n=$TIMEOUT
51 while test -s $old_pid && test $n -ge 0
53 printf '.' && sleep 1 && n=$(( $n - 1 ))
54 done
55 echo
57 if test $n -lt 0 && test -s $old_pid
58 then
59 echo >&2 "$old_pid still exists after $TIMEOUT seconds"
60 exit 1
62 exit 0
64 echo >&2 "Couldn't upgrade, starting '$CMD' instead"
65 $CMD
67 reopen-logs)
68 sig USR1
71 echo >&2 "Usage: $0 <start|stop|restart|upgrade|force-stop|reopen-logs>"
72 exit 1
74 esac