How to start a Zookeeper daemon after booting under specific user in Ubuntu server 16.04
8,631
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
Related videos on Youtube
Author by
Soheil Pourbafrani
Updated on September 18, 2022Comments
-
Soheil Pourbafrani over 1 year
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 &
beforeexit 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.-
wvxvw over 6 yearsI think Ubuntu 16 is a
systemd
system, so, maybe write a service description file instead? Here's an example btw: gist.github.com/gambtho/36a2f01e9e7d8c1b0046fb074f1a44ee (I don't think you need all the details though). -
George Udosen over 6 yearsHi please explain this
'$ZOOKEEPER_HOME/bin/zkServer.sh start' zookeeper
line, is'$ZOOKEEPER_HOME/bin/zkServer.sh start'
different fromzookeeper
or is it an argument pass in? -
Soheil Pourbafrani over 6 yearsThe command file path is
$ZOOKEEPER_HOME/bin/zkServer.sh
withstart
argument, and the user I want to run this command with iszookeeper
. I tried without using variable $ZOOKEEPER_HOME but no difference. Thanks! -
Soheil Pourbafrani over 6 years@GeorgeUdosen I want to run this command before logging in as user
zookeeper
. The post you suggest, running as a user try to run a command without password as another user. -
Soheil Pourbafrani over 6 years@GeorgeUdosen My question is the same as that question about running Spark with the difference that I want to run a script as a special user.
-
Soheil Pourbafrani over 6 years@GeorgeUdosen Could you post an answer to my question with more details, please.
-
-
Soheil Pourbafrani over 6 yearsHey your answer is ok but it runs my command under the user
root
but I want it to be run by a user namedzookeeper
, I have created a user namedzookeeper
in advance! -
Soheil Pourbafrani over 6 yearsYou know at the first line you said
To run an app with a different name that the source script
. I didn't want to run the app in a different name! I just to run it by a specific user at booting(before logging in) -
Soheil Pourbafrani over 6 yearsThanks, it solved. New problem is when zookeeper runs, it creates a
zookeeper.out
file on the directory the run command has been issued. For example, if I run it in home directory it creates azookeeper.out
file on home directory. Now as Zookeeper start command is running before logging in, it tries to createzookeeper.out
file on path/
that has no permission and fails! So is it possible to set command location run itsystemd.unit
? -
Soheil Pourbafrani over 6 yearsLet us continue this discussion in chat.