作为 linux-service 的 Bash 脚本不会运行,但从终端执行可以完美运行

rah*_*606 8 services bash daemon autostart

我有自定义脚本来挂载谷歌驱动器。
此脚本的一部分是以下代码:

if [ ! "$(which google-drive-ocamlfuse)" ]
then
    echo "Install google-drive-ocamlfuse first!"
    exit 1
fi
Run Code Online (Sandbox Code Playgroud)

从终端执行,就像魅力一样。
因此,我将其配置为服务:

[Unit]
Description=Mount and umount google drives

[Service]
User=<usernamehere>
Type=oneshot
RemainAfterExit=true
ExecStart=/home/<usernamehere>/mybscripts/gdrivemounter.sh -m
ExecStop=/home/<usernamehere>/mybscripts/gdrivemounter.sh -u
Environment="DISPLAY=:0"
Environment="XAUTHORITY=/home/<usernamehere>/.Xauthority"

[Install]
WantedBy=graphical.target
Run Code Online (Sandbox Code Playgroud)

不幸的是,我看到退出代码:“首先安装 google-drive-ocamlfuse!” 当我检查服务状态时。

命令which google-drive-ocamlfuse在用户和 root 下为我提供了有效路径:

$ which google-drive-ocamlfuse
/home/<usernamehere>/.opam/default/bin/google-drive-ocamlfuse
Run Code Online (Sandbox Code Playgroud)

问题出在哪儿?

gle*_*man 12

问题在于,当脚本作为服务运行时,它不会以“您”的身份运行:它没有您的环境。具体来说,它没有你的PATH变量。

要么添加/home/<usernamehere>/.opam/default/bin到脚本中的 PATH,要么简单地对该程序的完整路径进行硬编码。