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

Share:
8,631

Related videos on Youtube

Soheil Pourbafrani
Author by

Soheil Pourbafrani

Updated on September 18, 2022

Comments

  • Soheil Pourbafrani
    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 & 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.

    • wvxvw
      wvxvw over 6 years
      I 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
      George Udosen over 6 years
      Hi please explain this '$ZOOKEEPER_HOME/bin/zkServer.sh start' zookeeper line, is '$ZOOKEEPER_HOME/bin/zkServer.sh start' different from zookeeper or is it an argument pass in?
    • Soheil Pourbafrani
      Soheil Pourbafrani over 6 years
      The command file path is $ZOOKEEPER_HOME/bin/zkServer.sh with start argument, and the user I want to run this command with is zookeeper. I tried without using variable $ZOOKEEPER_HOME but no difference. Thanks!
    • Soheil Pourbafrani
      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
      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
      Soheil Pourbafrani over 6 years
      @GeorgeUdosen Could you post an answer to my question with more details, please.
  • Soheil Pourbafrani
    Soheil Pourbafrani over 6 years
    Hey your answer is ok but it runs my command under the user root but I want it to be run by a user named zookeeper, I have created a user named zookeeper in advance!
  • Soheil Pourbafrani
    Soheil Pourbafrani over 6 years
    You 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
    Soheil Pourbafrani over 6 years
    Thanks, 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 a zookeeper.out file on home directory. Now as Zookeeper start command is running before logging in, it tries to create zookeeper.out file on path / that has no permission and fails! So is it possible to set command location run it systemd.unit?
  • Soheil Pourbafrani
    Soheil Pourbafrani over 6 years