production
1.29 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/bin/sh
set -e
TIMEOUT=60
export RAILS_ENV=production
ACTION="$1"
if [ -z "$ACTION" ]; then
echo "usage: $0 start|stop|restart"
exit 1
fi
app_server_start() {
ruby -S bundle exec unicorn_rails \
--config-file config/unicorn.rb \
--env "$RAILS_ENV" \
--daemonize
}
app_server_stop() {
kill -s QUIT $(cat tmp/pids/unicorn.pid)
}
app_server_restart() {
kill -s USR2 $(cat tmp/pids/unicorn.pid)
sleep 5
kill -s QUIT $(cat tmp/pids/unicorn.pid.oldbin)
}
do_start() {
app_server_start
}
do_stop() {
app_server_stop || stop_via_pid_file tmp/pids/unicorn.pid
}
do_restart() {
app_server_restart
}
stop_via_pid_file() {
for pidfile in $@; do
if [ -e "$pidfile" ]; then
pid=$(cat $pidfile)
echo "Sentign TERM signal to stop $pid ..."
kill -TERM "$pid"
rm -f $pidfile
fi
done
}
do_running() {
pids=$(sed "s/.*/& /" tmp/pids/unicorn.pid 2>/dev/null | tr -d '\n')
kill -0 $pids > /dev/null 2>&1
}
case "$ACTION" in
start|stop)
do_$ACTION
;;
run)
do_start
echo "=> Running in production mode. Hit ctrl-C to stop."
trap do_stop INT TERM
tail -n 0 -f log/production.log || true
;;
restart)
do_restart
;;
running)
do_running
;;
*)
echo "usage: $0 start|stop|restart"
exit 1
;;
esac