停止时系统服务失败

时间:2019-01-19 20:43:43

标签: systemd

我停止服务/usr/lib/systemd/system/zookeeper.service失败。

[vagrant@localhost ~]$ sudo systemctl stop zookeeper
[vagrant@localhost ~]$ sudo systemctl status zookeeper
● zookeeper.service - Zookeeper Distributed Coordination Server
   Loaded: loaded (/usr/lib/systemd/system/zookeeper.service; enabled; vendor preset: disabled)
   Active: failed (Result: signal) since Sat 2019-01-19 20:36:50 UTC; 1s ago
     Docs: http://zookeeper.apache.org
  Process: 10488 ExecStop=/opt/zookeeper/bin/zkServer.sh stop (code=exited, status=0/SUCCESS)
  Process: 10439 ExecStart=/opt/zookeeper/bin/zkServer.sh start (code=exited, status=0/SUCCESS)
 Main PID: 10448 (code=killed, signal=KILL)

Jan 19 20:36:42 localhost.localdomain zkServer.sh[10439]: ZooKeeper JMX enabled by default
Jan 19 20:36:42 localhost.localdomain zkServer.sh[10439]: Using config: /opt/zookeeper/bin/../conf/zoo.cfg
Jan 19 20:36:43 localhost.localdomain systemd[1]: Started Zookeeper Distributed Coordination Server.
Jan 19 20:36:50 localhost.localdomain systemd[1]: Stopping Zookeeper Distributed Coordination Server...
Jan 19 20:36:50 localhost.localdomain zkServer.sh[10488]: ZooKeeper JMX enabled by default
Jan 19 20:36:50 localhost.localdomain zkServer.sh[10488]: Using config: /opt/zookeeper/bin/../conf/zoo.cfg
Jan 19 20:36:50 localhost.localdomain systemd[1]: zookeeper.service: main process exited, code=killed, status=9/KILL
Jan 19 20:36:50 localhost.localdomain systemd[1]: Stopped Zookeeper Distributed Coordination Server.
Jan 19 20:36:50 localhost.localdomain systemd[1]: Unit zookeeper.service entered failed state.
Jan 19 20:36:50 localhost.localdomain systemd[1]: zookeeper.service failed.

完整的服务代码:

[Unit]
Description=Zookeeper Distributed Coordination Server
Documentation=http://zookeeper.apache.org
After=network.target
Wants=network.target

[Service]
Type=forking
User=zookeeper
Group=zookeeper
WorkingDirectory=/opt/zookeeper
Environment=ZOO_LOG_DIR=/var/log/zookeeper
ExecStart=/opt/zookeeper/bin/zkServer.sh start
ExecStop=/opt/zookeeper/bin/zkServer.sh stop
Restart=on-failure

[Install]
WantedBy=multi-user.target

启动服务后,一切正常:

[vagrant@localhost ~]$ sudo systemctl start zookeeper
[vagrant@localhost ~]$ sudo systemctl status zookeeper
● zookeeper.service - Zookeeper Distributed Coordination Server
   Loaded: loaded (/usr/lib/systemd/system/zookeeper.service; enabled; vendor preset: disabled)
   Active: active (running) since Sat 2019-01-19 20:38:29 UTC; 1s ago
     Docs: http://zookeeper.apache.org
  Process: 10488 ExecStop=/opt/zookeeper/bin/zkServer.sh stop (code=exited, status=0/SUCCESS)
  Process: 10590 ExecStart=/opt/zookeeper/bin/zkServer.sh start (code=exited, status=0/SUCCESS)
 Main PID: 10599 (java)
    Tasks: 20
   Memory: 35.1M
   CGroup: /system.slice/zookeeper.service
           └─10599 java -Dzookeeper.log.dir=/var/log/zookeeper -Dzookeeper.root.logger=INFO,CONSOLE -cp /opt/zookeeper/bin/../build/classes:/opt/zookeeper/bin/../build/lib/*.jar:/opt/zookeeper/bin/../l...

Jan 19 20:38:28 localhost.localdomain systemd[1]: Starting Zookeeper Distributed Coordination Server...
Jan 19 20:38:28 localhost.localdomain zkServer.sh[10590]: ZooKeeper JMX enabled by default
Jan 19 20:38:28 localhost.localdomain zkServer.sh[10590]: Using config: /opt/zookeeper/bin/../conf/zoo.cfg
Jan 19 20:38:29 localhost.localdomain systemd[1]: Started Zookeeper Distributed Coordination Server.

2 个答案:

答案 0 :(得分:0)

ExecStop更改为使用/bin/kill,然后将PIDFile指向.pid文件:

[Unit]
Description=Zookeeper Distributed Coordination Server
Documentation=http://zookeeper.apache.org
After=network.target

[Service]
Type=forking
User=zookeeper
Group=zookeeper
WorkingDirectory=/opt/zookeeper
Environment=ZOO_LOG_DIR=/var/log/zookeeper
ExecStart=/opt/zookeeper/bin/zkServer.sh start
PIDFile=/var/lib/zookeeper/zookeeper_server.pid
ExecStop=/bin/kill $MAINPID
SuccessExitStatus=1 143
Restart=on-failure

[Install]
WantedBy=multi-user.target

不要忘记将SuccessExitStatus更改为代码1 143

答案 1 :(得分:0)

对我来说最有用的是:

ExecStop=/bin/kill -s SIGKILL $MAINPID
SuccessExitStatus=SIGKILL