2017-01-31 19:53:05 +00:00
#!/bin/sh
2008-12-08 06:42:56 +00:00
### BEGIN INIT INFO
2017-01-31 19:53:05 +00:00
# Provides: clickhouse-server
2016-05-31 02:40:03 +00:00
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
2021-09-11 20:00:07 +00:00
# Should-Start: $time $network
# Should-Stop: $network
2022-01-18 16:34:56 +00:00
# Short-Description: clickhouse-server daemon
2008-12-08 06:42:56 +00:00
### END INIT INFO
2021-09-11 20:00:07 +00:00
#
# NOTES:
# - Should-* -- script can start if the listed facilities are missing, unlike Required-*
#
# For the documentation [1]:
#
# [1]: https://wiki.debian.org/LSBInitScripts
2008-12-08 06:42:56 +00:00
2017-01-31 19:53:05 +00:00
CLICKHOUSE_USER=clickhouse
CLICKHOUSE_GROUP=${CLICKHOUSE_USER}
2014-12-11 16:55:25 +00:00
SHELL=/bin/bash
2017-01-31 19:53:05 +00:00
PROGRAM=clickhouse-server
2019-01-31 13:03:17 +00:00
CLICKHOUSE_GENERIC_PROGRAM=clickhouse
2018-11-19 22:07:34 +00:00
CLICKHOUSE_PROGRAM_ENV=""
2019-01-31 13:03:17 +00:00
EXTRACT_FROM_CONFIG=${CLICKHOUSE_GENERIC_PROGRAM}-extract-from-config
CLICKHOUSE_CONFDIR=/etc/$PROGRAM
2017-02-03 13:52:59 +00:00
CLICKHOUSE_LOGDIR=/var/log/clickhouse-server
2017-01-31 19:53:05 +00:00
CLICKHOUSE_LOGDIR_USER=root
2019-01-31 13:03:17 +00:00
CLICKHOUSE_DATADIR=/var/lib/clickhouse
2020-01-09 12:59:45 +00:00
if [ -d "/var/lock" ]; then
LOCALSTATEDIR=/var/lock
else
2020-01-10 10:19:59 +00:00
LOCALSTATEDIR=/run/lock
2020-01-09 12:59:45 +00:00
fi
2020-01-10 10:19:59 +00:00
if [ ! -d "$LOCALSTATEDIR" ]; then
mkdir -p "$LOCALSTATEDIR"
fi
2019-01-31 13:03:17 +00:00
CLICKHOUSE_BINDIR=/usr/bin
2017-01-31 19:53:05 +00:00
CLICKHOUSE_CRONFILE=/etc/cron.d/clickhouse-server
2019-01-31 13:03:17 +00:00
CLICKHOUSE_CONFIG=$CLICKHOUSE_CONFDIR/config.xml
2008-12-08 06:42:56 +00:00
LOCKFILE=$LOCALSTATEDIR/$PROGRAM
2017-01-31 19:53:05 +00:00
CLICKHOUSE_PIDDIR=/var/run/$PROGRAM
CLICKHOUSE_PIDFILE="$CLICKHOUSE_PIDDIR/$PROGRAM.pid"
2019-06-04 13:03:22 +00:00
# CLICKHOUSE_STOP_TIMEOUT=60 # Disabled by default. Place to /etc/default/clickhouse if you need.
2014-12-09 12:44:50 +00:00
2018-05-16 00:34:56 +00:00
# Some systems lack "flock"
command -v flock >/dev/null && FLOCK=flock
2017-01-31 19:53:05 +00:00
# Override defaults from optional config file
test -f /etc/default/clickhouse && . /etc/default/clickhouse
2014-12-09 12:44:50 +00:00
2017-02-26 18:15:48 +00:00
2017-03-26 03:29:34 +00:00
die()
{
2017-07-08 16:09:32 +00:00
echo $1 >&2
exit 1
2017-03-26 03:29:34 +00:00
}
# Check that configuration file is Ok.
check_config()
{
2019-01-31 13:03:17 +00:00
if [ -x "$CLICKHOUSE_BINDIR/$EXTRACT_FROM_CONFIG" ]; then
su -s $SHELL ${CLICKHOUSE_USER} -c "$CLICKHOUSE_BINDIR/$EXTRACT_FROM_CONFIG --config-file=\"$CLICKHOUSE_CONFIG\" --key=path" >/dev/null || die "Configuration file ${CLICKHOUSE_CONFIG} doesn't parse successfully. Won't restart server. You may use forcerestart if you are sure.";
2017-07-08 16:09:32 +00:00
fi
2017-03-26 03:29:34 +00:00
}
2017-01-31 19:53:05 +00:00
initdb()
{
2020-11-26 05:13:45 +00:00
${CLICKHOUSE_GENERIC_PROGRAM} install --user "${CLICKHOUSE_USER}" --pid-path "${CLICKHOUSE_PIDDIR}" --config-path "${CLICKHOUSE_CONFDIR}" --binary-path "${CLICKHOUSE_BINDIR}"
2017-01-31 19:53:05 +00:00
}
2017-02-26 18:15:48 +00:00
2008-12-08 06:42:56 +00:00
start()
{
2020-08-27 18:50:13 +00:00
${CLICKHOUSE_GENERIC_PROGRAM} start --user "${CLICKHOUSE_USER}" --pid-path "${CLICKHOUSE_PIDDIR}" --config-path "${CLICKHOUSE_CONFDIR}" --binary-path "${CLICKHOUSE_BINDIR}"
2008-12-08 06:42:56 +00:00
}
2017-02-26 18:15:48 +00:00
2008-12-08 06:42:56 +00:00
stop()
{
2020-08-26 19:53:49 +00:00
${CLICKHOUSE_GENERIC_PROGRAM} stop --pid-path "${CLICKHOUSE_PIDDIR}"
2008-12-08 06:42:56 +00:00
}
2017-02-26 18:15:48 +00:00
2008-12-08 06:42:56 +00:00
restart()
{
2020-08-27 18:50:13 +00:00
${CLICKHOUSE_GENERIC_PROGRAM} restart --user "${CLICKHOUSE_USER}" --pid-path "${CLICKHOUSE_PIDDIR}" --config-path "${CLICKHOUSE_CONFDIR}" --binary-path "${CLICKHOUSE_BINDIR}"
2008-12-08 06:42:56 +00:00
}
2017-02-26 18:15:48 +00:00
2010-03-09 19:01:07 +00:00
forcestop()
{
2020-11-26 04:54:18 +00:00
${CLICKHOUSE_GENERIC_PROGRAM} stop --force --pid-path "${CLICKHOUSE_PIDDIR}"
2010-03-09 19:01:07 +00:00
}
2017-02-26 18:15:48 +00:00
2019-05-06 13:24:22 +00:00
service_or_func()
{
if [ -x "/bin/systemctl" ] && [ -f /etc/systemd/system/clickhouse-server.service ] && [ -d /run/systemd/system ]; then
2021-07-02 20:02:27 +00:00
systemctl $1 $PROGRAM
2019-05-06 13:24:22 +00:00
else
$1
fi
}
2010-03-09 19:01:07 +00:00
forcerestart()
{
2017-07-08 16:09:32 +00:00
forcestop
2019-05-06 12:12:18 +00:00
# Should not use 'start' function if systemd active
2019-05-06 13:24:22 +00:00
service_or_func start
2010-03-09 19:01:07 +00:00
}
2018-03-01 20:16:03 +00:00
use_cron()
{
# 1. running systemd
if [ -x "/bin/systemctl" ] && [ -f /etc/systemd/system/clickhouse-server.service ] && [ -d /run/systemd/system ]; then
return 1
fi
# 2. disabled by config
if [ -z "$CLICKHOUSE_CRONFILE" ]; then
return 2
fi
return 0
}
2020-09-03 21:53:21 +00:00
# returns false if cron disabled (with systemd)
2009-04-03 14:48:11 +00:00
enable_cron()
{
2018-03-01 20:16:03 +00:00
use_cron && sed -i 's/^#*//' "$CLICKHOUSE_CRONFILE"
2009-04-03 14:48:11 +00:00
}
2020-09-03 21:53:21 +00:00
# returns false if cron disabled (with systemd)
2009-04-03 14:48:11 +00:00
disable_cron()
{
2018-03-01 20:16:03 +00:00
use_cron && sed -i 's/^#*/#/' "$CLICKHOUSE_CRONFILE"
2009-04-03 14:48:11 +00:00
}
2017-02-26 18:15:48 +00:00
2014-12-29 17:31:30 +00:00
is_cron_disabled()
{
2018-03-01 20:16:03 +00:00
use_cron || return 0
2017-02-26 17:59:42 +00:00
2017-07-08 16:09:32 +00:00
# Assumes that either no lines are commented or all lines are commented.
# Also please note, that currently cron file for ClickHouse has only one line (but some time ago there was more).
grep -q -E '^#' "$CLICKHOUSE_CRONFILE";
2014-12-29 17:31:30 +00:00
}
2017-02-26 18:15:48 +00:00
2014-12-29 17:31:30 +00:00
main()
{
2017-07-08 16:09:32 +00:00
# See how we were called.
EXIT_STATUS=0
case "$1" in
start)
2020-09-03 21:53:21 +00:00
service_or_func start && enable_cron
2017-07-08 16:09:32 +00:00
;;
stop)
2019-06-04 13:03:22 +00:00
disable_cron
2020-09-03 21:53:21 +00:00
service_or_func stop
2017-07-08 16:09:32 +00:00
;;
restart)
2020-09-03 21:53:21 +00:00
service_or_func restart && enable_cron
2017-07-08 16:09:32 +00:00
;;
forcestop)
2019-05-06 12:12:18 +00:00
disable_cron
forcestop
2017-07-08 16:09:32 +00:00
;;
forcerestart)
forcerestart && enable_cron
;;
reload)
2020-09-03 21:53:21 +00:00
service_or_func restart
2017-07-08 16:09:32 +00:00
;;
condstart)
2020-11-26 04:54:18 +00:00
service_or_func start
2017-07-08 16:09:32 +00:00
;;
condstop)
2020-11-26 04:54:18 +00:00
service_or_func stop
2017-07-08 16:09:32 +00:00
;;
condrestart)
2020-11-26 04:54:18 +00:00
service_or_func restart
2017-07-08 16:09:32 +00:00
;;
condreload)
2020-11-26 04:54:18 +00:00
service_or_func restart
2017-07-08 16:09:32 +00:00
;;
initdb)
initdb
;;
2018-02-01 17:55:08 +00:00
enable_cron)
enable_cron
;;
disable_cron)
disable_cron
;;
2017-07-08 16:09:32 +00:00
*)
2020-09-03 21:43:15 +00:00
echo "Usage: $0 {start|stop|status|restart|forcestop|forcerestart|reload|condstart|condstop|condrestart|condreload|initdb}"
2017-07-08 16:09:32 +00:00
exit 2
;;
esac
exit $EXIT_STATUS
2014-12-29 17:31:30 +00:00
}
2008-12-08 06:42:56 +00:00
2017-02-26 18:15:48 +00:00
2015-03-24 15:28:02 +00:00
status()
{
2020-11-26 04:54:18 +00:00
${CLICKHOUSE_GENERIC_PROGRAM} status --pid-path "${CLICKHOUSE_PIDDIR}"
2015-03-24 15:28:02 +00:00
}
2017-02-26 18:15:48 +00:00
2017-02-15 23:43:52 +00:00
# Running commands without need of locking
case "$1" in
status)
2017-07-08 16:09:32 +00:00
status
2021-06-07 12:46:49 +00:00
exit 0
2017-07-08 16:09:32 +00:00
;;
2017-02-15 23:43:52 +00:00
esac
2015-03-25 11:36:46 +00:00
2017-02-26 18:15:48 +00:00
2014-12-29 17:31:30 +00:00
(
2018-05-16 00:34:56 +00:00
if $FLOCK -n 9; then
2017-07-08 16:09:32 +00:00
main "$@"
else
echo "Init script is already running" && exit 1
fi
2017-02-26 18:53:24 +00:00
) 9> $LOCKFILE