summaryrefslogtreecommitdiff
path: root/.app/lib/pid-method
diff options
context:
space:
mode:
Diffstat (limited to '.app/lib/pid-method')
-rwxr-xr-x.app/lib/pid-method102
1 files changed, 88 insertions, 14 deletions
diff --git a/.app/lib/pid-method b/.app/lib/pid-method
index b4a672e..15e85ff 100755
--- a/.app/lib/pid-method
+++ b/.app/lib/pid-method
@@ -1,8 +1,8 @@
-#!/bin/bash
+#!/bin/bash -e
-set -e
+set -u
-PID_FILE=$APPSH_BASEDIR/.app/var/pid/$name-$instance.pid
+PID_FILE=$APPSH_BASEDIR/.app/var/pid/$APPSH_NAME-$APPSH_INSTANCE.pid
. $APPSH_HOME/.app/lib/app-conf
@@ -22,16 +22,90 @@ 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)
- set -x
- $bin &
- ret=$?
- pid=$!
- echo "Application launched with PID=$pid"
- echo $pid > $PID_FILE
- ;;
- *)
- exit 1
- ;;
+ start) method_start ;;
+ stop) method_stop ;;
+ status) method_status ;;
+ *) exit 1 ;;
esac
+
+exit $?