aboutsummaryrefslogtreecommitdiffstats
path: root/start_unicorn.sh
blob: 5266bdbad35074f414603d76f4a33853d3aafe6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/bin/sh

export RAILS_ENV=production
export RAILS_ROOT=$(readlink -f `dirname $0`)

PID="$RAILS_ROOT/tmp/pids/unicorn.pid"
PID_OLD="$PID.oldbin"

sig () {
    test -s $PID && kill -$1 `cat $PID`
}

sig_old () {
    test -s $PID_OLD && kill -$1 `cat $PID_OLD`
}

error () {
    echo $1
    exit 1
}

unicorn_start="bundle exec unicorn_rails -D -E $RAILS_ENV -c $RAILS_ROOT/config/unicorn.rb -l 4000"

case "$1" in
    start)
        sig 0 && error "Unicorn already started!"
        $unicorn_start && echo "Unicorn started!" || error "Unicorn failed to start!"
        ;;
    stop)
        sig QUIT && echo "Unicorn stopped" || error "Unicorn not started!"
        ;;
    restart)
        sig 0 || error "Unicorn not started!"
        sig USR2 && sleep 5
        sig_old QUIT && echo "Quit old unicorn" `cat $PID_OLD` || echo "Couldn't quit old unicorn"
        ;;
    status)
        sig 0 && echo "Unicorn started" || echo "Unicorn stopped"
        ;;
    *)
        error "usage: $0 {start|stop|restart|status}"
esac