1. #!/bin/sh
    2. #
    3. # RedHat system statup script for Jenkins
    4. # Based on SUSE system statup script for Jenkins
    5. # Copyright (C) 2007 Pascal Bleser
    6. #
    7. # This library is free software; you can redistribute it and/or modify it
    8. # under the terms of the GNU Lesser General Public License as published by
    9. # the Free Software Foundation; either version 2.1 of the License, or (at
    10. # your option) any later version.
    11. #
    12. # This library is distributed in the hope that it will be useful, but
    13. # WITHOUT ANY WARRANTY; without even the implied warranty of
    14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    15. # Lesser General Public License for more details.
    16. #
    17. # You should have received a copy of the GNU Lesser General Public
    18. # License along with this library; if not, write to the Free Software
    19. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,
    20. # USA.
    21. #
    22. ###############################################################################
    23. #
    24. # chkconfig: 35 99 01
    25. # description: Jenkins Automation Server
    26. #
    27. ###############################################################################
    28. ### BEGIN INIT INFO
    29. # Provides: jenkins
    30. # Required-Start: $local_fs $remote_fs $network $time $named
    31. # Should-Start: $time sendmail
    32. # Required-Stop: $local_fs $remote_fs $network $time $named
    33. # Should-Stop: $time sendmail
    34. # Default-Start: 3 5
    35. # Default-Stop: 0 1 2 6
    36. # Short-Description: Jenkins Automation Server
    37. # Description: Jenkins Automation Server
    38. ### END INIT INFO
    39. #export JAVA_HOME=/usr/java/jdk1.8.0_131
    40. export JAVA_HOME=/usr/java/jdk-12.0.2
    41. export PATH=$JAVA_HOME/bin:$PATH
    42. export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tool.jar
    43. # Check for missing binaries (stale symlinks should not happen)
    44. JENKINS_WAR="/usr/lib/jenkins/jenkins.war"
    45. test -r "$JENKINS_WAR" || { echo "$JENKINS_WAR not installed";
    46. if [ "$1" = "stop" ]; then exit 0;
    47. else exit 5; fi; }
    48. # Check for existence of needed config file and read it
    49. JENKINS_CONFIG=/etc/sysconfig/jenkins
    50. test -e "$JENKINS_CONFIG" || { echo "$JENKINS_CONFIG not existing";
    51. if [ "$1" = "stop" ]; then exit 0;
    52. else exit 6; fi; }
    53. test -r "$JENKINS_CONFIG" || { echo "$JENKINS_CONFIG not readable. Perhaps you forgot 'sudo'?";
    54. if [ "$1" = "stop" ]; then exit 0;
    55. else exit 6; fi; }
    56. JENKINS_PID_FILE="/var/run/jenkins.pid"
    57. JENKINS_LOCKFILE="/var/lock/subsys/jenkins"
    58. # Source function library.
    59. . /etc/init.d/functions
    60. # Read config
    61. [ -f "$JENKINS_CONFIG" ] && . "$JENKINS_CONFIG"
    62. # Set up environment accordingly to the configuration settings
    63. [ -n "$JENKINS_HOME" ] || { echo "JENKINS_HOME not configured in $JENKINS_CONFIG";
    64. if [ "$1" = "stop" ]; then exit 0;
    65. else exit 6; fi; }
    66. [ -d "$JENKINS_HOME" ] || { echo "JENKINS_HOME directory does not exist: $JENKINS_HOME";
    67. if [ "$1" = "stop" ]; then exit 0;
    68. else exit 1; fi; }
    69. # Search usable Java as /usr/bin/java might not point to minimal version required by Jenkins.
    70. # see http://www.nabble.com/guinea-pigs-wanted-----Hudson-RPM-for-RedHat-Linux-td25673707.html
    71. candidates="
    72. /etc/alternatives/java
    73. /usr/lib/jvm/java-1.8.0/bin/java
    74. /usr/lib/jvm/jre-1.8.0/bin/java
    75. /usr/lib/jvm/java-1.7.0/bin/java
    76. /usr/lib/jvm/jre-1.7.0/bin/java
    77. /usr/lib/jvm/java-11.0/bin/java
    78. /usr/lib/jvm/jre-11.0/bin/java
    79. /usr/lib/jvm/java-11-openjdk-amd64
    80. /usr/bin/java
    81. "
    82. for candidate in $candidates
    83. do
    84. [ -x "$JENKINS_JAVA_CMD" ] && break
    85. JENKINS_JAVA_CMD="$candidate"
    86. done
    87. JAVA_CMD="$JENKINS_JAVA_CMD $JENKINS_JAVA_OPTIONS -DJENKINS_HOME=$JENKINS_HOME -jar $JENKINS_WAR"
    88. PARAMS="--logfile=/var/log/jenkins/jenkins.log --webroot=/var/cache/jenkins/war --daemon"
    89. [ -n "$JENKINS_PORT" ] && PARAMS="$PARAMS --httpPort=$JENKINS_PORT"
    90. [ -n "$JENKINS_LISTEN_ADDRESS" ] && PARAMS="$PARAMS --httpListenAddress=$JENKINS_LISTEN_ADDRESS"
    91. [ -n "$JENKINS_HTTPS_PORT" ] && PARAMS="$PARAMS --httpsPort=$JENKINS_HTTPS_PORT"
    92. [ -n "$JENKINS_HTTPS_KEYSTORE" ] && PARAMS="$PARAMS --httpsKeyStore=$JENKINS_HTTPS_KEYSTORE"
    93. [ -n "$JENKINS_HTTPS_KEYSTORE_PASSWORD" ] && PARAMS="$PARAMS --httpsKeyStorePassword='$JENKINS_HTTPS_KEYSTORE_PASSWORD'"
    94. [ -n "$JENKINS_HTTPS_LISTEN_ADDRESS" ] && PARAMS="$PARAMS --httpsListenAddress=$JENKINS_HTTPS_LISTEN_ADDRESS"
    95. [ -n "$JENKINS_DEBUG_LEVEL" ] && PARAMS="$PARAMS --debug=$JENKINS_DEBUG_LEVEL"
    96. [ -n "$JENKINS_HANDLER_STARTUP" ] && PARAMS="$PARAMS --handlerCountStartup=$JENKINS_HANDLER_STARTUP"
    97. [ -n "$JENKINS_HANDLER_MAX" ] && PARAMS="$PARAMS --handlerCountMax=$JENKINS_HANDLER_MAX"
    98. [ -n "$JENKINS_HANDLER_IDLE" ] && PARAMS="$PARAMS --handlerCountMaxIdle=$JENKINS_HANDLER_IDLE"
    99. [ -n "$JENKINS_ARGS" ] && PARAMS="$PARAMS $JENKINS_ARGS"
    100. if [ "$JENKINS_ENABLE_ACCESS_LOG" = "yes" ]; then
    101. PARAMS="$PARAMS --accessLoggerClassName=winstone.accesslog.SimpleAccessLogger --simpleAccessLogger.format=combined --simpleAccessLogger.file=/var/log/jenkins/access_log"
    102. fi
    103. RETVAL=0
    104. case "$1" in
    105. start)
    106. echo -n "Starting Jenkins "
    107. daemon --user "$JENKINS_USER" --pidfile "$JENKINS_PID_FILE" $JAVA_CMD $PARAMS > /dev/null
    108. RETVAL=$?
    109. if [ $RETVAL = 0 ]; then
    110. success
    111. echo > "$JENKINS_PID_FILE" # just in case we fail to find it
    112. MY_SESSION_ID=`/bin/ps h -o sess -p $$`
    113. # get PID
    114. /bin/ps hww -u "$JENKINS_USER" -o sess,ppid,pid,cmd | \
    115. while read sess ppid pid cmd; do
    116. [ "$ppid" = 1 ] || continue
    117. # this test doesn't work because Jenkins sets a new Session ID
    118. # [ "$sess" = "$MY_SESSION_ID" ] || continue
    119. echo "$cmd" | grep $JENKINS_WAR > /dev/null
    120. [ $? = 0 ] || continue
    121. # found a PID
    122. echo $pid > "$JENKINS_PID_FILE"
    123. done
    124. touch $JENKINS_LOCKFILE
    125. else
    126. failure
    127. fi
    128. echo
    129. ;;
    130. stop)
    131. echo -n "Shutting down Jenkins "
    132. killproc jenkins
    133. rm -f $JENKINS_LOCKFILE
    134. RETVAL=$?
    135. echo
    136. ;;
    137. try-restart|condrestart)
    138. if test "$1" = "condrestart"; then
    139. echo "${attn} Use try-restart ${done}(LSB)${attn} rather than condrestart ${warn}(RH)${norm}"
    140. fi
    141. $0 status
    142. if test $? = 0; then
    143. $0 restart
    144. else
    145. : # Not running is not a failure.
    146. fi
    147. ;;
    148. restart)
    149. $0 stop
    150. $0 start
    151. ;;
    152. force-reload)
    153. echo -n "Reload service Jenkins "
    154. $0 try-restart
    155. ;;
    156. reload)
    157. $0 restart
    158. ;;
    159. status)
    160. status jenkins
    161. RETVAL=$?
    162. ;;
    163. probe)
    164. ## Optional: Probe for the necessity of a reload, print out the
    165. ## argument to this init script which is required for a reload.
    166. ## Note: probe is not (yet) part of LSB (as of 1.9)
    167. test "$JENKINS_CONFIG" -nt "$JENKINS_PID_FILE" && echo reload
    168. ;;
    169. *)
    170. echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload|probe}"
    171. exit 1
    172. ;;
    173. esac
    174. exit $RETVAL