Ubuntu – How to start a Zookeeper daemon after booting under specific user in Ubuntu server 16.04

bashcommand lineserverstartup

I want to start Zookeeper daemon after Ubuntu server 16.04 booting (not after logging) under user named zookeeper. So I changed the file /etc/rc.local like the following:

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

echo 'never'; defrag_file_pathname

su -c '$ZOOKEEPER_HOME/bin/zkServer.sh start' zookeeper &

exit 0

, adding the line su -c '$ZOOKEEPER_HOME/bin/zkServer.sh start' zookeeper & before exit 0. But the process is not started after restarting!

What's wrong here?

details: The zookeeper user is in sudo group and has password.

details: When I run command su -c '$ZOOKEEPER_HOME/bin/zkServer.sh start' zookeeper & in terminal, it needs password to run.

Best Answer

Create a .service file in /etc/systemd/system/zoo.service and add the following lines:

[Unit]
Description=Zookeeper Daemon
Wants=syslog.target

[Service]    
Type=forking
WorkingDirectory=/path/to/dir/of/interest
User=zookeeper 
ExecStart=/home/zookeeper_home/bin/zkServer.sh
TimeoutSec=30
Restart=on-failure

[Install]
WantedBy=multi-user.target

Now setup the service:

sudo systemctl start zoo
sudo systemctl enable zoo

Check status:

sudo systemctl status zoo

Please read for further details creating daemons:

https://www.freedesktop.org/software/systemd/man/systemd.unit.html