You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. #! /bin/sh
  2. APP_NAME="SonarQube"
  3. # By default, java from the PATH is used, except if SONAR_JAVA_PATH env variable is set
  4. findjava() {
  5. if [ -z "${SONAR_JAVA_PATH}" ]; then
  6. if ! command -v java 2>&1; then
  7. echo "Java not found. Please make sure that the environmental variable SONAR_JAVA_PATH points to a Java executable"
  8. exit 1
  9. fi
  10. JAVA_CMD=java
  11. else
  12. if ! [ -x "${SONAR_JAVA_PATH}" ] || ! [ -f "${SONAR_JAVA_PATH}" ]; then
  13. echo "File '${SONAR_JAVA_PATH}' is not executable. Please make sure that the environmental variable SONAR_JAVA_PATH points to a Java executable"
  14. exit 1
  15. fi
  16. JAVA_CMD="${SONAR_JAVA_PATH}"
  17. fi
  18. }
  19. findjava
  20. # Get the fully qualified path to the script
  21. case $0 in
  22. /*)
  23. SCRIPT="$0"
  24. ;;
  25. *)
  26. PWD=`pwd`
  27. SCRIPT="$PWD/$0"
  28. ;;
  29. esac
  30. # Resolve the true real path without any sym links.
  31. CHANGED=true
  32. while [ "X$CHANGED" != "X" ]
  33. do
  34. # Change spaces to ":" so the tokens can be parsed.
  35. SAFESCRIPT=`echo $SCRIPT | sed -e 's; ;:;g'`
  36. # Get the real path to this script, resolving any symbolic links
  37. TOKENS=`echo $SAFESCRIPT | sed -e 's;/; ;g'`
  38. REALPATH=
  39. for C in $TOKENS; do
  40. # Change any ":" in the token back to a space.
  41. C=`echo $C | sed -e 's;:; ;g'`
  42. REALPATH="$REALPATH/$C"
  43. # If REALPATH is a sym link, resolve it. Loop for nested links.
  44. while [ -h "$REALPATH" ] ; do
  45. LS="`ls -ld "$REALPATH"`"
  46. LINK="`expr "$LS" : '.*-> \(.*\)$'`"
  47. if expr "$LINK" : '/.*' > /dev/null; then
  48. # LINK is absolute.
  49. REALPATH="$LINK"
  50. else
  51. # LINK is relative.
  52. REALPATH="`dirname "$REALPATH"`""/$LINK"
  53. fi
  54. done
  55. done
  56. if [ "$REALPATH" = "$SCRIPT" ]
  57. then
  58. CHANGED=""
  59. else
  60. SCRIPT="$REALPATH"
  61. fi
  62. done
  63. # Change the current directory to the location of the script
  64. cd "`dirname "$REALPATH"`"
  65. LIB_DIR="../../lib"
  66. HAZELCAST_ADDITIONAL="--add-exports=java.base/jdk.internal.ref=ALL-UNNAMED \
  67. --add-opens=java.base/java.lang=ALL-UNNAMED \
  68. --add-opens=java.base/java.nio=ALL-UNNAMED \
  69. --add-opens=java.base/sun.nio.ch=ALL-UNNAMED \
  70. --add-opens=java.management/sun.management=ALL-UNNAMED \
  71. --add-opens=jdk.management/com.sun.management.internal=ALL-UNNAMED"
  72. # Sonar app launching process memory setting
  73. XMS="-Xms8m"
  74. XMX="-Xmx32m"
  75. COMMAND_LINE="$JAVA_CMD $XMS $XMX $HAZELCAST_ADDITIONAL -jar $LIB_DIR/sonar-application-@sqversion@.jar"
  76. # Location of the pid file.
  77. PIDFILE="./$APP_NAME.pid"
  78. # Resolve the location of the 'ps' command
  79. PSEXE="/usr/bin/ps"
  80. if [ ! -x "$PSEXE" ]
  81. then
  82. PSEXE="/bin/ps"
  83. if [ ! -x "$PSEXE" ]
  84. then
  85. echo "Unable to locate 'ps'."
  86. echo "Please report this message along with the location of the command on your system."
  87. exit 1
  88. fi
  89. fi
  90. getpid() {
  91. if [ -f "$PIDFILE" ]
  92. then
  93. if [ -r "$PIDFILE" ]
  94. then
  95. pid=`cat "$PIDFILE"`
  96. if [ "X$pid" != "X" ]
  97. then
  98. # It is possible that 'a' process with the pid exists but that it is not the
  99. # correct process. This can happen in a number of cases, but the most
  100. # common is during system startup after an unclean shutdown.
  101. # The ps statement below looks for the specific wrapper command running as
  102. # the pid. If it is not found then the pid file is considered to be stale.
  103. pidtest=`$PSEXE -p $pid | grep "sonar-application-@sqversion@.jar" | tail -1`
  104. if [ "X$pidtest" = "X" ]
  105. then
  106. # This is a stale pid file.
  107. rm -f "$PIDFILE"
  108. echo "Removed stale pid file: $PIDFILE"
  109. pid=""
  110. fi
  111. fi
  112. else
  113. echo "Cannot read $PIDFILE."
  114. exit 1
  115. fi
  116. fi
  117. }
  118. testpid() {
  119. pid=`$PSEXE -p $pid | grep $pid | grep -v grep | awk '{print $1}' | tail -1`
  120. if [ "X$pid" = "X" ]
  121. then
  122. # Process is gone so remove the pid file.
  123. rm -f "$PIDFILE"
  124. pid=""
  125. fi
  126. }
  127. console() {
  128. echo "Running $APP_NAME..."
  129. getpid
  130. if [ "X$pid" = "X" ]
  131. then
  132. echo $$ > $PIDFILE
  133. exec $COMMAND_LINE -Dsonar.log.console=true
  134. else
  135. echo "$APP_NAME is already running."
  136. exit 1
  137. fi
  138. }
  139. start() {
  140. echo "Starting $APP_NAME..."
  141. getpid
  142. if [ "X$pid" = "X" ]
  143. then
  144. exec nohup $COMMAND_LINE >../../logs/nohup.log 2>&1 &
  145. echo $! > $PIDFILE
  146. else
  147. echo "$APP_NAME is already running."
  148. exit 1
  149. fi
  150. getpid
  151. if [ "X$pid" != "X" ]
  152. then
  153. echo "Started $APP_NAME."
  154. else
  155. echo "Failed to start $APP_NAME."
  156. fi
  157. }
  158. waitforstop() {
  159. savepid=$pid
  160. CNT=0
  161. TOTCNT=0
  162. while [ "X$pid" != "X" ]
  163. do
  164. # Show a waiting message every 5 seconds.
  165. if [ "$CNT" -lt "5" ]
  166. then
  167. CNT=`expr $CNT + 1`
  168. else
  169. echo "Waiting for $APP_NAME to exit..."
  170. CNT=0
  171. fi
  172. TOTCNT=`expr $TOTCNT + 1`
  173. sleep 1
  174. testpid
  175. done
  176. pid=$savepid
  177. testpid
  178. if [ "X$pid" != "X" ]
  179. then
  180. echo "Failed to stop $APP_NAME."
  181. exit 1
  182. else
  183. echo "Stopped $APP_NAME."
  184. fi
  185. }
  186. stopit() {
  187. echo "Gracefully stopping $APP_NAME..."
  188. getpid
  189. if [ "X$pid" = "X" ]
  190. then
  191. echo "$APP_NAME was not running."
  192. else
  193. kill $pid
  194. if [ $? -ne 0 ]
  195. then
  196. # An explanation for the failure should have been given
  197. echo "Unable to stop $APP_NAME."
  198. exit 1
  199. fi
  200. waitforstop
  201. fi
  202. }
  203. forcestopit() {
  204. getpid
  205. if [ "X$pid" = "X" ]
  206. then
  207. echo "$APP_NAME not running"
  208. exit 1
  209. fi
  210. testpid
  211. if [ "X$pid" != "X" ]
  212. then
  213. # start shutdowner from SQ installation directory
  214. cd "../.."
  215. echo "Force stopping $APP_NAME..."
  216. ${JAVA_CMD} -jar "lib/sonar-shutdowner-@sqversion@.jar"
  217. waitforstop
  218. fi
  219. }
  220. status() {
  221. getpid
  222. if [ "X$pid" = "X" ]
  223. then
  224. echo "$APP_NAME is not running."
  225. exit 1
  226. else
  227. echo "$APP_NAME is running ($pid)."
  228. exit 0
  229. fi
  230. }
  231. dump() {
  232. echo "Dumping $APP_NAME..."
  233. getpid
  234. if [ "X$pid" = "X" ]
  235. then
  236. echo "$APP_NAME was not running."
  237. else
  238. kill -3 $pid
  239. if [ $? -ne 0 ]
  240. then
  241. echo "Failed to dump $APP_NAME."
  242. exit 1
  243. else
  244. echo "Dumped $APP_NAME."
  245. fi
  246. fi
  247. }
  248. case "$1" in
  249. 'console')
  250. console
  251. ;;
  252. 'start')
  253. start
  254. ;;
  255. 'stop')
  256. stopit
  257. ;;
  258. 'force-stop')
  259. forcestopit
  260. ;;
  261. 'restart')
  262. stopit
  263. start
  264. ;;
  265. 'status')
  266. status
  267. ;;
  268. 'dump')
  269. dump
  270. ;;
  271. *)
  272. echo "Usage: $0 { console | start | stop | force-stop | restart | status | dump }"
  273. exit 1
  274. ;;
  275. esac
  276. exit 0