Thursday, 20 October 2016

How to make Zookeeper run automatically on startup in Mac

Zookeeper automatic startup in Mac Tutorial

ZooKeeper is a centralized service for maintaining configuration information, naming, providing distributed synchronization, and providing group services. All of these kinds of services are used in some form or another by distributed applications.


In Mac you can either install zookeeper via home brew using below command
brew install zookeeper
which will install it in /usr/local/Cellar/zookeeper directory.
or you can manually download it and extract it in /usr/local/zookeeper/ directory.

Post successful installation if you are able to start zookeeper using below command
bin/zkServer.sh start

and verify it by doing a telnet to port 2181 as below
telnet localhost 2181

Then all you need is to add a plist file in order to have zookeeper running on startup and not manually running it everytime. Verified to be working in Mac-OS 10.12
1. Go to the zookeeper installation directory - usually /usr/local/zookeeper/zookeeper-3.4.6/
cd /usr/local/zookeeper/zookeeper-3.4.6/

2. Create a file named - homebrew.mxcl.zookeeper.plist
vi homebrew.mxcl.zookeeper.plist
or
sudo vi homebrew.mxcl.zookeeper.plist

3. Add below contents to the file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>KeepAlive</key>
    <dict>
      <key>SuccessfulExit</key>
      <false/>
    </dict>
    <key>Label</key>
    <string>homebrew.mxcl.zookeeper</string>
    <key>ProgramArguments</key>
    <array>
      <string>/usr/local/zookeeper/zookeeper-3.4.6/bin/zkServer.sh</string>
      <string>start-foreground</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>WorkingDirectory</key>
    <string>/usr/local/zookeeper/zookeeper-3.4.6</string>
    <key>StandardErrorPath</key>
    <string>/usr/local/zookeeper/zookeeper-3.4.6/zookeeper.err</string>
    <key>StandardOutPath</key>
    <string>/usr/local/zookeeper/zookeeper-3.4.6/zookeeper.out</string>
  </dict>
</plist> 

4. Create a symbolic link to this file in ~/Library/LaunchAgents
ln -sfv /usr/local/zookeeper/zookeeper-3.8.1/homebrew.mxcl.zookeeper.plist ~/Library/LaunchAgents/

5. Now just use launchctl to load this plist file using below command
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.zookeeper.plist

Now reboot your Mac and zookeeper should be running after startup :)

No comments :

Post a Comment