how to run kafka as a service on linux
# Change directory into system folder holding services
cd /etc/systemd/system
# Create your Zookeeper service as root and enter Zookeeper config.
# In Nano: Ctrl-O and enter to write, Ctrl-X and enter to Quit & Save.
sudo nano zookeeper.service
#paste the code below to run in the zookeeper.service file,
#don't forget to change the dir path on the ExecStart & ExecStop lines
[Unit]
Description=Bootstrapped Zookeeper
After=syslog.target network.target
[Service]
Type=simple
User=root
Group=root
ExecStart=/dir/to/kafka/bin/zookeeper-server-start.sh /dir/to/kafka/config/zookeeper.properties
ExecStop=/dir/to/kafka/bin/zookeeper-server-stop.sh
[Install]
WantedBy=multi-user.target
# Create your Kafka Broker service as root and enter Kafka Broker config.
sudo nano kafka.service
#paste the code below to run in the kafka.service file
#don't forget to change the dir path on the ExecStart & ExecStop lines
[Unit]
Description=Apache Kafka
Requires=zookeeper.service
After=zookeeper.service
[Service]
Type=simple
User=root
Group=root
ExecStart=/dir/to/kafka/bin/kafka-server-start.sh /dir/to/kafka/config/server.properties
ExecStop=/dir/to/kafka/bin/kafka-server-stop.sh
[Install]
WantedBy=multi-user.target
# Reload Systemd Manager Configuration
sudo systemctl daemon-reload
# Enable your new services to run when incase the server reboots
#you can run the following code individually
sudo systemctl enable zookeeper.service
sudo systemctl enable kafka.service
#Start zookeeper & kafka services
sudo systemctl start zookeeper.service
sudo systemctl start kafka.service
#Check status of zookeeper & kafka services
sudo systemctl status zookeeper.service
sudo systemctl status kafka.service
# Stop zookeeper & kafka Services
sudo systemctl stop zookeeper.service
sudo systemctl stop kafka.service
#check logs of zookeeper & kafka services
journalctl -u zookeeper.service -f
journalctl -u kafka.service -f
#ensure zookeeper is started before kafka