代码之家  ›  专栏  ›  技术社区  ›  Mark

Systemd服务:blob数据和返回代码

  •  0
  • Mark  · 技术社区  · 7 年前

    在Raspbian Stretch Lite中,我创建了以下systemd服务:

    [Unit]
    Description=Autostart
    After=multi-user.target
    
    [Service]
    Type=forking
    ExecStart=/home/pi/autostart.sh
    User=pi
    Group=pi
    
    [Install]
    WantedBy=multi-user.target
    

    这里是autostart的内容。上海:

    #!/bin/sh -ex
    
    export TERM=linux
    clear   
    mkdir -p /home/pi/logs
    /home/pi/bin/./TestApp&
    

    脚本实际上已执行(我向文件中添加了调试回音),但应用程序未启动。这是一个Qt5控制台应用程序,不是GUI应用程序。

    ./autostart.sh )按预期工作。 相反,手动启动服务将导致以下输出:

    $ sudo systemctl start autostart.service
    $ systemctl status autostart.service
    ● autostart.service - Autostart
       Loaded: loaded (/lib/systemd/system/autostart.service; enabled; vendor preset: enabled)
       Active: failed (Result: exit-code) since Thu 2017-09-28 19:56:33 CEST; 9s ago
      Process: 1351 ExecStart=/home/pi/autostart.sh (code=exited, status=0/SUCCESS)
     Main PID: 1354 (code=exited, status=127)
    
    Sep 28 19:56:33 localhost systemd[1]: Starting Autostart...
    Sep 28 19:56:33 localhost autostart.sh[1351]: + export TERM=linux
    Sep 28 19:56:33 localhost autostart.sh[1351]: + clear
    Sep 28 19:56:33 localhost autostart.sh[1351]: [34B blob data]
    Sep 28 19:56:33 localhost systemd[1]: Started Autostart.
    Sep 28 19:56:33 localhost systemd[1]: autostart.service: Main process exited, code=exited, status=127/n/a
    Sep 28 19:56:33 localhost systemd[1]: autostart.service: Unit entered failed state.
    Sep 28 19:56:33 localhost systemd[1]: autostart.service: Failed with result 'exit-code'.
    

    没关系,mkdir命令没有执行(目录已经存在),但我不理解为什么应用程序没有执行。

    1 回复  |  直到 7 年前
        1
  •  1
  •   Mark Stosberg    7 年前

    第一 running in the background is not the same as forking .

    您可以去掉autostart脚本并简化 systemd 配置文件。

    • Type=forking Type=simple ,它将为您在后台运行应用程序。
    • 设置 Environment="TERM=linux" 直接在系统配置中。
    • ExecStart= 线添加第一个 ExecStart=-/bin/mkdir -p /home/pi/logs ,额外的“破折号”将允许命令“成功”,即使目录已经创建。
    • 最后,用 ExecStart=/home/pi/bin/TestApp

    man systemd.service , man systemd.exec ,或使用 man systemd.directives