Created
November 7, 2013 02:15
-
-
Save dvliman/7347827 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
PIDFILE=/var/run/os-<%= @name %>.pid | |
# Start the service | |
start() { | |
if [ -f $PIDFILE ]; then | |
echo "<%= @name %> already running, process `cat $PIDFILE`." | |
exit 1 | |
fi | |
echo "Starting os-<%= @name %>..." | |
cd <%= @config[:app_home] %> && bundle exec ./<%= @name %>.sh & | |
while [ ! -f $PIDFILE ]; do | |
sleep 1 | |
done | |
echo "os-<%= @name %> start/running, process `cat $PIDFILE`." | |
} | |
# Stop the service | |
stop() { | |
if [ ! -f $PIDFILE ]; then | |
echo "os-<%= @name %>: not running. Unable to stop." | |
exit 1 | |
fi | |
PID=`cat $PIDFILE` | |
echo "Stopping os-<%= @name %>, process $PID.." | |
sudo kill $PID | |
while ps -p $PID > /dev/null | |
do | |
sleep 1 | |
done | |
echo "Stopped os-<%= @name %>" | |
} | |
# To prevent cold start problem | |
reload() { | |
if [ -f $PIDFILE ]; then | |
OLDPID=`cat $PIDFILE` | |
echo "Reloading os-<%= @name %>, process $OLDPID..." | |
sudo kill -s USR2 $OLDPID | |
while ps -p $OLDPID > /dev/null | |
do | |
sleep 1 | |
done | |
echo "os-<%= @name %> start/running, process `cat $PIDFILE`." | |
else | |
echo "os-<%= @name %> not running. Starting instead." | |
start | |
fi | |
} | |
# Display the status of the service | |
status() { | |
if [ ! -f $PIDFILE ]; then | |
echo "os-<%= @name %>: not running." | |
else | |
echo "os-<%= @name %>: running, process `cat $PIDFILE`." | |
fi | |
} | |
### main logic ### | |
case "$1" in | |
start) | |
start | |
;; | |
stop) | |
stop | |
;; | |
status) | |
status | |
;; | |
restart) | |
stop | |
start | |
;; | |
reload) | |
reload | |
;; | |
*) | |
echo $"Usage: $0 {start|stop|restart|reload|status}" | |
exit 1 | |
esac | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment