rc.sonarqube 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. #! /bin/sh
  2. #
  3. # Copyright (c) 1999, 2006 Tanuki Software Inc.
  4. #
  5. # Java Service Wrapper sh script. Suitable for starting and stopping
  6. # wrapped Java applications on UNIX platforms.
  7. #
  8. #-----------------------------------------------------------------------------
  9. # These settings can be modified to fit the needs of your application
  10. # Default values for the Application variables, below.
  11. #
  12. # NOTE: The build for specific applications may override this during the resource-copying
  13. # phase, to fill in a concrete name and avoid the use of the defaults specified here.
  14. DEF_APP_NAME="SonarQube"
  15. DEF_APP_LONG_NAME="SonarQube"
  16. # Application
  17. APP_NAME="${DEF_APP_NAME}"
  18. APP_LONG_NAME="${DEF_APP_LONG_NAME}"
  19. # Wrapper
  20. WRAPPER_CMD="./wrapper"
  21. WRAPPER_CONF="/etc/sonarqube/wrapper.conf"
  22. # Priority at which to run the wrapper. See "man nice" for valid priorities.
  23. # nice is only used if a priority is specified.
  24. PRIORITY=
  25. # Location of the pid file.
  26. PIDDIR="/var/run/sonarqube"
  27. # If uncommented, causes the Wrapper to be shutdown using an anchor file.
  28. # When launched with the 'start' command, it will also ignore all INT and
  29. # TERM signals.
  30. #IGNORE_SIGNALS=true
  31. # If specified, the Wrapper will be run as the specified user.
  32. # IMPORTANT - Make sure that the user has the required privileges to write
  33. # the PID file and wrapper.log files. Failure to be able to write the log
  34. # file will cause the Wrapper to exit without any way to write out an error
  35. # message.
  36. # NOTE - This will set the user which is used to run the Wrapper as well as
  37. # the JVM and is not useful in situations where a privileged resource or
  38. # port needs to be allocated prior to the user being changed.
  39. RUN_AS_USER=sonar
  40. # The following two lines are used by the chkconfig command. Change as is
  41. # appropriate for your application. They should remain commented.
  42. # chkconfig: 2345 20 80
  43. # description: Test Wrapper Sample Application
  44. # Do not modify anything beyond this point
  45. #-----------------------------------------------------------------------------
  46. # Get the fully qualified path to the script
  47. case $0 in
  48. /*)
  49. SCRIPT="$0"
  50. ;;
  51. *)
  52. PWD=`pwd`
  53. SCRIPT="$PWD/$0"
  54. ;;
  55. esac
  56. # Resolve the true real path without any sym links.
  57. CHANGED=true
  58. while [ "X$CHANGED" != "X" ]
  59. do
  60. # Change spaces to ":" so the tokens can be parsed.
  61. SAFESCRIPT=`echo $SCRIPT | sed -e 's; ;:;g'`
  62. # Get the real path to this script, resolving any symbolic links
  63. TOKENS=`echo $SAFESCRIPT | sed -e 's;/; ;g'`
  64. REALPATH=
  65. for C in $TOKENS; do
  66. # Change any ":" in the token back to a space.
  67. C=`echo $C | sed -e 's;:; ;g'`
  68. REALPATH="$REALPATH/$C"
  69. # If REALPATH is a sym link, resolve it. Loop for nested links.
  70. while [ -h "$REALPATH" ] ; do
  71. LS="`ls -ld "$REALPATH"`"
  72. LINK="`expr "$LS" : '.*-> \(.*\)$'`"
  73. if expr "$LINK" : '/.*' > /dev/null; then
  74. # LINK is absolute.
  75. REALPATH="$LINK"
  76. else
  77. # LINK is relative.
  78. REALPATH="`dirname "$REALPATH"`""/$LINK"
  79. fi
  80. done
  81. done
  82. if [ "$REALPATH" = "$SCRIPT" ]
  83. then
  84. CHANGED=""
  85. else
  86. SCRIPT="$REALPATH"
  87. fi
  88. done
  89. # Change the current directory to the location of the script
  90. cd "`dirname "$REALPATH"`"
  91. REALDIR=`pwd`
  92. # If the PIDDIR is relative, set its value relative to the full REALPATH to avoid problems if
  93. # the working directory is later changed.
  94. FIRST_CHAR=`echo $PIDDIR | cut -c1,1`
  95. if [ "$FIRST_CHAR" != "/" ]
  96. then
  97. PIDDIR=$REALDIR/$PIDDIR
  98. fi
  99. # Same test for WRAPPER_CMD
  100. FIRST_CHAR=`echo $WRAPPER_CMD | cut -c1,1`
  101. if [ "$FIRST_CHAR" != "/" ]
  102. then
  103. WRAPPER_CMD=$REALDIR/$WRAPPER_CMD
  104. fi
  105. # Same test for WRAPPER_CONF
  106. FIRST_CHAR=`echo $WRAPPER_CONF | cut -c1,1`
  107. if [ "$FIRST_CHAR" != "/" ]
  108. then
  109. WRAPPER_CONF=$REALDIR/$WRAPPER_CONF
  110. fi
  111. # Process ID
  112. ANCHORFILE="$PIDDIR/$APP_NAME.anchor"
  113. PIDFILE="$PIDDIR/$APP_NAME.pid"
  114. LOCKDIR="/var/lock/subsys"
  115. LOCKFILE="$LOCKDIR/$APP_NAME"
  116. pid=""
  117. # Resolve the location of the 'ps' command
  118. PSEXE="/usr/bin/ps"
  119. if [ ! -x "$PSEXE" ]
  120. then
  121. PSEXE="/bin/ps"
  122. if [ ! -x "$PSEXE" ]
  123. then
  124. echo "Unable to locate 'ps'."
  125. echo "Please report this message along with the location of the command on your system."
  126. exit 1
  127. fi
  128. fi
  129. # Resolve the os
  130. DIST_OS=`uname -s | tr [:upper:] [:lower:] | tr -d [:blank:]`
  131. case "$DIST_OS" in
  132. 'sunos')
  133. DIST_OS="solaris"
  134. ;;
  135. 'hp-ux' | 'hp-ux64')
  136. DIST_OS="hpux"
  137. ;;
  138. 'darwin')
  139. DIST_OS="macosx"
  140. ;;
  141. 'unix_sv')
  142. DIST_OS="unixware"
  143. ;;
  144. esac
  145. # Resolve the architecture
  146. DIST_ARCH=`uname -p | tr [:upper:] [:lower:] | tr -d [:blank:]`
  147. if [ "$DIST_ARCH" = "unknown" ]
  148. then
  149. DIST_ARCH=`uname -m | tr [:upper:] [:lower:] | tr -d [:blank:]`
  150. fi
  151. case "$DIST_ARCH" in
  152. 'amd64' | 'athlon' | 'ia32' | 'ia64' | 'i386' | 'i486' | 'i586' | 'i686' | 'x86_64')
  153. DIST_ARCH="x86"
  154. ;;
  155. 'ip27')
  156. DIST_ARCH="mips"
  157. ;;
  158. 'power' | 'powerpc' | 'power_pc' | 'ppc64')
  159. DIST_ARCH="ppc"
  160. ;;
  161. 'pa_risc' | 'pa-risc')
  162. DIST_ARCH="parisc"
  163. ;;
  164. 'sun4u' | 'sparcv9')
  165. DIST_ARCH="sparc"
  166. ;;
  167. '9000/800')
  168. DIST_ARCH="parisc"
  169. ;;
  170. esac
  171. outputFile() {
  172. if [ -f "$1" ]
  173. then
  174. echo " $1 (Found but not executable.)";
  175. else
  176. echo " $1"
  177. fi
  178. }
  179. # Decide on the wrapper binary to use.
  180. # If a 32-bit wrapper binary exists then it will work on 32 or 64 bit
  181. # platforms, if the 64-bit binary exists then the distribution most
  182. # likely wants to use long names. Otherwise, look for the default.
  183. # For macosx, we also want to look for universal binaries.
  184. WRAPPER_TEST_CMD="$WRAPPER_CMD-$DIST_OS-$DIST_ARCH-32"
  185. if [ -x "$WRAPPER_TEST_CMD" ]
  186. then
  187. WRAPPER_CMD="$WRAPPER_TEST_CMD"
  188. else
  189. if [ "$DIST_OS" = "macosx" ]
  190. then
  191. WRAPPER_TEST_CMD="$WRAPPER_CMD-$DIST_OS-universal-32"
  192. if [ -x "$WRAPPER_TEST_CMD" ]
  193. then
  194. WRAPPER_CMD="$WRAPPER_TEST_CMD"
  195. else
  196. WRAPPER_TEST_CMD="$WRAPPER_CMD-$DIST_OS-$DIST_ARCH-64"
  197. if [ -x "$WRAPPER_TEST_CMD" ]
  198. then
  199. WRAPPER_CMD="$WRAPPER_TEST_CMD"
  200. else
  201. WRAPPER_TEST_CMD="$WRAPPER_CMD-$DIST_OS-universal-64"
  202. if [ -x "$WRAPPER_TEST_CMD" ]
  203. then
  204. WRAPPER_CMD="$WRAPPER_TEST_CMD"
  205. else
  206. if [ ! -x "$WRAPPER_CMD" ]
  207. then
  208. echo "Unable to locate any of the following binaries:"
  209. outputFile "$WRAPPER_CMD-$DIST_OS-$DIST_ARCH-32"
  210. outputFile "$WRAPPER_CMD-$DIST_OS-universal-32"
  211. outputFile "$WRAPPER_CMD-$DIST_OS-$DIST_ARCH-64"
  212. outputFile "$WRAPPER_CMD-$DIST_OS-universal-64"
  213. outputFile "$WRAPPER_CMD"
  214. exit 1
  215. fi
  216. fi
  217. fi
  218. fi
  219. else
  220. WRAPPER_TEST_CMD="$WRAPPER_CMD-$DIST_OS-$DIST_ARCH-64"
  221. if [ -x "$WRAPPER_TEST_CMD" ]
  222. then
  223. WRAPPER_CMD="$WRAPPER_TEST_CMD"
  224. else
  225. if [ ! -x "$WRAPPER_CMD" ]
  226. then
  227. echo "Unable to locate any of the following binaries:"
  228. outputFile "$WRAPPER_CMD-$DIST_OS-$DIST_ARCH-32"
  229. outputFile "$WRAPPER_CMD-$DIST_OS-$DIST_ARCH-64"
  230. outputFile "$WRAPPER_CMD"
  231. exit 1
  232. fi
  233. fi
  234. fi
  235. fi
  236. # Build the nice clause
  237. if [ "X$PRIORITY" = "X" ]
  238. then
  239. CMDNICE=""
  240. else
  241. CMDNICE="nice -$PRIORITY"
  242. fi
  243. # Build the anchor file clause.
  244. if [ "X$IGNORE_SIGNALS" = "X" ]
  245. then
  246. ANCHORPROP=
  247. IGNOREPROP=
  248. else
  249. ANCHORPROP=wrapper.anchorfile=\"$ANCHORFILE\"
  250. IGNOREPROP=wrapper.ignore_signals=TRUE
  251. fi
  252. # Build the lock file clause. Only create a lock file if the lock directory exists on this platform.
  253. LOCKPROP=
  254. if [ -d $LOCKDIR ]
  255. then
  256. if [ -w $LOCKDIR ]
  257. then
  258. LOCKPROP=wrapper.lockfile=\"$LOCKFILE\"
  259. fi
  260. fi
  261. checkUser() {
  262. # $1 touchLock flag
  263. # $2 command
  264. # Check the configured user. If necessary rerun this script as the desired user.
  265. if [ "X$RUN_AS_USER" != "X" ]
  266. then
  267. # Resolve the location of the 'id' command
  268. IDEXE="/usr/xpg4/bin/id"
  269. if [ ! -x "$IDEXE" ]
  270. then
  271. IDEXE="/usr/bin/id"
  272. if [ ! -x "$IDEXE" ]
  273. then
  274. echo "Unable to locate 'id'."
  275. echo "Please report this message along with the location of the command on your system."
  276. exit 1
  277. fi
  278. fi
  279. if [ "`$IDEXE -u -n`" = "$RUN_AS_USER" ]
  280. then
  281. # Already running as the configured user. Avoid password prompts by not calling su.
  282. RUN_AS_USER=""
  283. fi
  284. fi
  285. if [ "X$RUN_AS_USER" != "X" ]
  286. then
  287. # If LOCKPROP and $RUN_AS_USER are defined then the new user will most likely not be
  288. # able to create the lock file. The Wrapper will be able to update this file once it
  289. # is created but will not be able to delete it on shutdown. If $2 is defined then
  290. # the lock file should be created for the current command
  291. if [ "X$LOCKPROP" != "X" ]
  292. then
  293. if [ "X$1" != "X" ]
  294. then
  295. # Resolve the primary group
  296. RUN_AS_GROUP=`groups $RUN_AS_USER | awk '{print $3}' | tail -1`
  297. if [ "X$RUN_AS_GROUP" = "X" ]
  298. then
  299. RUN_AS_GROUP=$RUN_AS_USER
  300. fi
  301. touch $LOCKFILE
  302. chown $RUN_AS_USER:$RUN_AS_GROUP $LOCKFILE
  303. fi
  304. fi
  305. # Still want to change users, recurse. This means that the user will only be
  306. # prompted for a password once. Variables shifted by 1
  307. su -m $RUN_AS_USER -c "\"$REALPATH\" $2"
  308. RETVAL=$?
  309. # Now that we are the original user again, we may need to clean up the lock file.
  310. if [ "X$LOCKPROP" != "X" ]
  311. then
  312. getpid
  313. if [ "X$pid" = "X" ]
  314. then
  315. # Wrapper is not running so make sure the lock file is deleted.
  316. if [ -f "$LOCKFILE" ]
  317. then
  318. rm "$LOCKFILE"
  319. fi
  320. fi
  321. fi
  322. exit $RETVAL
  323. fi
  324. }
  325. getpid() {
  326. if [ -f "$PIDFILE" ]
  327. then
  328. if [ -r "$PIDFILE" ]
  329. then
  330. pid=`cat "$PIDFILE"`
  331. if [ "X$pid" != "X" ]
  332. then
  333. # It is possible that 'a' process with the pid exists but that it is not the
  334. # correct process. This can happen in a number of cases, but the most
  335. # common is during system startup after an unclean shutdown.
  336. # The ps statement below looks for the specific wrapper command running as
  337. # the pid. If it is not found then the pid file is considered to be stale.
  338. pidtest=`$PSEXE -p $pid -o args | grep "$WRAPPER_CMD" | tail -1`
  339. if [ "X$pidtest" = "X" ]
  340. then
  341. # This is a stale pid file.
  342. rm -f "$PIDFILE"
  343. echo "Removed stale pid file: $PIDFILE"
  344. pid=""
  345. fi
  346. fi
  347. else
  348. echo "Cannot read $PIDFILE."
  349. exit 1
  350. fi
  351. fi
  352. }
  353. testpid() {
  354. pid=`$PSEXE -p $pid | grep $pid | grep -v grep | awk '{print $1}' | tail -1`
  355. if [ "X$pid" = "X" ]
  356. then
  357. # Process is gone so remove the pid file.
  358. rm -f "$PIDFILE"
  359. pid=""
  360. fi
  361. }
  362. console() {
  363. echo "Running $APP_LONG_NAME..."
  364. getpid
  365. if [ "X$pid" = "X" ]
  366. then
  367. # The string passed to eval must handles spaces in paths correctly.
  368. COMMAND_LINE="$CMDNICE \"$WRAPPER_CMD\" \"$WRAPPER_CONF\" wrapper.syslog.ident=$APP_NAME wrapper.pidfile=\"$PIDFILE\" $ANCHORPROP $LOCKPROP"
  369. eval $COMMAND_LINE
  370. else
  371. echo "$APP_LONG_NAME is already running."
  372. exit 1
  373. fi
  374. }
  375. start() {
  376. echo "Starting $APP_LONG_NAME..."
  377. getpid
  378. if [ "X$pid" = "X" ]
  379. then
  380. # The string passed to eval must handles spaces in paths correctly.
  381. COMMAND_LINE="$CMDNICE \"$WRAPPER_CMD\" \"$WRAPPER_CONF\" wrapper.syslog.ident=$APP_NAME wrapper.pidfile=\"$PIDFILE\" wrapper.daemonize=TRUE $ANCHORPROP $IGNOREPROP $LOCKPROP"
  382. eval $COMMAND_LINE
  383. else
  384. echo "$APP_LONG_NAME is already running."
  385. exit 1
  386. fi
  387. getpid
  388. if [ "X$pid" != "X" ]
  389. then
  390. echo "Started $APP_LONG_NAME."
  391. else
  392. echo "Failed to start $APP_LONG_NAME."
  393. fi
  394. }
  395. stopit() {
  396. echo "Stopping $APP_LONG_NAME..."
  397. getpid
  398. if [ "X$pid" = "X" ]
  399. then
  400. echo "$APP_LONG_NAME was not running."
  401. else
  402. if [ "X$IGNORE_SIGNALS" = "X" ]
  403. then
  404. # Running so try to stop it.
  405. kill $pid
  406. if [ $? -ne 0 ]
  407. then
  408. # An explanation for the failure should have been given
  409. echo "Unable to stop $APP_LONG_NAME."
  410. exit 1
  411. fi
  412. else
  413. rm -f "$ANCHORFILE"
  414. if [ -f "$ANCHORFILE" ]
  415. then
  416. # An explanation for the failure should have been given
  417. echo "Unable to stop $APP_LONG_NAME."
  418. exit 1
  419. fi
  420. fi
  421. # We can not predict how long it will take for the wrapper to
  422. # actually stop as it depends on settings in wrapper.conf.
  423. # Loop until it does.
  424. savepid=$pid
  425. CNT=0
  426. TOTCNT=0
  427. while [ "X$pid" != "X" ]
  428. do
  429. # Show a waiting message every 5 seconds.
  430. if [ "$CNT" -lt "5" ]
  431. then
  432. CNT=`expr $CNT + 1`
  433. else
  434. echo "Waiting for $APP_LONG_NAME to exit..."
  435. CNT=0
  436. fi
  437. TOTCNT=`expr $TOTCNT + 1`
  438. sleep 1
  439. testpid
  440. done
  441. pid=$savepid
  442. testpid
  443. if [ "X$pid" != "X" ]
  444. then
  445. echo "Failed to stop $APP_LONG_NAME."
  446. exit 1
  447. else
  448. echo "Stopped $APP_LONG_NAME."
  449. fi
  450. fi
  451. }
  452. status() {
  453. getpid
  454. if [ "X$pid" = "X" ]
  455. then
  456. echo "$APP_LONG_NAME is not running."
  457. exit 1
  458. else
  459. echo "$APP_LONG_NAME is running ($pid)."
  460. exit 0
  461. fi
  462. }
  463. dump() {
  464. echo "Dumping $APP_LONG_NAME..."
  465. getpid
  466. if [ "X$pid" = "X" ]
  467. then
  468. echo "$APP_LONG_NAME was not running."
  469. else
  470. kill -3 $pid
  471. if [ $? -ne 0 ]
  472. then
  473. echo "Failed to dump $APP_LONG_NAME."
  474. exit 1
  475. else
  476. echo "Dumped $APP_LONG_NAME."
  477. fi
  478. fi
  479. }
  480. case "$1" in
  481. 'console')
  482. checkUser touchlock $1
  483. console
  484. ;;
  485. 'start')
  486. checkUser touchlock $1
  487. start
  488. ;;
  489. 'stop')
  490. checkUser "" $1
  491. stopit
  492. ;;
  493. 'restart')
  494. checkUser touchlock $1
  495. stopit
  496. start
  497. ;;
  498. 'status')
  499. checkUser "" $1
  500. status
  501. ;;
  502. 'dump')
  503. checkUser "" $1
  504. dump
  505. ;;
  506. *)
  507. echo "Usage: $0 { console | start | stop | restart | status | dump }"
  508. exit 1
  509. ;;
  510. esac
  511. exit 0