Permalink
Browse files

Tweaked cjdns.sh so its variables can be overridden when set in advan…

…ce, so it only kills actual cjdns processes and not other processes that happen to use the same name, and so return values are provided based on whether the process failed or succeeded
  • Loading branch information...
1 parent 76bd81e commit 92fe8acbf9c09e669a11748522f05d884ac31898 @prurigro prurigro committed Nov 11, 2012
Showing with 47 additions and 38 deletions.
  1. +47 −38 scripts/cjdns.sh
View
@@ -1,4 +1,5 @@
-#!/bin/sh
+#!/bin/bash
+#
# You may redistribute this program and/or modify it under the terms of
# the GNU General Public License as published by the Free Software Foundation,
# either version 3 of the License, or (at your option) any later version.
@@ -13,55 +14,63 @@
###
# cjdns.sh
-# copy this file into your cjdns user's hime directory and add
-# */5 * * * * /home/cjdns/cjdns.sh check >>/dev/null 2>>/dev/null
-# to your cron tab for the cjdns user.
#
-# When you type:
-# ./cjdns.sh start
-# cjdns will be started and if it should stop for any reason (including the computer beign halted)
-# the cron job will restart it.
-# To stop it and prevent it from restarting, use:
+# Copy this script to the desired location, then add the following to the cjdns user's crontab
+# */5 * * * * /path/to/cjdns.sh check >>/dev/null 2>>/dev/null
+#
+# Start cjdns if it isn't already running (and set the above cronjob to restart failed processes):
+# ./cjdns.sh start
+#
+# Stop cjdns if it's currently running (and set the above cronjob not to restart failed processes):
# ./cjdns.sh stop
-# After doing an update, use:
+#
+# Restart cjdns after upgrades and changes to the config:
# ./cjdns.sh restart
-# to stop it and bring it back online immedietly.
##
-CJDPATH="${0##*/}"
+# path of cjdns
+if [ -z "$CJDPATH" ]; then CJDPATH="`dirname $0`/"; fi
-CJDROUTE="$CJDPATH/cjdns/cjdroute"
+# path to the cjdroute process
+if [ -z "$CJDROUTE" ]; then CJDROUTE="${CJDPATH}cjdns/cjdroute"; fi
-CONF="$CJDPATH/cjdroute.conf"
+# path of the cjdns process
+if [ -z "$CJDNS" ]; then CJDNS="${CJDPATH}cjdns/cjdns"; fi
-LOGTO="/dev/null"
+# path to the configuration
+if [ -z "$CONF" ]; then CONF="${CJDPATH}cjdroute.conf"; fi
-do_start() {
- "$CJDROUTE" <"$CONF" >>"$LOGPATH" 2>&1 &
-}
+# path ot the log file.
+if [ -z "$LOGTO" ]; then LOGTO="/dev/null"; fi
+
+PID=`pidof -o %PPID $CJDNS`
-do_stop() {
- pkill -x cjdns
+stop()
+{
+ [ ! -z "$PID" ] && kill $PID &> /dev/null
+ if [ $? -gt 0 ]; then return 1; fi
}
-is_running() {
- pgrep -x cjdns >/dev/null
+start()
+{
+ $CJDROUTE < $CONF 2>&1 >> $LOGTO &
+ if [ $? -gt 0 ]; then return 1; fi
}
-case $1 in
-start)
- do_start
- ;;
-stop)
- do_stop
- ;;
-restart)
- do_stop && do_start
- ;;
-status)
- is_running
- ;;
-check)
- is_running || start
- ;;
+case "$1" in
+ "start" )
+ start
+ ;;
+ "restart" )
+ stop
+ start
+ ;;
+ "stop" )
+ stop
+ ;;
+ "check" )
+ ps aux | grep -v 'grep' | grep 'cjdns core' > /dev/null 2>/dev/null || start
+ ;;
+ *)
+ echo "usage: /etc/rc.d/cjdns {start|stop|restart|check}"
esac

0 comments on commit 92fe8ac

Please sign in to comment.