10

这是我的问题。我有 CentOS 和 java 进程在它上面运行。Java 进程由启动/停止脚本操作。它也创建了一个 java 实例的 .pid 文件。

我的单位文件看起来像:

[Unit]
After=syslog.target network.target
Description=Someservice

[Service]
User=xxxuser
Type=forking
WorkingDirectory=/srv/apps/someservice
ExecStart=/srv/apps/someservice/server.sh start
ExecStop=/srv/apps/someservice/server.sh stop
PIDFile=/srv/apps/someservice/application.pid
TimeoutStartSec=0

[Install]
WantedBy=multi-user.target

当我调用stop函数时,脚本终止 java 进程SIGTERM并返回 0 代码:

kill $OPT_FORCEKILL `cat $PID_FILE`
<...>
return 0

之后,如果我检查我的单位的状态,我会得到类似的东西(状态=143):

● someservice.service - Someservice
   Loaded: loaded (/usr/lib/systemd/system/someservice.service; enabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Wed 2017-08-30 09:17:40 EEST; 4s ago
  Process: 48365 ExecStop=/srv/apps/someservice/server.sh stop (code=exited, status=0/SUCCESS)
 Main PID: 46115 (code=exited, status=143)

Aug 29 17:10:02 whatever.domain.com systemd[1]: Starting Someservice...
Aug 29 17:10:02 whatever.domain.com systemd[1]: PID file /srv/apps/someservice/application.pid not readable (yet?) after start.
Aug 29 17:10:04 whatever.domain.com systemd[1]: Started Someservice.
Aug 30 09:17:39 whatever.domain.com systemd[1]: Stopping Someservice...
Aug 30 09:17:39 whatever.domain.com server.sh[48365]: Stopping someservice - PID [46115]
Aug 30 09:17:40 whatever.domain.com systemd[1]: someservice.service: main process exited, code=exited, status=143/n/a
Aug 30 09:17:40 whatever.domain.com systemd[1]: Stopped Someservice.
Aug 30 09:17:40 whatever.domain.com systemd[1]: Unit someservice.service entered failed state.
Aug 30 09:17:40 whatever.domain.com systemd[1]: someservice.service failed.

当我的启动/停止脚本中没有该return值时,它的行为完全相同。
添加到单元文件中,例如:

[Service]
SuccessExitStatus=143

对我来说不是个好主意。为什么systemctl表演如此,并没有向我展示正常的服务状态?

当我尝试修改我的启动/停止脚本而不是return 0我把return 10它的行为相同时,但我可以看到它exit 10通过了。
这是一个例子:

● someservice.service - Someservice
   Loaded: loaded (/usr/lib/systemd/system/someservice.service; enabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Wed 2017-08-30 09:36:22 EEST; 5s ago
  Process: 48460 ExecStop=/srv/apps/someservice/server.sh stop (code=exited, status=10)
  Process: 48424 ExecStart=/srv/apps/someservice/server.sh start (code=exited, status=0/SUCCESS)
 Main PID: 48430 (code=exited, status=143)

Aug 30 09:36:11 whatever.domain.com systemd[1]: Starting Someservice...
Aug 30 09:36:11 whatever.domain.com systemd[1]: PID file /srv/apps/someservice/application.pid not readable (yet?) after start.
Aug 30 09:36:13 whatever.domain.com systemd[1]: Started Someservice.
Aug 30 09:36:17 whatever.domain.com systemd[1]: Stopping Someservice...
Aug 30 09:36:17 whatever.domain.com server.sh[48460]: Stopping someservice - PID [48430]
Aug 30 09:36:21 whatever.domain.com systemd[1]: someservice.service: main process exited, code=exited, status=143/n/a
Aug 30 09:36:22 whatever.domain.com systemd[1]: someservice.service: control process exited, code=exited status=10
Aug 30 09:36:22 whatever.domain.com systemd[1]: Stopped Someservice.
Aug 30 09:36:22 whatever.domain.com systemd[1]: Unit someservice.service entered failed state.
Aug 30 09:36:22 whatever.domain.com systemd[1]: someservice.service failed.

journalctl日志中我可以看到systemctl首先返回状态 = 143,然后返回值 10。所以我猜我的错误是在启动/停止脚本中的某个地方(因为在函数返回 0 之前传递了错误代码 143)?

4

1 回答 1

11

您应该能够通过将退出代码添加到单元文件中作为“成功”退出状态来抑制这种情况:

[Service]
SuccessExitStatus=143

资源

于 2018-06-12T21:27:06.040 回答