Systemd 服务:blob 数据和 return 代码
Systemd service: blob data and return code
在 Raspbian Stretch Lite 中,我创建了以下系统服务:
[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.sh的内容:
#!/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命令是可以的(目录已经存在),但我不明白为什么不执行应用程序。
我该怎么做才能获得有关正在发生的事情的更多信息?
首先,running in the background is not the same as forking。
您可以删除自动启动脚本并创建一个更简单的 systemd
配置文件。
- 删除
Type=forking
。默认值为 Type=simple
,它将在后台为您处理 运行 应用程序。
- 直接在系统配置中设置
Environment="TERM=linux"
。
- 您可以有多个
ExecStart=
行。添加第一个 ExecStart=-/bin/mkdir -p /home/pi/logs
,额外的 "dash" 将允许命令 'succeed' 即使目录已经创建。
- 最后,运行 您的应用
ExecStart=/home/pi/bin/TestApp
相关的手册页是 man systemd.service
、man systemd.exec
,或使用 man systemd.directives
查找任何指令。
在 Raspbian Stretch Lite 中,我创建了以下系统服务:
[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.sh的内容:
#!/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命令是可以的(目录已经存在),但我不明白为什么不执行应用程序。
我该怎么做才能获得有关正在发生的事情的更多信息?
首先,running in the background is not the same as forking。
您可以删除自动启动脚本并创建一个更简单的 systemd
配置文件。
- 删除
Type=forking
。默认值为Type=simple
,它将在后台为您处理 运行 应用程序。 - 直接在系统配置中设置
Environment="TERM=linux"
。 - 您可以有多个
ExecStart=
行。添加第一个ExecStart=-/bin/mkdir -p /home/pi/logs
,额外的 "dash" 将允许命令 'succeed' 即使目录已经创建。 - 最后,运行 您的应用
ExecStart=/home/pi/bin/TestApp
相关的手册页是 man systemd.service
、man systemd.exec
,或使用 man systemd.directives
查找任何指令。