install-service.sh.in 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  1. #!/usr/bin/env sh
  2. # SPDX-License-Identifier: GPL-3.0-or-later
  3. # Handle installation of the Netdata agent as a system service.
  4. #
  5. # Exit codes:
  6. # 0 - Successfully installed service.
  7. # 1 - Invalid arguments or other internal error.
  8. # 2 - Unable to detect system service type.
  9. # 3 - Detected system service type, but type not supported.
  10. # 4 - Detected system service type, but could not install due to other issues.
  11. # 5 - Platform not supported.
  12. set -e
  13. SCRIPT_SOURCE="$(
  14. self=${0}
  15. while [ -L "${self}" ]
  16. do
  17. cd "${self%/*}" || exit 1
  18. self=$(readlink "${self}")
  19. done
  20. cd "${self%/*}" || exit 1
  21. echo "$(pwd -P)/${self##*/}"
  22. )"
  23. DUMP_CMDS=0
  24. ENABLE="auto"
  25. EXPORT_CMDS=0
  26. INSTALL=1
  27. LINUX_INIT_TYPES="systemd openrc lsb initd runit"
  28. PLATFORM="$(uname -s)"
  29. SHOW_SVC_TYPE=0
  30. SVC_SOURCE="@libsysdir_POST@"
  31. SVC_TYPE="detect"
  32. WSL_ERROR_MSG="We appear to be running in WSL and were unable to find a usable service manager. We currently support systemd, LSB init scripts, and traditional init.d style init scripts when running under WSL."
  33. # =====================================================================
  34. # Utility functions
  35. cleanup() {
  36. ec="${?}"
  37. if [ -n "${NETDATA_SAVE_WARNINGS}" ]; then
  38. if [ -n "${NETDATA_PROPAGATE_WARNINGS}" ]; then
  39. export NETDATA_WARNINGS="${NETDATA_WARNINGS}${SAVED_WARNINGS}"
  40. fi
  41. fi
  42. trap - EXIT
  43. exit "${ec}"
  44. }
  45. info() {
  46. printf >&2 "%s\n" "${*}"
  47. }
  48. warning() {
  49. if [ -n "${NETDATA_SAVE_WARNINGS}" ]; then
  50. SAVED_WARNINGS="${SAVED_WARNINGS}\n - ${*}"
  51. fi
  52. printf >&2 "WARNING: %s\n" "${*}"
  53. }
  54. error() {
  55. if [ -n "${NETDATA_SAVE_WARNINGS}" ]; then
  56. SAVED_WARNINGS="${SAVED_WARNINGS}\n - ${*}"
  57. fi
  58. printf >&2 "ERROR: %s\n" "${*}"
  59. }
  60. get_os_key() {
  61. if [ -f /etc/os-release ]; then
  62. # shellcheck disable=SC1091
  63. . /etc/os-release || return 1
  64. echo "${ID}-${VERSION_ID}"
  65. elif [ -f /etc/redhat-release ]; then
  66. cat /etc/redhat-release
  67. else
  68. echo "unknown"
  69. fi
  70. }
  71. valid_types() {
  72. case "${PLATFORM}" in
  73. Linux)
  74. echo "detect ${LINUX_INIT_TYPES}"
  75. ;;
  76. FreeBSD)
  77. echo "detect freebsd"
  78. ;;
  79. Darwin)
  80. echo "detect launchd"
  81. ;;
  82. *)
  83. echo "detect"
  84. ;;
  85. esac
  86. }
  87. install_generic_service() {
  88. svc_path="${1}"
  89. svc_type_name="${2}"
  90. svc_file="${3}"
  91. svc_enable_hook="${4}"
  92. svc_disable_hook="${5}"
  93. info "Installing ${svc_type_name} service file."
  94. if [ ! -f "${svc_file}" ] && [ "${ENABLE}" = "auto" ]; then
  95. ENABLE="enable"
  96. fi
  97. if ! install -p -m 0755 -o 0 -g 0 "${SVC_SOURCE}/${svc_path}/netdata" "${svc_file}"; then
  98. error "Failed to install service file."
  99. exit 4
  100. fi
  101. case "${ENABLE}" in
  102. auto) true ;;
  103. disable)
  104. info "Disabling Netdata service."
  105. ${svc_disable_hook}
  106. ;;
  107. enable)
  108. info "Enabling Netdata service."
  109. ${svc_enable_hook}
  110. ;;
  111. esac
  112. }
  113. dump_cmds() {
  114. [ -n "${NETDATA_START_CMD}" ] && echo "NETDATA_START_CMD='${NETDATA_START_CMD}'"
  115. [ -n "${NETDATA_STOP_CMD}" ] && echo "NETDATA_STOP_CMD='${NETDATA_STOP_CMD}'"
  116. [ -n "${NETDATA_INSTALLER_START_CMD}" ] && echo "NETDATA_INSTALLER_START_CMD='${NETDATA_INSTALLER_START_CMD}'"
  117. return 0
  118. }
  119. export_cmds() {
  120. [ -n "${NETDATA_START_CMD}" ] && export NETDATA_START_CMD="${NETDATA_START_CMD}"
  121. [ -n "${NETDATA_STOP_CMD}" ] && export NETDATA_STOP_CMD="${NETDATA_STOP_CMD}"
  122. [ -n "${NETDATA_INSTALLER_START_CMD}" ] && export NETDATA_INSTALLER_START_CMD="${NETDATA_INSTALLER_START_COMMAND}"
  123. return 0
  124. }
  125. save_cmds() {
  126. dump_cmds > "${SAVE_CMDS_PATH}"
  127. }
  128. # =====================================================================
  129. # Help functions
  130. usage() {
  131. cat << HEREDOC
  132. USAGE: install-service.sh [options]
  133. where options include:
  134. --source Specify where to find the service files to install (default ${SVC_SOURCE}).
  135. --type Specify the type of service file to install. Specify a type of 'help' to get a list of valid types for your platform.
  136. --show-type Display information about what service managers are detected.
  137. --cmds Additionally print a list of commands for starting and stopping the agent with the detected service type.
  138. --export-cmds Export the variables that would be printed by the --cmds option.
  139. --cmds-only Don't install, just handle the --cmds or --export-cmds option.
  140. --enable Explicitly enable the service on install (default is to enable if not already installed).
  141. --disable Explicitly disable the service on install.
  142. --help Print this help information.
  143. HEREDOC
  144. }
  145. help_types() {
  146. cat << HEREDOC
  147. Valid service types for ${PLATFORM} are:
  148. $(valid_types)
  149. HEREDOC
  150. }
  151. # =====================================================================
  152. # systemd support functions
  153. _check_systemd() {
  154. pids=''
  155. p=''
  156. myns=''
  157. ns=''
  158. # if the directory /lib/systemd/system OR /usr/lib/systemd/system (SLES 12.x) does not exit, it is not systemd
  159. if [ ! -d /lib/systemd/system ] && [ ! -d /usr/lib/systemd/system ]; then
  160. echo "NO" && return 0
  161. fi
  162. # if there is no systemctl command, it is not systemd
  163. [ -z "$(command -v systemctl 2>/dev/null || true)" ] && echo "NO" && return 0
  164. # if pid 1 is systemd, it is systemd
  165. [ "$(basename "$(readlink /proc/1/exe)" 2> /dev/null)" = "systemd" ] && echo "YES" && return 0
  166. # it ‘is’ systemd at this point, but systemd might not be running
  167. # if not, return 2 to indicate ‘systemd, but not running’
  168. pids=$(safe_pidof systemd 2> /dev/null)
  169. [ -z "${pids}" ] && echo "OFFLINE" && return 0
  170. # check if the running systemd processes are not in our namespace
  171. myns="$(readlink /proc/self/ns/pid 2> /dev/null)"
  172. for p in ${pids}; do
  173. ns="$(readlink "/proc/${p}/ns/pid" 2> /dev/null)"
  174. # if pid of systemd is in our namespace, it is systemd
  175. [ -n "${myns}" ] && [ "${myns}" = "${ns}" ] && echo "YES" && return 0
  176. done
  177. # else, it is not systemd
  178. echo "NO"
  179. }
  180. check_systemd() {
  181. if [ -z "${IS_SYSTEMD}" ]; then
  182. IS_SYSTEMD="$(_check_systemd)"
  183. fi
  184. echo "${IS_SYSTEMD}"
  185. }
  186. get_systemd_service_dir() {
  187. if [ -w "/lib/systemd/system" ]; then
  188. echo "/lib/systemd/system"
  189. elif [ -w "/usr/lib/systemd/system" ]; then
  190. echo "/usr/lib/systemd/system"
  191. elif [ -w "/etc/systemd/system" ]; then
  192. echo "/etc/systemd/system"
  193. else
  194. error "Unable to detect systemd service directory."
  195. exit 4
  196. fi
  197. }
  198. install_systemd_service() {
  199. SRCFILE="${SVC_SOURCE}/systemd/netdata.service"
  200. PRESET_FILE="${SVC_SOURCE}/systemd/50-netdata.preset"
  201. SVCDIR="$(get_systemd_service_dir)"
  202. if [ "$(systemctl --version | head -n 1 | cut -f 2 -d ' ')" -le 235 ]; then
  203. SRCFILE="${SVC_SOURCE}/systemd/netdata.service.v235"
  204. fi
  205. if [ "${ENABLE}" = "auto" ]; then
  206. if [ "$(check_systemd)" = "YES" ]; then
  207. IS_NETDATA_ENABLED="$(systemctl is-enabled netdata 2> /dev/null || echo "Netdata not there")"
  208. fi
  209. if [ "${IS_NETDATA_ENABLED}" = "disabled" ]; then
  210. ENABLE="disable"
  211. else
  212. ENABLE="enable"
  213. fi
  214. fi
  215. info "Installing systemd service..."
  216. if ! install -p -m 0644 -o 0 -g 0 "${SRCFILE}" "${SVCDIR}/netdata.service"; then
  217. error "Failed to install systemd service file."
  218. exit 4
  219. fi
  220. if [ -f "${PRESET_FILE}" ]; then
  221. if ! install -p -m 0644 -o 0 -g 0 "${PRESET_FILE}" "${SVCDIR}-preset/50-netdata.preset"; then
  222. warning "Failed to install netdata preset file."
  223. fi
  224. fi
  225. if [ "$(check_systemd)" = "YES" ]; then
  226. if ! systemctl daemon-reload; then
  227. warning "Failed to reload systemd unit files."
  228. fi
  229. if ! systemctl "${ENABLE}" netdata; then
  230. warning "Failed to ${ENABLE} Netdata service."
  231. fi
  232. fi
  233. }
  234. systemd_cmds() {
  235. if [ "$(check_systemd)" = "YES" ]; then
  236. NETDATA_START_CMD='systemctl start netdata'
  237. NETDATA_STOP_CMD='systemctl stop netdata'
  238. else # systemd is not running, use external defaults by providing no commands
  239. warning "Detected systemd, but not booted using systemd. Unable to provide commands to start or stop Netdata using the service manager."
  240. fi
  241. }
  242. # =====================================================================
  243. # OpenRC support functions
  244. _check_openrc() {
  245. # if /lib/rc/sh/functions.sh does not exist, it's not OpenRC
  246. [ ! -f /lib/rc/sh/functions.sh ] && echo "NO" && return 0
  247. # if there is no /etc/init.d, it's not OpenRC
  248. [ ! -d /etc/init.d ] && echo "NO" && return 0
  249. # if there is no /etc/conf.d, it's not OpenRC
  250. [ ! -d /etc/conf.d ] && echo "NO" && return 0
  251. # if there is no rc-update command, it's not OpenRC
  252. [ -z "$(command -v rc-update 2>/dev/null || true)" ] && echo "NO" && return 0
  253. # If /run/openrc/softlevel exists, it's OpenRC
  254. [ -f /run/openrc/softlevel ] && echo "YES" && return 0
  255. # if PID 1 is openrc-init, it's OpenRC
  256. [ "$(basename "$(readlink /proc/1/exe)" 2> /dev/null)" = "openrc-init" ] && echo "YES" && return 0
  257. # if there is an openrc command, it's OpenRC, but not booted as such
  258. [ -n "$(command -v openrc 2>/dev/null || true)" ] && echo "OFFLINE" && return 0
  259. # if /etc/init.d/local exists and has `openrc-run` in it's shebang line, it’s OpenRC, but not booted as such
  260. [ -r /etc/init.d/local ] && head -n 1 /etc/init.d/local | grep -q openrc-run && echo "OFFLINE" && return 0
  261. # Otherwise, it’s not OpenRC
  262. echo "NO" && return 0
  263. }
  264. check_openrc() {
  265. if [ -z "${IS_OPENRC}" ]; then
  266. IS_OPENRC="$(_check_openrc)"
  267. fi
  268. echo "${IS_OPENRC}"
  269. }
  270. enable_openrc() {
  271. if [ "$(check_openrc)" = "YES" ]; then
  272. runlevel="$(rc-status -r)"
  273. fi
  274. runlevel="${runlevel:-default}"
  275. if ! rc-update add netdata "${runlevel}"; then
  276. warning "Failed to enable Netdata service in runlevel ${runlevel}."
  277. fi
  278. }
  279. disable_openrc() {
  280. for runlevel in /etc/runlevels/*; do
  281. if [ -e "${runlevel}/netdata" ]; then
  282. runlevel="$(basename "${runlevel}")"
  283. if ! rc-update del netdata "${runlevel}"; then
  284. warning "Failed to disable Netdata service in runlevel ${runlevel}."
  285. fi
  286. fi
  287. done
  288. }
  289. install_openrc_service() {
  290. install_generic_service openrc/init.d OpenRC /etc/init.d/netdata enable_openrc disable_openrc
  291. if [ ! -f /etc/conf.d/netdata ]; then
  292. info "Installing OpenRC configuration file."
  293. if ! install -p -m 0755 -o 0 -g 0 "${SVC_SOURCE}/openrc/conf.d/netdata" "/etc/conf.d/netdata"; then
  294. warning "Failed to install configuration file, however the service will still work."
  295. fi
  296. fi
  297. }
  298. openrc_cmds() {
  299. if [ "$(check_openrc)" = "YES" ]; then
  300. NETDATA_START_CMD='rc-service netdata start'
  301. NETDATA_STOP_CMD='rc-service netdata stop'
  302. else # Not booted using OpenRC, use external defaults by not providing commands.
  303. warning "Detected OpenRC, but the system is not booted using OpenRC. Unable to provide commands to start or stop Netdata using the service manager."
  304. fi
  305. }
  306. # =====================================================================
  307. # LSB init script support functions
  308. _check_lsb_ignore_systemd() {
  309. # if there is no /etc/init.d directory, it’s not an LSB system
  310. [ ! -d /etc/init.d ] && echo "NO" && return 0
  311. # If it's an OpenRC system, then it's not an LSB system
  312. [ "$(check_openrc)" != "NO" ] && echo "NO" && return 0
  313. # If /lib/lsb/init-functions exists, it’s an LSB system
  314. [ -f /lib/lsb/init-functions ] && echo "YES" && return 0
  315. echo "NO" && return 0
  316. }
  317. _check_lsb() {
  318. # if there is _any_ systemd, it’s not an LSB system
  319. [ "$(check_systemd)" != "NO" ] && echo "NO" && return 0
  320. _check_lsb_ignore_systemd
  321. }
  322. check_lsb() {
  323. if [ -z "${IS_LSB}" ]; then
  324. IS_LSB="$(_check_lsb)"
  325. fi
  326. echo "${IS_LSB}"
  327. }
  328. enable_lsb() {
  329. if ! update-rc.d netdata defaults; then
  330. warning "Failed to enable Netdata service."
  331. elif ! update-rc.d netdata defaults-disabled; then
  332. warning "Failed to fully enable Netdata service."
  333. fi
  334. }
  335. disable_lsb() {
  336. if ! update-rc.d netdata remove; then
  337. warning "Failed to disable Netdata service."
  338. fi
  339. }
  340. install_lsb_service() {
  341. install_generic_service lsb/init.d LSB /etc/init.d/netdata enable_lsb disable_lsb
  342. }
  343. lsb_cmds() {
  344. NETDATA_START_CMD='/etc/init.d/netdata start'
  345. NETDATA_STOP_CMD='/etc/init.d/netdata stop'
  346. }
  347. # =====================================================================
  348. # init.d init script support functions
  349. _check_initd_ignore_systemd() {
  350. # if there is no /etc/init.d directory, it’s not an init.d system
  351. [ ! -d /etc/init.d ] && echo "NO" && return 1
  352. # if there is no chkconfig command, it's not a (usable) init.d system
  353. [ -z "$(command -v chkconfig 2>/dev/null || true)" ] && echo "NO" && return 0
  354. # if there is _any_ openrc, it’s not init.d
  355. [ "$(check_openrc)" != "NO" ] && echo "NO" && return 0
  356. # if it's not an LSB setup, it’s init.d
  357. [ "$(check_lsb)" != "NO" ] && echo "NO" && return 0
  358. echo "YES" && return 0
  359. }
  360. _check_initd() {
  361. # if there is _any_ systemd, it’s not init.d
  362. [ "$(check_systemd)" != "NO" ] && echo "NO" && return 0
  363. _check_initd_ignore_systemd
  364. }
  365. check_initd() {
  366. if [ -z "${IS_INITD}" ]; then
  367. IS_INITD="$(_check_initd)"
  368. fi
  369. echo "${IS_INITD}"
  370. }
  371. enable_initd() {
  372. if ! chkconfig netdata on; then
  373. warning "Failed to enable Netdata service."
  374. fi
  375. }
  376. disable_initd() {
  377. if ! chkconfig netdata off; then
  378. warning "Failed to disable Netdata service."
  379. fi
  380. }
  381. install_initd_service() {
  382. install_generic_service initd/init.d init.d /etc/init.d/netdata enable_initd disable_initd
  383. }
  384. initd_cmds() {
  385. NETDATA_START_CMD='/etc/init.d/netdata start'
  386. NETDATA_STOP_CMD='/etc/init.d/netdata stop'
  387. }
  388. # =====================================================================
  389. # runit support functions
  390. _check_runit() {
  391. # if there is no runsvdir command, then it's not runit
  392. [ -z "$(command -v runsvdir 2>/dev/null || true)" ] && echo "NO" && return 0
  393. # if there is no runit command, then it's not runit
  394. [ -z "$(command -v runit 2>/dev/null || true)" ] && echo "NO" && return 0
  395. # if /run/runit exists, then it's runit
  396. [ -d /run/runit ] && echo "YES" && return 0
  397. # if /etc/runit/1 exists and is executable, then it's runit
  398. [ -x /etc/runit/1 ] && echo "YES" && return 0
  399. echo "NO" && return 0
  400. }
  401. check_runit() {
  402. if [ -z "${IS_RUNIT}" ]; then
  403. IS_RUNIT="$(_check_runit)"
  404. fi
  405. echo "${IS_RUNIT}"
  406. }
  407. install_runit_service() {
  408. if [ -d /etc/sv ]; then
  409. svc_dir="/etc/sv/netdata"
  410. elif [ -d /etc/runit/sv ]; then
  411. svc_dir="/etc/runit/sv/netdata"
  412. else
  413. error "Failed to locate service directory"
  414. exit 4
  415. fi
  416. if [ -d /service ]; then
  417. live_svc_dir="/service"
  418. elif [ -d /var/service ]; then
  419. live_svc_dir="/var/service"
  420. elif [ -d /run/runit/service ]; then
  421. live_svc_dir="/run/runit/service"
  422. elif [ -d /etc/runit/runsvdir/default ]; then
  423. live_svc_dir="/etc/runit/runsvdir/default"
  424. else
  425. error "Failed to locate live service directory"
  426. exit 4
  427. fi
  428. svc_file="${svc_dir}/run"
  429. info "Installing runit service file."
  430. if [ ! -f "${svc_file}" ] && [ "${ENABLE}" = "auto" ]; then
  431. ENABLE="enable"
  432. fi
  433. if ! install -D -p -m 0755 -o 0 -g 0 "${SVC_SOURCE}/runit/run" "${svc_file}"; then
  434. error "Failed to install service file."
  435. exit 4
  436. fi
  437. case ${ENABLE} in
  438. enable)
  439. if ! ln -s "${svc_dir}" "${live_svc_dir}"; then
  440. warning "Failed to enable the Netdata service."
  441. fi
  442. ;;
  443. disable)
  444. if ! rm "${live_svc_dir}/netdata"; then
  445. warning "Failed to disable the Netdata service."
  446. fi
  447. ;;
  448. esac
  449. }
  450. runit_cmds() {
  451. NETDATA_START_CMD="sv start netdata"
  452. NETDATA_STOP_CMD="sv stop netdata"
  453. }
  454. # =====================================================================
  455. # WSL support functions
  456. _check_wsl() {
  457. # If uname -r contains the string WSL, then it's WSL.
  458. uname -r | grep -q 'WSL' && echo "YES" && return 0
  459. # If uname -r contains the string Microsoft, then it's WSL.
  460. # This probably throws a false positive on CBL-Mariner, but it's part of what MS officially recommends for
  461. # detecting if you're running under WSL.
  462. uname -r | grep -q "Microsoft" && echo "YES" && return 0
  463. echo "NO" && return 0
  464. }
  465. check_wsl() {
  466. if [ -z "${IS_WSL}" ]; then
  467. IS_WSL="$(_check_wsl)"
  468. fi
  469. echo "${IS_WSL}"
  470. }
  471. install_wsl_service() {
  472. error "${WSL_ERROR_MSG}"
  473. exit 3
  474. }
  475. wsl_cmds() {
  476. error "${WSL_ERROR_MSG}"
  477. exit 3
  478. }
  479. # =====================================================================
  480. # FreeBSD support functions
  481. enable_freebsd() {
  482. if ! sysrc netdata_enable=YES; then
  483. warning "Failed to enable netdata service."
  484. fi
  485. }
  486. disable_freebsd() {
  487. if ! sysrc netdata_enable=NO; then
  488. warning "Failed to disable netdata service."
  489. fi
  490. }
  491. install_freebsd_service() {
  492. install_generic_service freebsd/rc.d "FreeBSD rc.d" /usr/local/etc/rc.d/netdata enable_freebsd disable_freebsd
  493. }
  494. freebsd_cmds() {
  495. NETDATA_START_CMD='service netdata start'
  496. NETDATA_STOP_CMD='service netdata stop'
  497. NETDATA_INSTALLER_START_CMD='service netdata onestart'
  498. }
  499. # =====================================================================
  500. # macOS support functions
  501. install_darwin_service() {
  502. info "Installing macOS plist file for launchd."
  503. if ! install -C -S -p -m 0644 -o 0 -g 0 "${SVC_SOURCE}/launchd/netdata.plist" /Library/LaunchDaemons/com.github.netdata.plist; then
  504. error "Failed to copy plist file."
  505. exit 4
  506. fi
  507. launchctl unload /Library/LaunchDaemons/com.github.netdata.plist 2>/dev/null
  508. if ! launchctl load /Library/LaunchDaemons/com.github.netdata.plist; then
  509. error "Failed to load plist file."
  510. exit 4
  511. fi
  512. }
  513. darwin_cmds() {
  514. NETDATA_START_CMD='launchctl start com.github.netdata'
  515. NETDATA_STOP_CMD='launchctl stop com.github.netdata'
  516. }
  517. # =====================================================================
  518. # Linux support functions
  519. detect_linux_svc_type() {
  520. if [ "${SVC_TYPE}" = "detect" ]; then
  521. found_types=''
  522. for t in wsl ${LINUX_INIT_TYPES}; do
  523. case "$("check_${t}")" in
  524. YES)
  525. SVC_TYPE="${t}"
  526. break
  527. ;;
  528. NO) continue ;;
  529. OFFLINE)
  530. if [ -z "${found_types}" ]; then
  531. found_types="${t}"
  532. else
  533. found_types="${found_types} ${t}"
  534. fi
  535. ;;
  536. esac
  537. done
  538. if [ "${SVC_TYPE}" = "detect" ]; then
  539. if [ -z "${found_types}" ]; then
  540. error "Failed to detect what type of service manager is in use."
  541. else
  542. SVC_TYPE="$(echo "${found_types}" | cut -f 1 -d ' ')"
  543. warning "Failed to detect a running service manager, using detected (but not running) ${SVC_TYPE}."
  544. fi
  545. elif [ "${SVC_TYPE}" = "wsl" ]; then
  546. if [ "$(check_systemd)" = "YES" ]; then
  547. # Support for systemd in WSL.
  548. SVC_TYPE="systemd"
  549. elif [ "$(_check_lsb_ignore_systemd)" = "YES" ]; then
  550. # Support for LSB init.d in WSL.
  551. SVC_TYPE="lsb"
  552. elif [ "$(_check_initd_ignore_systemd)" = "YES" ]; then
  553. # Support for ‘generic’ init.d in WSL.
  554. SVC_TYPE="initd"
  555. fi
  556. fi
  557. fi
  558. echo "${SVC_TYPE}"
  559. }
  560. install_linux_service() {
  561. t="$(detect_linux_svc_type)"
  562. if [ -z "${t}" ] || [ "${t}" = 'detect' ]; then
  563. exit 2
  564. fi
  565. "install_${t}_service"
  566. }
  567. linux_cmds() {
  568. t="$(detect_linux_svc_type)"
  569. if [ -z "${t}" ] || [ "${t}" = 'detect' ]; then
  570. exit 2
  571. fi
  572. "${t}_cmds"
  573. }
  574. # =====================================================================
  575. # Service type display function
  576. show_service_type() {
  577. info "Detected platform: ${PLATFORM}"
  578. case "${PLATFORM}" in
  579. FreeBSD)
  580. info "Detected service managers:"
  581. info " - freebsd: YES"
  582. info "Would use freebsd service management."
  583. ;;
  584. Darwin)
  585. info "Detected service managers:"
  586. info " - launchd: YES"
  587. info "Would use launchd service management."
  588. ;;
  589. Linux)
  590. [ "$(check_wsl)" = "YES" ] && info "Detected WSL environment."
  591. info "Detected service managers:"
  592. for t in ${LINUX_INIT_TYPES}; do
  593. info " - ${t}: $("check_${t}")"
  594. done
  595. info "Would use $(detect_linux_svc_type) service management."
  596. ;;
  597. *)
  598. info "${PLATFORM} is not supported by this script. No service file would be installed."
  599. esac
  600. exit 0
  601. }
  602. # =====================================================================
  603. # Argument handling
  604. parse_args() {
  605. while [ -n "${1}" ]; do
  606. case "${1}" in
  607. "--source" | "-s")
  608. SVC_SOURCE="${2}"
  609. shift 1
  610. ;;
  611. "--type" | "-t")
  612. if [ "${2}" = "help" ]; then
  613. help_types
  614. exit 0
  615. else
  616. SVC_TYPE="${2}"
  617. shift 1
  618. fi
  619. ;;
  620. "--show-type") SHOW_SVC_TYPE=1 ; INSTALL=0 ;;
  621. "--save-cmds")
  622. if [ -z "${2}" ]; then
  623. info "No path specified to save command variables."
  624. exit 1
  625. else
  626. SAVE_CMDS_PATH="${2}"
  627. shift 1
  628. fi
  629. ;;
  630. "--cmds" | "-c") DUMP_CMDS=1 ;;
  631. "--cmds-only") INSTALL=0 ;;
  632. "--export-cmds") EXPORT_CMDS=1 ;;
  633. "--enable" | "-e") ENABLE="enable" ;;
  634. "--disable" | "-d") ENABLE="disable" ;;
  635. "--help" | "-h")
  636. usage
  637. exit 0
  638. ;;
  639. *)
  640. info "Unrecognized option '${1}'."
  641. exit 1
  642. ;;
  643. esac
  644. shift 1
  645. done
  646. if [ "${SVC_SOURCE#@}" = "libsysdir_POST@" ]; then
  647. SVC_SOURCE="$(dirname "${SCRIPT_SOURCE}")/../../lib/netdata/system"
  648. warning "SVC_SOURCE not templated, using ${SVC_SOURCE} as source directory."
  649. fi
  650. if [ ! -d "${SVC_SOURCE}" ] && [ "${INSTALL}" -eq 1 ]; then
  651. error "${SVC_SOURCE} does not appear to be a directory. Please specify a valid source for service files with the --source option."
  652. exit 1
  653. fi
  654. if valid_types | grep -vw "${SVC_TYPE}"; then
  655. error "${SVC_TYPE} is not supported on this platform."
  656. help_types
  657. exit 1
  658. fi
  659. }
  660. # =====================================================================
  661. # Core logic
  662. main() {
  663. trap "cleanup" EXIT
  664. parse_args "${@}"
  665. if [ "${SHOW_SVC_TYPE}" -eq 1 ]; then
  666. show_service_type
  667. else
  668. case "${PLATFORM}" in
  669. FreeBSD)
  670. [ "${INSTALL}" -eq 1 ] && install_freebsd_service
  671. freebsd_cmds
  672. ;;
  673. Darwin)
  674. [ "${INSTALL}" -eq 1 ] && install_darwin_service
  675. darwin_cmds
  676. ;;
  677. Linux)
  678. [ "${INSTALL}" -eq 1 ] && install_linux_service
  679. linux_cmds
  680. ;;
  681. *)
  682. error "${PLATFORM} is not supported by this script."
  683. exit 5
  684. ;;
  685. esac
  686. [ "${DUMP_CMDS}" -eq 1 ] && dump_cmds
  687. [ "${EXPORT_CMDS}" -eq 1 ] && export_cmds
  688. [ -n "${SAVE_CMDS_PATH}" ] && save_cmds
  689. fi
  690. exit 0
  691. }
  692. main "${@}"