为什么我不能使用 systemd 停止我的 Mojo 应用程序?

Why can't I stop my Mojo app using systemd?

我可以通过 systemd 启动我的 Mojo 应用程序而不会出现任何问题,但我无法以同样的方式停止或重新启动它。我的配置主要来自 http://mojolicious.org/perldoc/Mojolicious/Guides/Cookbook#Pre-forking:

的 Mojo 文档
[Unit]
Description=ldsquiz.org
After=network.target

[Service]
Type=simple
Restart=always
RestartSec=30
ExecStart=/home/sites/www.ldsquiz.org/checkout/bin/carton-exec app.pl prefork -m production -l http://*:5103
KillMode=process
User=ldsquiz
Group=ldsquiz
SyslogIdentifier=ldsquiz-org

[Install]
WantedBy=multi-user.target 

如果我通过 sudo service ldsquiz-org stop 停止它,系统日志会显示 Jan 13 16:37:48 web-hosting systemd[1]: Stopped ldsquiz.org. 但进程仍然 运行.

A restart via systemd 看起来像我期望的那样,因为由于关闭失败,侦听套接字仍在使用中:

Jan 13 16:39:13 web-hosting systemd[1]: Stopped ldsquiz.org.
Jan 13 16:39:13 web-hosting systemd[1]: Started ldsquiz.org.
Jan 13 16:39:14 web-hosting ldsquiz-org[20353]: Can't create listen socket: Address already in use at 
/home/sites/www.ldsquiz.org/checkout/local/lib/perl5/Mojo/IOLoop.pm line 126.
Jan 13 16:39:14 web-hosting systemd[1]: ldsquiz-org.service: Main process exited, code=exited, status=98/n/a
Jan 13 16:39:14 web-hosting systemd[1]: ldsquiz-org.service: Unit entered failed state.
Jan 13 16:39:14 web-hosting systemd[1]: ldsquiz-org.service: Failed with result 'exit-code'.
Jan 13 16:39:15 web-hosting ldsquiz-org[16886]: [Sat Jan 13 16:39:15 2018] [info] Creating process id file "/tmp/prefork.pid"

我在这里错过了什么?

显然 Mojolicious Cookbook w.r.t 中存在缺陷。预分叉和系统单元。

They say:

And to manage the pre-forking web server with systemd, you can use a unit configuration file like this.

[Unit]
Description=My Mojolicious application
After=network.target

[Service]
Type=simple
ExecStart=/home/sri/myapp/script/my_app prefork -m production -l http://*:8080
KillMode=process

[Install]
WantedBy=multi-user.target

KillMode=process 设置被证明是错误的,需要替换为 KillMode=control-group — 这是默认设置,因此我们可以完全放弃它。

KillMode docs 说:

If set to control-group, all remaining processes in the control group of this unit will be killed on unit stop (for services: after the stop command is executed, as configured with ExecStop=). If set to process, only the main process itself is killed.

(强调我的)

因此:

[Unit]
Description=My Mojolicious application
After=network.target

[Service]
Type=simple
ExecStart=/home/sri/myapp/script/my_app prefork -m production -l http://*:8080
KillMode=control-group

[Install]
WantedBy=multi-user.target

请注意,KillMode=control-group 可以省略,因为此设置是默认设置。

参考文献: