elasticsearch 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #!/bin/sh
  2. # OPTIONS:
  3. # -d daemonize (run in background)
  4. # -p pidfile write PID to <pidfile>
  5. # -h
  6. # --help print command line options
  7. # -v print elasticsearch version, then exit
  8. # -D prop set JAVA system property
  9. # -X prop set non-standard JAVA system property
  10. # --prop=val
  11. # --prop val set elasticsearch property (i.e. -Des.<prop>=<val>)
  12. # CONTROLLING STARTUP:
  13. #
  14. # This script relies on few environment variables to determine startup
  15. # behavior, those variables are:
  16. #
  17. # ES_CLASSPATH -- A Java classpath containing everything necessary to run.
  18. # JAVA_OPTS -- Additional arguments to the JVM for heap size, etc
  19. # ES_JAVA_OPTS -- External Java Opts on top of the defaults set
  20. #
  21. #
  22. # Optionally, exact memory values can be set using the following values, note,
  23. # they can still be set using the `ES_JAVA_OPTS`. Sample format include "512m", and "10g".
  24. #
  25. # ES_HEAP_SIZE -- Sets both the minimum and maximum memory to allocate (recommended)
  26. #
  27. # As a convenience, a fragment of shell is sourced in order to set one or
  28. # more of these variables. This so-called `include' can be placed in a
  29. # number of locations and will be searched for in order. The lowest
  30. # priority search path is the same directory as the startup script, and
  31. # since this is the location of the sample in the project tree, it should
  32. # almost work Out Of The Box.
  33. #
  34. # Any serious use-case though will likely require customization of the
  35. # include. For production installations, it is recommended that you copy
  36. # the sample to one of /usr/share/elasticsearch/elasticsearch.in.sh,
  37. # /usr/local/share/elasticsearch/elasticsearch.in.sh, or
  38. # /opt/elasticsearch/elasticsearch.in.sh and make your modifications there.
  39. #
  40. # Another option is to specify the full path to the include file in the
  41. # environment. For example:
  42. #
  43. # $ ES_INCLUDE=/path/to/in.sh elasticsearch -p /var/run/es.pid
  44. #
  45. # Note: This is particularly handy for running multiple instances on a
  46. # single installation, or for quick tests.
  47. #
  48. # If you would rather configure startup entirely from the environment, you
  49. # can disable the include by exporting an empty ES_INCLUDE, or by
  50. # ensuring that no include files exist in the aforementioned search list.
  51. # Be aware that you will be entirely responsible for populating the needed
  52. # environment variables.
  53. # Maven will replace the project.name with elasticsearch below. If that
  54. # hasn't been done, we assume that this is not a packaged version and the
  55. # user has forgotten to run Maven to create a package.
  56. IS_PACKAGED_VERSION='${project.name}'
  57. if [ "$IS_PACKAGED_VERSION" != "elasticsearch" ]; then
  58. cat >&2 << EOF
  59. Error: You must build the project with Maven or download a pre-built package
  60. before you can run Elasticsearch. See 'Building from Source' in README.textile
  61. or visit http://www.elasticsearch.org/download to get a pre-built package.
  62. EOF
  63. exit 1
  64. fi
  65. CDPATH=""
  66. SCRIPT="$0"
  67. # SCRIPT may be an arbitrarily deep series of symlinks. Loop until we have the concrete path.
  68. while [ -h "$SCRIPT" ] ; do
  69. ls=`ls -ld "$SCRIPT"`
  70. # Drop everything prior to ->
  71. link=`expr "$ls" : '.*-> \(.*\)$'`
  72. if expr "$link" : '/.*' > /dev/null; then
  73. SCRIPT="$link"
  74. else
  75. SCRIPT=`dirname "$SCRIPT"`/"$link"
  76. fi
  77. done
  78. # determine elasticsearch home
  79. ES_HOME=`dirname "$SCRIPT"`/..
  80. # make ELASTICSEARCH_HOME absolute
  81. ES_HOME=`cd "$ES_HOME"; pwd`
  82. # If an include wasn't specified in the environment, then search for one...
  83. if [ "x$ES_INCLUDE" = "x" ]; then
  84. # Locations (in order) to use when searching for an include file.
  85. for include in /usr/share/elasticsearch/elasticsearch.in.sh \
  86. /usr/local/share/elasticsearch/elasticsearch.in.sh \
  87. /opt/elasticsearch/elasticsearch.in.sh \
  88. ~/.elasticsearch.in.sh \
  89. "$ES_HOME/bin/elasticsearch.in.sh" \
  90. "`dirname "$0"`"/elasticsearch.in.sh; do
  91. if [ -r "$include" ]; then
  92. . "$include"
  93. break
  94. fi
  95. done
  96. # ...otherwise, source the specified include.
  97. elif [ -r "$ES_INCLUDE" ]; then
  98. . "$ES_INCLUDE"
  99. fi
  100. if [ -x "$JAVA_HOME/bin/java" ]; then
  101. JAVA="$JAVA_HOME/bin/java"
  102. else
  103. JAVA=`which java`
  104. fi
  105. if [ ! -x "$JAVA" ]; then
  106. echo "Could not find any executable java binary. Please install java in your PATH or set JAVA_HOME"
  107. exit 1
  108. fi
  109. if [ -z "$ES_CLASSPATH" ]; then
  110. echo "You must set the ES_CLASSPATH var" >&2
  111. exit 1
  112. fi
  113. # Special-case path variables.
  114. case `uname` in
  115. CYGWIN*)
  116. ES_CLASSPATH=`cygpath -p -w "$ES_CLASSPATH"`
  117. ES_HOME=`cygpath -p -w "$ES_HOME"`
  118. ;;
  119. esac
  120. launch_service()
  121. {
  122. pidpath=$1
  123. daemonized=$2
  124. props=$3
  125. es_parms="-Delasticsearch"
  126. if [ "x$pidpath" != "x" ]; then
  127. es_parms="$es_parms -Des.pidfile=$pidpath"
  128. fi
  129. # Make sure we dont use any predefined locale, as we check some exception message strings and rely on english language
  130. # As those strings are created by the OS, they are dependant on the configured locale
  131. LANG=en_US.UTF-8
  132. LC_ALL=en_US.UTF-8
  133. export HOSTNAME=`hostname -s`
  134. # The es-foreground option will tell Elasticsearch not to close stdout/stderr, but it's up to us not to daemonize.
  135. if [ "x$daemonized" = "x" ]; then
  136. es_parms="$es_parms -Des.foreground=yes"
  137. eval exec "$JAVA" $JAVA_OPTS $ES_JAVA_OPTS $es_parms "\"-Des.path.home=$ES_HOME\"" -cp "\"$ES_CLASSPATH\"" $props \
  138. org.elasticsearch.bootstrap.Elasticsearch
  139. # exec without running it in the background, makes it replace this shell, we'll never get here...
  140. # no need to return something
  141. else
  142. # Startup Elasticsearch, background it, and write the pid.
  143. eval exec "$JAVA" $JAVA_OPTS $ES_JAVA_OPTS $es_parms "\"-Des.path.home=$ES_HOME\"" -cp "\"$ES_CLASSPATH\"" $props \
  144. org.elasticsearch.bootstrap.Elasticsearch <&- &
  145. return $?
  146. fi
  147. }
  148. # Print command line usage / help
  149. usage() {
  150. echo "Usage: $0 [-vdh] [-p pidfile] [-D prop] [-X prop]"
  151. echo "Start elasticsearch."
  152. echo " -d daemonize (run in background)"
  153. echo " -p pidfile write PID to <pidfile>"
  154. echo " -h"
  155. echo " --help print command line options"
  156. echo " -v print elasticsearch version, then exit"
  157. echo " -D prop set JAVA system property"
  158. echo " -X prop set non-standard JAVA system property"
  159. echo " --prop=val"
  160. echo " --prop val set elasticsearch property (i.e. -Des.<prop>=<val>)"
  161. }
  162. # Parse any long getopt options and put them into properties before calling getopt below
  163. # Be dash compatible to make sure running under ubuntu works
  164. ARGV=""
  165. while [ $# -gt 0 ]
  166. do
  167. case $1 in
  168. --help) ARGV="$ARGV -h"; shift;;
  169. --*=*) properties="$properties -Des.${1#--}"
  170. shift 1
  171. ;;
  172. --*) [ $# -le 1 ] && {
  173. echo "Option requires an argument: '$1'."
  174. shift
  175. continue
  176. }
  177. properties="$properties -Des.${1#--}=$2"
  178. shift 2
  179. ;;
  180. *) ARGV="$ARGV $1" ; shift
  181. esac
  182. done
  183. # Parse any command line options.
  184. args=`getopt vdhp:D:X: $ARGV`
  185. eval set -- "$args"
  186. while true; do
  187. case $1 in
  188. -v)
  189. eval "$JAVA" $JAVA_OPTS $ES_JAVA_OPTS $es_parms "\"-Des.path.home=$ES_HOME\"" -cp "\"$ES_CLASSPATH\"" $props \
  190. org.elasticsearch.Version
  191. exit 0
  192. ;;
  193. -p)
  194. pidfile="$2"
  195. shift 2
  196. ;;
  197. -d)
  198. daemonized="yes"
  199. shift
  200. ;;
  201. -h)
  202. usage
  203. exit 0
  204. ;;
  205. -D)
  206. properties="$properties -D$2"
  207. shift 2
  208. ;;
  209. -X)
  210. properties="$properties -X$2"
  211. shift 2
  212. ;;
  213. --)
  214. shift
  215. break
  216. ;;
  217. *)
  218. echo "Error parsing argument $1!" >&2
  219. usage
  220. exit 1
  221. ;;
  222. esac
  223. done
  224. # Start up the service
  225. launch_service "$pidfile" "$daemonized" "$properties"
  226. exit $?