我想在 Ubuntu 服务器 16.04 启动后(而不是登录后)以用户名启动 Zookeeper 守护进程zookeeper。所以我将文件修改/etc/rc.local
如下:
#!/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
su -c '$ZOOKEEPER_HOME/bin/zkServer.sh start' zookeeper &
,在 之前添加这一行exit 0
。但是重启之后进程并没有启动!
这里有什么问题?
细节: 这zookeeper用户在须藤组并有密码。
细节:当我在终端中运行命令时su -c '$ZOOKEEPER_HOME/bin/zkServer.sh start' zookeeper &
,它需要密码才能运行。
答案1
创建一个 .service
文件/etc/systemd/system/zoo.service
并添加以下行:
[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
现在设置服务:
sudo systemctl start zoo
sudo systemctl enable zoo
检查状态:
sudo systemctl status zoo
请阅读有关创建守护进程的更多详细信息:
https://www.freedesktop.org/software/systemd/man/systemd.unit.html