Systemd 服务启动后停止(仅在启动时)

Systemd 服务启动后停止(仅在启动时)

我有一个最小的 systemd 服务,使用 手动启动时似乎可以正常工作 systemctl start my-application.service ,使用相应的停止命令可以正常停止。由于某种原因,在系统启动期间,该服务已正确启动但立即停止。我不明白为什么这两种情况下的行为会有所不同。

该服务配置如下:

[Unit]
After=network.target

[Service]
ExecStart=start-application.sh
ExecStop=stop-application.sh
SendSIGKILL=no
KillMode=none

[Install]
WantedBy=default.target

start-application.sh如下:

#!/bin/bash
. /home/me/app-settings

cd /home/me/$APPLICATION/

./$APPLICATION

$APPLICATION 变量是从 /home/me/app-settings 读取的。它只是启动一个简单的应用程序(我甚至尝试过一个除了等待 CTRL+c 之外什么都不做的应用程序)。

我想强调的是,当我手动启动服务时,它似乎工作正常,但是在启动时,它启动并立即停止(调用 ExecStop 命令)。

使用 systemctl status 时,在启动时自动启动和使用 systemctrl start 启动时有不同的输出。

启动后

○ gp-application.service
     Loaded: loaded (/etc/systemd/system/gp-application.service; enabled; preset: enabled)
     Active: inactive (dead) since Mon 2024-02-19 12:03:09 +01; 19s ago
   Duration: 1.859s
    Process: 586 ExecStart=start-application.sh (code=exited, status=0/SUCCESS)
    Process: 746 ExecStop=stop-application.sh (code=exited, status=0/SUCCESS)
   Main PID: 586 (code=exited, status=0/SUCCESS)
        CPU: 64ms

Feb 19 12:03:08 controller su[757]: (to gp) root on none
Feb 19 12:03:08 controller su[757]: pam_unix(su-l:session): session opened for user gp(uid=1000) by (uid=0)
Feb 19 12:03:08 controller su[757]: pam_unix(su-l:session): session closed for user gp
Feb 19 12:03:08 controller su[760]: (to gp) root on none
Feb 19 12:03:08 controller su[760]: pam_unix(su-l:session): session opened for user gp(uid=1000) by (uid=0)
Feb 19 12:03:08 controller su[760]: pam_unix(su-l:session): session closed for user gp
Feb 19 12:03:08 controller su[763]: (to gp) root on none
Feb 19 12:03:08 controller su[763]: pam_unix(su-l:session): session opened for user gp(uid=1000) by (uid=0)
Feb 19 12:03:09 controller su[763]: pam_unix(su-l:session): session closed for user gp
Feb 19 12:03:09 controller systemd[1]: gp-application.service: Deactivated successfully.

如果使用 systemctl start gp-application.service 启动

● gp-application.service
     Loaded: loaded (/etc/systemd/system/gp-application.service; enabled; preset: enabled)
     Active: active (running) since Mon 2024-02-19 12:04:32 +01; 4s ago
   Main PID: 826 (start-applicati)
      Tasks: 2 (limit: 9279)
     Memory: 3.2M
        CPU: 26ms
     CGroup: /system.slice/gp-application.service
             ├─826 /bin/bash /usr/local/bin/start-application.sh
             └─840 . speedy

Feb 19 12:04:33 controller su[837]: (to gp) root on none
Feb 19 12:04:33 controller su[837]: pam_unix(su-l:session): session opened for user gp(uid=1000) by (uid=0)
Feb 19 12:04:33 controller su[837]: pam_unix(su-l:session): session closed for user gp
Feb 19 12:04:33 controller start-application.sh[840]: SPEEDY 7.0.0
Feb 19 12:04:33 controller start-application.sh[840]: RT-APP-COMMON 2.0.0
Feb 19 12:04:33 controller start-application.sh[840]: 12:04:33.587 utils.cpp                     :117  - utilsInit()                   Utils successfully initialized.
Feb 19 12:04:33 controller start-application.sh[840]: 12:04:33.587 utils.cpp                     :244  - checkAndCreateRTLockFile()    RT lock file: /tmp/.rt-app-lock successfully created.
Feb 19 12:04:33 controller start-application.sh[840]: 12:04:33.587 main.cpp                      :768  - InitProgram()                 Reading font table
Feb 19 12:04:33 controller start-application.sh[840]: 12:04:33.587 fnttable.cpp                  :144  - ReadFontTable()               Error font file font12x8.aff
Feb 19 12:04:33 controller start-application.sh[840]: 12:04:33.587 common-parread.cpp            :83   - readCommonParameters()        EventSocket disabled as requested via parameter file.

答案1

作为亚历克斯指出,应用程序正在启动但立即停止,退出代码为 0/SUCCESS。

文档,应用程序SUCCESS退出后,会自动调用ExecStop命令。

应用程序停止的原因与 systemd 本身无关,因为我从应用程序日志中看到,亚历克斯建议。

相关内容