我的系统版本:
Linux 6.2.15-lux-amd64 #2 SMP PREEMPT_DYNAMIC x86_64 GNU/Linux
(基于 Debian 的系统)
我基本上是在尝试创建一个服务来在启动桌面时执行我的Python应用程序,所以直接说吧,我基本上有我的.service文件:
[Unit]
Description=init_todo_app
[Service]
ExecStart=/bin/sh /etc/systemd/system/init_script_caller.sh
[Install]
WantedBy=multi-user.target
它执行的脚本(init_script_caller.sh):
#!/bin/sh
PATH_EXEC=/path_to/init.sh
${PATH_EXEC}
最后,在前面的脚本中调用了 init.sh 文件,该文件位于应用程序根文件夹内,他启动虚拟环境并执行应用程序。
#!/bin/sh
ACTUAL_FOLDER=/folder/to_app_root
. "${ACTUAL_FOLDER}/env/bin/activate"
python3 "${ACTUAL_FOLDER}/src/main.py" "minimize"
chmod 755 init.sh
chmod 755 init_script_caller.sh
chmod 755 todo_service.service
sudo cp -p todo_service.service /etc/systemd/system/todo_service.service
sudo cp -p init_script_caller.sh /etc/systemd/system/init_script_caller.sh
systemctl enable --now todo_service.service
我授予了所有文件的执行权限,并将服务和他调用的脚本(init_script_caller.sh)移动到/etc/systemd/system/。但是当我尝试
systemctl status todo_service.service
时,我得到:
todo_service.service - init_todo_app
Loaded: loaded (/etc/systemd/system/todo_service.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Sun 2024-02-18 10:08:43 -03; 7min ago
Process: 293266 ExecStart=/bin/sh /etc/systemd/system/init_script_caller.sh (code=exited, status=1/FAILURE)
Main PID: 293266 (code=exited, status=1/FAILURE)
CPU: 259ms
我已经测试过了,我可以使用“./init_script_caller.sh”在里面运行一切正常 /etc/systemd/system/ 文件夹,所以我不知道问题出在哪里,我也找不到调试它的方法,因为脚本本身运行良好。
这与其说是一个“systemd”问题,不如说是一个“软件架构”问题。
我基本上是在尝试创建一个服务来在启动桌面时执行我的Python应用程序,所以直接说吧,我基本上有我的.service文件:
这是行不通的,因为系统服务与桌面应用程序运行在不同的环境中。 (这不再是 Windows 98!)
您的桌面应用程序在您自己的用户帐户下运行;如果没有另外指定,服务将作为“root”运行 - 它们不会对“当前登录”用户做出任何假设,因为登录用户的数量可能少于一个、多个,或者实际上是不同数量的。
您的桌面应用程序从 shell(或图形启动器)接收大量环境变量,最关键的是指定“显示服务器”地址(X11 或 Wayland 套接字)的变量。服务不会收到其中任何一个,部分原因是对于 multi-user.target,其中许多环境变量尚不存在(详见下文)。
甚至图形登录屏幕(与您自己的桌面完全独立的“桌面”)也只是作为另一个服务启动,所以即使这样尚不能保证可用 – 更不用说您自己的桌面了,它会晚很多时间启动。
(与 Windows 相比,Windows XP 中曾经有“会话 0”和“交互式服务”,但在后续版本中故意删除了这种可能性。)
请勿将其设为服务 - 通过将 *.desktop 文件放入 /etc/xdg/autostart
或
~/.config/autostart
中来使用“XDG 自动启动”机制。
仅通过 pkexec、D-Bus 或某些方式调用特权“帮助程序”进程来实现其他形式的 IPC。 (示例:GNOME 磁盘,它以非特权方式运行,但调用 UDisks 服务来创建文件系统并执行其他特权操作。)