jlanserver.sh 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #! /bin/bash
  2. #
  3. # JLAN Server Startup Script
  4. #
  5. #-----------------------------------------------------------------------------
  6. # These settings can be modified to fit the needs of your application
  7. # Application
  8. APP_NAME="JLANServer"
  9. APP_LONG_NAME="JLANServer Virtual Filesystem Server"
  10. # Wrapper
  11. WRAPPER_CMD="./wrapper"
  12. WRAPPER_CONF="./jlansrv.conf"
  13. # Priority (see the start() method if you want to use this)
  14. PRIORITY=
  15. # Do not modify anything beyond this point
  16. #-----------------------------------------------------------------------------
  17. # Get the fully qualified path to the script
  18. case $0 in
  19. /*)
  20. SCRIPT="$0"
  21. ;;
  22. *)
  23. PWD=`pwd`
  24. SCRIPT="$PWD/$0"
  25. ;;
  26. esac
  27. # Change spaces to ":" so the tokens can be parsed.
  28. SCRIPT=`echo $SCRIPT | sed -e 's; ;:;g'`
  29. # Get the real path to this script, resolving any symbolic links
  30. TOKENS=`echo $SCRIPT | sed -e 's;/; ;g'`
  31. REALPATH=
  32. for C in $TOKENS; do
  33. REALPATH="$REALPATH/$C"
  34. while [ -h "$REALPATH" ] ; do
  35. LS="`ls -ld "$REALPATH"`"
  36. LINK="`expr "$LS" : '.*-> \(.*\)$'`"
  37. if expr "$LINK" : '/.*' > /dev/null; then
  38. REALPATH="$LINK"
  39. else
  40. REALPATH="`dirname "$REALPATH"`""/$LINK"
  41. fi
  42. done
  43. done
  44. # Change ":" chars back to spaces.
  45. REALPATH=`echo $REALPATH | sed -e 's;:; ;g'`
  46. # Change the current directory to the location of the script
  47. cd "`dirname "$REALPATH"`"
  48. # Find pidof.
  49. PIDOF="/bin/pidof"
  50. if [ ! -x $PIDOF ]
  51. then
  52. PIDOF="/sbin/pidof"
  53. if [ ! -x $PIDOF ]
  54. then
  55. echo "Cannot find 'pidof' in /bin or /sbin."
  56. echo "This script requires 'pidof' to run."
  57. exit 1
  58. fi
  59. fi
  60. console() {
  61. echo "Running $APP_LONG_NAME..."
  62. pid=`$PIDOF $APP_NAME`
  63. if [ -z $pid ]
  64. then
  65. # If you wanted to specify the priority with which
  66. # your app runs, you could use nice here:
  67. # exec -a $APP_NAME nice -$PRIORITY $WRAPPER_CMD $WRAPPER_CONF
  68. # See "man nice" for more details.
  69. exec -a $APP_NAME $WRAPPER_CMD $WRAPPER_CONF
  70. else
  71. echo "$APP_LONG_NAME is already running."
  72. exit 1
  73. fi
  74. }
  75. start() {
  76. echo "Starting $APP_LONG_NAME..."
  77. pid=`$PIDOF $APP_NAME`
  78. if [ -z $pid ]
  79. then
  80. # If you wanted to specify the priority with which
  81. # your app runs, you could use nice here:
  82. # exec -a $APP_NAME nice -$PRIORITY $WRAPPER_CMD $WRAPPER_CONF wrapper.daemonize=TRUE wrapper.console.loglevel=NONE
  83. # See "man nice" for more details.
  84. exec -a $APP_NAME $WRAPPER_CMD $WRAPPER_CONF wrapper.daemonize=TRUE wrapper.console.loglevel=NONE
  85. else
  86. echo "$APP_LONG_NAME is already running."
  87. exit 1
  88. fi
  89. }
  90. stopit() {
  91. echo "Stopping $APP_LONG_NAME..."
  92. pid=`$PIDOF $APP_NAME`
  93. if [ -z $pid ]
  94. then
  95. echo "$APP_LONG_NAME was not running."
  96. else
  97. # Running so try to stop it.
  98. kill $pid
  99. if [ $? -ne 0 ]
  100. then
  101. # An explanation for the failure should have been given
  102. echo "Unable to stop $APP_LONG_NAME."
  103. exit 1
  104. fi
  105. # We can not predict how long it will take for the wrapper to
  106. # actually stop as it depends on settings in wrapper.conf.
  107. # Loop until it does.
  108. CNT=0
  109. TOTCNT=0
  110. while [ ! -z $pid ]
  111. do
  112. # Loop for up to 5 minutes
  113. if [ "$TOTCNT" -lt "300" ]
  114. then
  115. if [ "$CNT" -lt "5" ]
  116. then
  117. CNT=`expr $CNT + 1`
  118. else
  119. echo "Waiting for $APP_LONG_NAME to exit..."
  120. CNT=0
  121. fi
  122. TOTCNT=`expr $TOTCNT + 1`
  123. sleep 1
  124. pid=`$PIDOF $APP_NAME`
  125. else
  126. pid=
  127. fi
  128. done
  129. pid=`$PIDOF $APP_NAME`
  130. if [ ! -z $pid ]
  131. then
  132. echo "Timed out waiting for $APP_LONG_NAME to exit."
  133. echo " Attempting a forced exit..."
  134. kill -9 $pid
  135. fi
  136. pid=`$PIDOF $APP_NAME`
  137. if [ ! -z $pid ]
  138. then
  139. echo "Failed to stop $APP_LONG_NAME."
  140. exit 1
  141. else
  142. echo "Stopped $APP_LONG_NAME."
  143. fi
  144. fi
  145. }
  146. dump() {
  147. echo "Dumping $APP_LONG_NAME..."
  148. pid=`$PIDOF $APP_NAME`
  149. if [ -z $pid ]
  150. then
  151. echo "$APP_LONG_NAME was not running."
  152. else
  153. kill -3 $pid
  154. if [ $? -ne 0 ]
  155. then
  156. echo "Failed to dump $APP_LONG_NAME."
  157. else
  158. echo "Dumped $APP_LONG_NAME."
  159. fi
  160. fi
  161. }
  162. case "$1" in
  163. 'console')
  164. console
  165. ;;
  166. 'start')
  167. start
  168. ;;
  169. 'stop')
  170. stopit
  171. ;;
  172. 'restart')
  173. stopit
  174. start
  175. ;;
  176. 'dump')
  177. dump
  178. ;;
  179. *)
  180. echo "Usage: $0 { console | start | stop | restart | dump }"
  181. exit 1
  182. ;;
  183. esac
  184. exit 0