When working with ClickHouse, It is important to understand the different methods available to start and stop the ClickHouse server. It would be really helpful to debug the issues when your ClickHouse server is not getting UP. In this article, I will explain the following topics to better understand this subject.
- Start ClickHouse using the “clickhouse” program
- Start ClickHouse using init startup scripts
- Start ClickHouse using “Service” and “Systemctl” commands
- How to enable/disable on boot time?
Start ClickHouse using the “clickhouse” program
The “clickhouse” program is used to perform the ClickHouse server start/stop/install/restart and other operations. This program will be installed automatically while installing the ClickHouse server. It will be automatically configured under “/usr/bin”
root@ClickHouse:~# find ~ /usr/bin -iname clickhouse /usr/bin/clickhouse
The program itself has the start/stop/status/restart arguments. We just need to run the program with the arguments as shown below. It will do the action based on the default configurations.
root@ClickHouse:~# clickhouse status /var/run/clickhouse-server/clickhouse-server.pid file exists and contains pid = 11395. The process with pid = 11395 is running. root@ClickHouse:~# clickhouse stop /var/run/clickhouse-server/clickhouse-server.pid file exists and contains pid = 11395. The process with pid = 11395 is running. Sent terminate signal to process with pid 11395. Waiting for server to stop Now there is no clickhouse-server process. Server stopped root@ClickHouse:~# clickhouse start chown -R clickhouse: '/var/run/clickhouse-server/' Will run clickhouse su 'clickhouse' /usr/bin/clickhouse-server --config-file /etc/clickhouse-server/config.xml --pid-file /var/run/clickhouse-server/clickhouse-server.pid --daemon Waiting for server to start Server started
We can also manually start the ClickHouse server by passing the options as shown below.
/usr/bin/clickhouse start --config=/etc/clickhouse-server/config.xml /usr/bin/clickhouse-server --config=/etc/clickhouse-server/config.xml
The other methods will have their own commands and files. But still, at the bottom, they will call the program “clickhouse / clickhouse-server” to perform the task.
Start ClickHouse using init startup scripts
These scripts are written in shell language. The files are located in the directory “/etc/init.d/”. These scripts are used to perform start/ stop/ restart/reload operations. For example,
root@ClickHouse:~# /etc/init.d/clickhouse-server status /var/run/clickhouse-server/clickhouse-server.pid file exists and contains pid = 13681. The process with pid = 13681 is running. root@ClickHouse:~# /etc/init.d/clickhouse-server stop The process with pid = 13681 is running. Waiting for server to stop Now there is no clickhouse-server process. Server stopped root@ClickHouse:~# /etc/init.d/clickhouse-server start chown -R clickhouse: '/var/run/clickhouse-server/' Will run clickhouse su 'clickhouse' /usr/bin/clickhouse-server --config-file /etc/clickhouse-server/config.xml --pid-file /var/run/clickhouse-server/clickhouse-server.pid --daemon Waiting for server to start Server started
Let’s take a brief look at the script. We have the following variables declared in the script. These variables will be used in the commands.
##cat /etc/init.d/clickhouse-server #!/bin/sh 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
The commands in the scripts are,
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}" }
The relevant function will be called when we call the “/etc/init.d/clickhouse-server” script with an argument. For example, when I run the command “/etc/init.d/clickhouse-server start” the following function will be called.
start() { ${CLICKHOUSE_GENERIC_PROGRAM} start --user "${CLICKHOUSE_USER}" --pid-path "${CLICKHOUSE_PIDDIR}" --config-path "${CLICKHOUSE_CONFDIR}" --binary-path "${CLICKHOUSE_BINDIR}" }
Start ClickHouse using “Service” and “Systemctl” commands
We can use the “service” and “systemctl” commands to start/stop the ClickHouse server. Personally, I would prefer to use the “systemctl” as it has some advantages compared with “service” commands.
When executing service/systemctl commands, it will find the relevant service file located under the systemd folder. The service file will be automatically created during the ClickHouse installation. The service file will have the service configurations. Those configurations will be used to perform the start/stop task.
root@ClickHouse:~# cd /usr/lib/systemd/system root@ClickHouse:/usr/lib/systemd/system# ls -lrth | grep clickhouse -rw-rw-r-- 1 root root 971 Aug 27 06:57 clickhouse-server.service
The service files can be located in different locations for various purposes.
- /etc/systemd/system – This location can be used to create the service files manually.
- /run/systemd/system – Used for runtime units.
- /usr/lib/systemd/system (or) /lib/systemd/system – This location will have the service files during the installation
Note: Make sure that you use all the prefix words before “.service” in the command line. For example, If my service file name is “xxxxx.service” then, I need to use a command like “systemctl xxxxx start”. otherwise, you will get the error ( No such file or directory ).
For example,
using the “service” command,
root@ClickHouse:~# service clickhouse-server start root@ClickHouse:~# root@ClickHouse:~# service clickhouse-server status | grep Active Active: active (running) since Fri 2022-09-02 11:20:41 IST; 4s ago root@ClickHouse:~# root@ClickHouse:~# service clickhouse-server stop root@ClickHouse:~# root@ClickHouse:~# service clickhouse-server status | grep Active Active: inactive (dead) since Fri 2022-09-02 11:20:54 IST; 2s ago
using the “systemctl” command,
root@ClickHouse:~# systemctl start clickhouse-server root@ClickHouse:~# root@ClickHouse:~# systemctl status clickhouse-server | grep Active Active: active (running) since Fri 2022-09-02 11:22:31 IST; 11s ago root@ClickHouse:~# root@ClickHouse:~# systemctl stop clickhouse-server root@ClickHouse:~# root@ClickHouse:~# systemctl status clickhouse-server | grep Active Active: inactive (dead) since Fri 2022-09-02 11:22:51 IST; 2s ago
Let us take a look at the service file,
### cat /usr/lib/systemd/system/clickhouse-server.service [Unit] Description=ClickHouse Server (analytic DBMS for big data) Requires=network-online.target After=time-sync.target network-online.target Wants=time-sync.target [Service] Type=simple User=clickhouse Group=clickhouse Restart=always RestartSec=30 RuntimeDirectory=clickhouse-server ExecStart=/usr/bin/clickhouse-server --config=/etc/clickhouse-server/config.xml --pid-file=/run/clickhouse-server/clickhouse-server.pid # Minus means that this file is optional. EnvironmentFile=-/etc/default/clickhouse LimitCORE=infinity LimitNOFILE=500000 CapabilityBoundingSet=CAP_NET_ADMIN CAP_IPC_LOCK CAP_SYS_NICE CAP_NET_BIND_SERVICE [Install] WantedBy=multi-user.target
The service file has a group ( [service] ), with all the necessary variable declarations and the command to start the ClickHouse.
RuntimeDirectory=clickhouse-server ExecStart=/usr/bin/clickhouse-server --config=/etc/clickhouse-server/config.xml --pid-file=/run/clickhouse-server/clickhouse-server.pid
To stop the service, If ExecStop is not specified, by default, SIGTERM will be used.
- SIGTERM – Signal Termination.
This means It will wait for a response from the process. If it fails to stop within a deadline, the system sends a SIGKILL which simply forcefully ends the process.
How to enable/disable on boot time?
There are different options to do this task. You can easily manage this with Systemd commands. By default, the ClickHouse service is enabled on boot time. This means if you restart the Linux server when the server is Up, the ClickHouse service will also be started automatically. You can manually enable and check the current configuration using the following commands.
root@ClickHouse:~# systemctl enable clickhouse-server Synchronizing state of clickhouse-server.service with SysV service script with /lib/systemd/systemd-sysv-install. Executing: /lib/systemd/systemd-sysv-install enable clickhouse-server Created symlink /etc/systemd/system/multi-user.target.wants/clickhouse-server.service → /lib/systemd/system/clickhouse-server.service. root@ClickHouse:~# systemctl is-enabled clickhouse-server enabled
In some reason, if you would like to disable this, you can use the following command.
root@ClickHouse:~# systemctl disable clickhouse-server Synchronizing state of clickhouse-server.service with SysV service script with /lib/systemd/systemd-sysv-install. Executing: /lib/systemd/systemd-sysv-install disable clickhouse-server Removed /etc/systemd/system/multi-user.target.wants/clickhouse-server.service. root@ClickHouse:~# systemctl is-enabled clickhouse-server disabled
All set! Hopefully, this article helps to understand the different methods available to start/stop/restart the ClickHouse service.