# Start Kafka as a Linux service

In 
Published 2022-12-03

This tutorial explains to you how to start Kafka as a Linux service.

In order to start/ stop Kafka as a Linux service (on CentOS 8 in my case), you need to do the following as root:

-- Go to the /etc/systemd/system directory

cd /etc/systemd/system

-- Create the service file (named kafka.service in my case)

vim kafka.service

-- Add the following content into the file

[Unit]
Description=Kafka server
Requires=zookeeper.service

[Service]
User=kafka
WorkingDirectory=/kafka/kafka_2.13-2.8.0/bin/
ExecStart=/usr/bin/bash kafka-server-start.sh /kafka/kafka_2.13-2.8.0/config/server.properties
ExecStop=/usr/bin/bash kafka-server-stop.sh
SuccessExitStatus=143

[Install]
WantedBy=multi-user.target

As you can see in the following picture :

-- Change the following permissions:

chmod +x kafka.service
chown kafka:kafka kafka.service

-- Reload the service files to include the new service

systemctl daemon-reload

-- Finally to start the script on boot, enable the service with systemd (optional)

systemctl enable kafka.service

-- Test the start of the service (using kafka user)

systemctl start kafka

or

service kafka start

-- Test the status of the service (using kafka user)

systemctl status kafka

or

service kafka status

As you can see when you start the kafka service, zookeeper service is started automatically before kafka service.

-- Test the stop of the service (using kafka user)

systemctl stop zookeeper

or

service zookeeper stop

As you can see when you stop the kafka service, zookeeper service is still running.