systemctl 无法启动服务(代码=退出状态=0/成功)

systemctl 无法启动服务(代码=退出状态=0/成功)

我在 CentOS 7 服务器上安装了 Shadowsocks-r,我想使用 systemd 来管理它。因此我在 /etc/systemd/system 中创建了一个名为 shadowsocks-r.service 的文件。

shadowsocks-r.service:

[Unit]
Description=Shadowsocks-R Server
After=network.target

[Service]
ExecStart=/usr/local/shadowsocks/server.py -c /etc/shadowsocks-r/config.json -d start
ExecStop=/etc/init.d/shadowsocks-r stop

[Install]
WantedBy=multi-user.target

但服务无法正确启动。检查服务状态时,我得到以下信息:

shadowsocks-r.service - Shadowsocks-R Server
Loaded: loaded (/etc/systemd/system/shadowsocks-r.service; enabled; vendor preset: disabled)
Active: inactive (dead) since Tue 2019-05-14 10:29:46 UTC; 8s ago
Process: 19267 ExecStop=/etc/init.d/shadowsocks-r stop (code=exited, status=0/SUCCESS)
Process: 19234 ExecStart=/etc/init.d/shadowsocks-r start (code=exited, status=0/SUCCESS)
Main PID: 19234 (code=exited, status=0/SUCCESS)

May 14 10:29:46 vultr.guest systemd[1]: Started Shadowsocks-R Server.
May 14 10:29:46 vultr.guest shadowsocks-r[19234]: 2019-05-14 10:29:46 INFO     util.py:94 loading libcrypto from libcrypto.so.10
May 14 10:29:46 vultr.guest shadowsocks-r[19234]: 2019-05-14 10:29:46 INFO     shell.py:74 ShadowsocksR 2.8.2
May 14 10:29:46 vultr.guest shadowsocks-r[19234]: IPv6 support
May 14 10:29:46 vultr.guest shadowsocks-r[19234]: Starting ShadowsocksR success
May 14 10:29:46 vultr.guest shadowsocks-r[19267]: 2019-05-14 10:29:46 INFO     shell.py:74 ShadowsocksR 2.8.2
May 14 10:29:46 vultr.guest shadowsocks-r[19267]: IPv6 support
May 14 10:29:46 vultr.guest shadowsocks-r[19267]: stopped
May 14 10:29:46 vultr.guest shadowsocks-r[19267]: Stopping ShadowsocksR success

shadowsocks-r 消息来自启动和停止函数。从消息来看,启动和停止函数似乎都已执行。为什么会发生这种情况?我应该怎么做才能纠正它?谢谢!


更新:

跑步后systemctl start shadowsocks-r.service我尝试跑步journalctl -xe,这是我得到的结果:

May 14 11:09:01 vultr.guest systemd[1]: Started Shadowsocks-R Server.
-- Subject: Unit shadowsocks-r.service has finished start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit shadowsocks-r.service has finished starting up.
--
-- The start-up result is done.
May 14 11:09:01 vultr.guest polkitd[1547]: Unregistered Authentication Agent for unix-process:19521:1303198 (system bus name :1.57, object path
May 14 11:09:01 vultr.guest shadowsocks-r[19527]: 2019-05-14 11:09:01 INFO     util.py:94 loading libcrypto from libcrypto.so.10
May 14 11:09:01 vultr.guest shadowsocks-r[19527]: 2019-05-14 11:09:01 INFO     shell.py:74 ShadowsocksR 2.8.2
May 14 11:09:01 vultr.guest shadowsocks-r[19527]: IPv6 support
May 14 11:09:01 vultr.guest shadowsocks-r[19527]: Starting ShadowsocksR success
May 14 11:09:01 vultr.guest shadowsocks-r[19560]: 2019-05-14 11:09:01 INFO     shell.py:74 ShadowsocksR 2.8.2
May 14 11:09:02 vultr.guest shadowsocks-r[19560]: IPv6 support
May 14 11:09:02 vultr.guest shadowsocks-r[19560]: stopped
May 14 11:09:02 vultr.guest shadowsocks-r[19560]: Stopping ShadowsocksR success

- - - 更新 - - -

现在我又尝试了几次,确认即使我使用命令“start”,systemctl 命令实际上也会停止该进程。以下是我尝试的:

  1. 首先我开始/etc/init.d/shadowsocks-r start

终端返回:

IPv6 support
2019-05-16 08:39:30 INFO     util.py:94 loading libcrypto from libcrypto.so.10
2019-05-16 08:39:30 INFO     shell.py:74 ShadowsocksR 2.8.2
started
Starting ShadowsocksR success
  1. 然后我跑systemctl start shadowsocks-r.service
  2. 然后我运行/etc/init.d/shadowsocks-r status,结果是:ShadowsocksR is stopped

因此,即使我使用其他方法启动了 shadowsocks-r 进程,systemctl 也会在我尝试启动它时将其终止。

  1. 运行后systemctl status shadowsocks-r.service,我得到的结果如下:
    shadowsocks-r.service - Shadowsocks-R Server
    Loaded: loaded (/etc/systemd/system/shadowsocks-r.service; enabled; vendor preset: disabled)
    Active: failed (Result: exit-code) since Thu 2019-05-16 08:45:10 UTC; 12min ago
    Process: 713 ExecStop=/usr/local/shadowsocks/server.py -c /etc/shadowsocks-r/config.json -d stop (code=exited, status=0/SUCCESS)
    Process: 686 ExecStart=/usr/local/shadowsocks/server.py -c /etc/shadowsocks-r/config.json -d start (code=exited, status=1/FAILURE)
    Main PID: 686 (code=exited, status=1/FAILURE)

    May 16 08:45:10 vultr.guest server.py[686]: 2019-05-16 08:45:10 INFO     shell.py:74 ShadowsocksR 2.8.2
    May 16 08:45:10 vultr.guest server.py[686]: 2019-05-16 08:45:10 ERROR    daemon.py:75 already started at pid 670
    May 16 08:45:10 vultr.guest server.py[686]: IPv6 support
    May 16 08:45:10 vultr.guest server.py[686]: IPv6 support
    May 16 08:45:10 vultr.guest systemd[1]: shadowsocks-r.service: main process exited, code=exited, status=1/FAILURE
    May 16 08:45:10 vultr.guest server.py[713]: 2019-05-16 08:45:10 INFO     shell.py:74 ShadowsocksR 2.8.2
    May 16 08:45:10 vultr.guest server.py[713]: IPv6 support
    May 16 08:45:10 vultr.guest server.py[713]: stopped
    May 16 08:45:10 vultr.guest systemd[1]: Unit shadowsocks-r.service entered failed state.
    May 16 08:45:10 vultr.guest systemd[1]: shadowsocks-r.service failed.

- - - -更新 - - - -

根据@Daniel B 的建议,我删除了服务文件中的 ExecStop 行:

[Unit]
Description=Shadowsocks-R Server
After=network.target

[Service]
ExecStart=/usr/local/shadowsocks/server.py -c /etc/shadowsocks-r/config.json -d start

[Install]
WantedBy=multi-user.target

我首先停止 server.py 进程,/etc/init.d/shadowsocks-r stop然后它返回ShadowsocksR is stopped

然后我跑

systemctl daemon-reload
systemctl start shadowsocks-r.service
systemctl status shadowsocks-r.service

我仍然收到错误消息,并且似乎 systemctl 命令无法启动该进程(在启动之前先停止该进程)

shadowsocks-r.service - Shadowsocks-R Server
   Loaded: loaded (/etc/systemd/system/shadowsocks-r.service; enabled; vendor preset: disabled)
   Active: inactive (dead) since Thu 2019-05-16 09:40:43 UTC; 29s ago
  Process: 2964 ExecStart=/usr/local/shadowsocks/server.py -c /etc/shadowsocks-r/config.json -d start (code=exited, status=0/SUCCESS)
 Main PID: 2964 (code=exited, status=0/SUCCESS)

May 16 09:40:43 vultr.guest systemd[1]: Started Shadowsocks-R Server.
May 16 09:40:43 vultr.guest server.py[2964]: 2019-05-16 09:40:43 INFO     util.py:94 loading libcrypto from libcrypto.so.10
May 16 09:40:43 vultr.guest server.py[2964]: 2019-05-16 09:40:43 INFO     shell.py:74 ShadowsocksR 2.8.2
May 16 09:40:43 vultr.guest server.py[2964]: IPv6 support

答案1

你没有指定Type。默认情况下为simple,即期望服务进程不退出。

根据项目页面,前台运行就跟不通过一样简单-d start

完整的单元文件如下所示:

[Unit]
Description=Shadowsocks-R Server
After=network.target

[Service]
ExecStart=/usr/local/shadowsocks/server.py -c /etc/shadowsocks-r/config.json

[Install]
WantedBy=multi-user.target

Systemd 将自动负责监控该过程,并根据请求/要求重新启动和停止它。

答案2

将其投入服务

RemainAfterExit=true

相关内容