t0011-active-unix-socket.sh: fix race condition in test
[unicorn.git] / examples / init.sh
blob4ef6cdc07ff123cc7abc51d545f8837efbc2dbe5
1 #!/bin/sh
2 set -e
3 ### BEGIN INIT INFO
4 # Provides: unicorn
5 # Required-Start: $local_fs $network
6 # Required-Stop: $local_fs $network
7 # Default-Start: 2 3 4 5
8 # Default-Stop: 0 1 6
9 # Short-Description: Start/stop unicorn Rack app server
10 ### END INIT INFO
12 # Example init script, this can be used with nginx, too,
13 # since nginx and unicorn accept the same signals.
15 # Feel free to change any of the following variables for your app:
16 TIMEOUT=${TIMEOUT-60}
17 APP_ROOT=/home/x/my_app/current
18 PID=$APP_ROOT/tmp/pids/unicorn.pid
19 CMD="/usr/bin/unicorn -D -c $APP_ROOT/config/unicorn.rb"
20 INIT_CONF=$APP_ROOT/config/init.conf
21 UPGRADE_DELAY=${UPGRADE_DELAY-2}
22 action="$1"
23 set -u
25 test -f "$INIT_CONF" && . $INIT_CONF
27 OLD="$PID.oldbin"
29 cd $APP_ROOT || exit 1
31 sig () {
32 test -s "$PID" && kill -$1 $(cat $PID)
35 oldsig () {
36 test -s "$OLD" && kill -$1 $(cat $OLD)
39 case $action in
40 start)
41 sig 0 && echo >&2 "Already running" && exit 0
42 $CMD
44 stop)
45 sig QUIT && exit 0
46 echo >&2 "Not running"
48 force-stop)
49 sig TERM && exit 0
50 echo >&2 "Not running"
52 restart|reload)
53 sig HUP && echo reloaded OK && exit 0
54 echo >&2 "Couldn't reload, starting '$CMD' instead"
55 $CMD
57 upgrade)
58 if oldsig 0
59 then
60 echo >&2 "Old upgraded process still running with $OLD"
61 exit 1
64 cur_pid=
65 if test -s "$PID"
66 then
67 cur_pid=$(cat $PID)
70 if test -n "$cur_pid" &&
71 kill -USR2 "$cur_pid" &&
72 sleep $UPGRADE_DELAY &&
73 new_pid=$(cat $PID) &&
74 test x"$new_pid" != x"$cur_pid" &&
75 kill -0 "$new_pid" &&
76 kill -QUIT "$cur_pid"
77 then
78 n=$TIMEOUT
79 while kill -0 "$cur_pid" 2>/dev/null && test $n -ge 0
81 printf '.' && sleep 1 && n=$(( $n - 1 ))
82 done
83 echo
85 if test $n -lt 0 && kill -0 "$cur_pid" 2>/dev/null
86 then
87 echo >&2 "$cur_pid still running after $TIMEOUT seconds"
88 exit 1
90 exit 0
92 echo >&2 "Couldn't upgrade, starting '$CMD' instead"
93 $CMD
95 reopen-logs)
96 sig USR1
99 echo >&2 "Usage: $0 <start|stop|restart|upgrade|force-stop|reopen-logs>"
100 exit 1
102 esac