aboutsummaryrefslogtreecommitdiff
path: root/bin/pid-method
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2013-01-26 23:58:22 +0100
committerTrygve Laugstøl <trygvis@inamo.no>2013-01-26 23:58:22 +0100
commit110ffae47db27a49bbc43f86ba3737bccc1b3085 (patch)
treed74934b12e2904b8aff5fe87421a6501b8ea5c8e /bin/pid-method
parentca27d6f6d9ccc35bf55db3d360d1d464d5e206e7 (diff)
downloadapp.sh-110ffae47db27a49bbc43f86ba3737bccc1b3085.tar.gz
app.sh-110ffae47db27a49bbc43f86ba3737bccc1b3085.tar.bz2
app.sh-110ffae47db27a49bbc43f86ba3737bccc1b3085.tar.xz
app.sh-110ffae47db27a49bbc43f86ba3737bccc1b3085.zip
o Rewriting most of this stuff to make it feel more like git.
Diffstat (limited to 'bin/pid-method')
-rwxr-xr-xbin/pid-method113
1 files changed, 113 insertions, 0 deletions
diff --git a/bin/pid-method b/bin/pid-method
new file mode 100755
index 0000000..29f6b4f
--- /dev/null
+++ b/bin/pid-method
@@ -0,0 +1,113 @@
+#!/bin/bash -e
+
+set -u
+
+. $APPSH_HOME/.app/lib/app-conf
+
+pid_file=$APPSH_APPS/.app/var/pid/$APPSH_NAME-$APPSH_INSTANCE.pid
+bin=`get_conf $APPSH_APPS $APPSH_NAME $APPSH_INSTANCE app.bin`
+
+cd $APPSH_APPS/$APPSH_NAME/$APPSH_INSTANCE/current
+
+if [ -z "$bin" ]
+then
+ echo "Missing required configuration: app.bin." >&2
+ exit 1
+fi
+
+if [ ! -r "$bin" ]
+then
+ echo "No such file: $bin" >&2
+ exit 1
+fi
+
+chmod +x "$bin"
+
+PID=
+if [ -r $pid_file ]
+then
+ PID="`cat $pid_file`"
+fi
+
+do_status() {
+ if [ -z "$PID" ]
+ then
+ echo stopped
+ else
+ if [ `ps -p "$PID" 2>/dev/null | wc -l` -gt 1 ]
+ then
+ echo running
+ else
+ echo crashed
+ fi
+ fi
+}
+
+method_start() {
+ case `do_status` in
+ running)
+ echo "The application is already running as $PID."
+ exit 1
+ ;;
+ esac
+
+ $bin <&- 1<&- 2<&- &
+
+ PID=$!
+ echo "Application launched as $PID"
+ echo $PID > $pid_file
+
+ return 0
+}
+
+method_stop() {
+ case `do_status` in
+ stopped)
+ echo "The application not running."
+ exit 1
+ ;;
+ crashed)
+ echo "The application crashed. Was running as $PID"
+ # TODO: should this remove the PID file?
+ # That makes it possible to run "stop" to stop "status" from showing "crashed"
+ exit 1
+ ;;
+ esac
+
+ signal="-9"
+ echo -n "Sending kill $signal to $PID, waiting for shutdown"
+ kill $signal $PID
+
+ while [ "`do_status`" == "running" ]
+ do
+ sleep 1
+ echo -n "."
+ done
+
+ echo " OK"
+ rm -f $pid_file
+ return 0
+}
+
+method_status() {
+ case `do_status` in
+ running)
+ echo "$APPSH_NAME/$APPSH_INSTANCE is running as $PID"
+ ;;
+ stopped)
+ echo "$APPSH_NAME/$APPSH_INSTANCE is not running"
+ ;;
+ crashed)
+ echo "$APPSH_NAME/$APPSH_INSTANCE crashed. Was running as $PID"
+ ;;
+ esac
+}
+
+case "$APPSH_METHOD" in
+ start) method_start ;;
+ stop) method_stop ;;
+ status) method_status ;;
+ *) exit 1 ;;
+esac
+
+exit $?