ClickHouse (2)启动脚本解读

使用RPM包安装的时候,会自带一个启动脚本,安装在 /etc/init.d/clickhouse-server ,内容如下

#!/bin/sh
### BEGIN INIT INFO # 这一段的说明请查阅 checkconfig
# Provides:          clickhouse-server
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Should-Start:      $time $network
# Should-Stop:       $network
# Short-Description: clickhouse-server daemon ClickHouse后台启动脚本
### END INIT INFO
#
# NOTES:
# - Should-* -- script can start if the listed facilities are missing, unlike Required-*
#
# For the documentation [1]:
#
#   [1]: https://wiki.debian.org/LSBInitScripts

CLICKHOUSE_USER=clickhouse # 启动的用户
CLICKHOUSE_GROUP=${CLICKHOUSE_USER} # 启动的用户组
SHELL=/bin/bash
PROGRAM=clickhouse-server # 程序名
CLICKHOUSE_GENERIC_PROGRAM=clickhouse
CLICKHOUSE_PROGRAM_ENV="" # 环境变量
EXTRACT_FROM_CONFIG=${CLICKHOUSE_GENERIC_PROGRAM}-extract-from-config # 扩展配置
CLICKHOUSE_CONFDIR=/etc/$PROGRAM # 配置文件目录
CLICKHOUSE_LOGDIR=/var/log/clickhouse-server # 日志目录
CLICKHOUSE_LOGDIR_USER=root # 日志用户
CLICKHOUSE_DATADIR=/var/lib/clickhouse # 数据目录

# Lock 文件目录
if [ -d "/var/lock" ]; then
    LOCALSTATEDIR=/var/lock
else
    LOCALSTATEDIR=/run/lock
fi
if [ ! -d "$LOCALSTATEDIR" ]; then
    mkdir -p "$LOCALSTATEDIR"
fi

CLICKHOUSE_BINDIR=/usr/bin # BIN目录
CLICKHOUSE_CRONFILE=/etc/cron.d/clickhouse-server 
CLICKHOUSE_CONFIG=$CLICKHOUSE_CONFDIR/config.xml # 配置文件
LOCKFILE=$LOCALSTATEDIR/$PROGRAM # LOCK文件
CLICKHOUSE_PIDDIR=/var/run/$PROGRAM # PID目录
CLICKHOUSE_PIDFILE="$CLICKHOUSE_PIDDIR/$PROGRAM.pid" # PID 文件

# Disabled by default. Place to /etc/default/clickhouse if you need.
# 停止超时时间,默认不设置,如果需要可以在 /etc/default/clickhouse中设置
# CLICKHOUSE_STOP_TIMEOUT=60 

# Some systems lack "flock" # 系统兼容处理
command -v flock >/dev/null && FLOCK=flock

# Override defaults from optional config file
# 覆盖可选配置中的默认值
test -f /etc/default/clickhouse && . /etc/default/clickhouse


die()
{
    echo $1 >&2
    exit 1
}


# Check that configuration file is Ok. # 检查配置文件
check_config()
{
    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.";
    fi
}


# 初始化数据
initdb()
{
    ${CLICKHOUSE_GENERIC_PROGRAM} install --user "${CLICKHOUSE_USER}" --pid-path "${CLICKHOUSE_PIDDIR}" --config-path "${CLICKHOUSE_CONFDIR}" --binary-path "${CLICKHOUSE_BINDIR}"
}


start()
{
    ${CLICKHOUSE_GENERIC_PROGRAM} start --user "${CLICKHOUSE_USER}" --pid-path "${CLICKHOUSE_PIDDIR}" --config-path "${CLICKHOUSE_CONFDIR}" --binary-path "${CLICKHOUSE_BINDIR}"
}


stop()
{
    ${CLICKHOUSE_GENERIC_PROGRAM} stop --pid-path "${CLICKHOUSE_PIDDIR}"
}


restart()
{
    ${CLICKHOUSE_GENERIC_PROGRAM} restart --user "${CLICKHOUSE_USER}" --pid-path "${CLICKHOUSE_PIDDIR}" --config-path "${CLICKHOUSE_CONFDIR}" --binary-path "${CLICKHOUSE_BINDIR}"
}


forcestop()
{
    ${CLICKHOUSE_GENERIC_PROGRAM} stop --force --pid-path "${CLICKHOUSE_PIDDIR}"
}


# 添加到Service中管理服务
service_or_func()
{
    if [ -x "/bin/systemctl" ] && [ -f /etc/systemd/system/clickhouse-server.service ] && [ -d /run/systemd/system ]; then
        systemctl $1 $PROGRAM
    else
        $1
    fi
}

forcerestart()
{
    forcestop
    # Should not use 'start' function if systemd active
    service_or_func start
}

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
}
# returns false if cron disabled (with systemd)
enable_cron()
{
    use_cron && sed -i 's/^#*//' "$CLICKHOUSE_CRONFILE"
}
# returns false if cron disabled (with systemd)
disable_cron()
{
    use_cron && sed -i 's/^#*/#/' "$CLICKHOUSE_CRONFILE"
}


is_cron_disabled()
{
    use_cron || return 0

    # 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";
}


main()
{
    # See how we were called.
    EXIT_STATUS=0
    case "$1" in
    start)
        service_or_func start && enable_cron
        ;;
    stop)
        disable_cron
        service_or_func stop
        ;;
    restart)
        service_or_func restart && enable_cron
        ;;
    forcestop)
        disable_cron
        forcestop
        ;;
    forcerestart)
        forcerestart && enable_cron
        ;;
    reload)
        service_or_func restart
        ;;
    condstart)
        service_or_func start
        ;;
    condstop)
        service_or_func stop
        ;;
    condrestart)
        service_or_func restart
        ;;
    condreload)
        service_or_func restart
        ;;
    initdb)
        initdb
        ;;
    enable_cron)
        enable_cron
        ;;
    disable_cron)
        disable_cron
        ;;
    *)
        echo "Usage: $0 {start|stop|status|restart|forcestop|forcerestart|reload|condstart|condstop|condrestart|condreload|initdb}"
        exit 2
        ;;
    esac

    exit $EXIT_STATUS
}


status()
{
    ${CLICKHOUSE_GENERIC_PROGRAM} status --pid-path "${CLICKHOUSE_PIDDIR}"
}


# Running commands without need of locking
case "$1" in
status)
    status
    exit 0
    ;;
esac


(
    if $FLOCK -n 9; then
        main "$@"
    else
        echo "Init script is already running" && exit 1
    fi
) 9> $LOCKFILE