Shell 脚本手动运行,但由 systemd 启动时行为不符合预期

Shell 脚本手动运行,但由 systemd 启动时行为不符合预期

我正在尝试google-drive-ocamlfuse在启动时挂载 Fedora 35 Linux。

我已经安装opam并且安装成功了google-drive-ocamlfuse

因为这是在笔记本电脑上使用 WiFi 连接到互联网,所以我创建了一个 bash 脚本在启动时运行,按照google-drive-ocamlfuse维基上的这些说明

#!/bin/bash

while true; do
  # check to see if there is a connection by pinging a Google server
  if ping -q -c 1 -W 1 8.8.8.8 >/dev/null; then
    # if connected, mount the drive and break the loop
    /home/myusername/.opam/default/bin/google-drive-ocamlfuse /home/myusername/GoogleDrive; break;
  else
    # if not connected, wait for one second and then check again
    sleep 1
  fi
done

我已将脚本放入 中/usr/local/bin,尽管 wiki 上的简介确实说将其放入 中/home/username/bin。我看不出为什么这会以某种方式重要。

在 中/etc/systemd/system,我创建了一个 systemd 服务,mount-google-drive.service它应该运行脚本:

[Unit]
Description=Run shell script to launch google-drive-ocamlfuse at startup

[Service]
User=myusername
Group=myusername
ExecStart=/usr/local/bin/mount-google-drive-ocamlfuse.sh

[Install]
WantedBy=multi-user.target

如果我$ /usr/local/bin/mount-google-drive-ocamlfuse.sh手动运行,那么 Google Drive 文件夹就可以正常安装。但是,我似乎无法让 systemd 服务在启动时成功运行脚本。

我修改了我的~/.bashrc文件,export PATH=$PATH:$HOME/.opam/default/bin/以便google-drive-ocamlfuse在我的$PATH.我已经运行$ sudo systemctl daemon-reload并重新启动。

/usr/local/bin/mount-google-drive-ocamlfuse.sh有权限-rwxr-xr-x

/etc/systemd/system/mount-google-drive.service有权限-rwxr-xr-x

当我运行时$ sudo systemctl status mount-google-drive.service,似乎 systemd 确实运行了该脚本,但由于某种原因它没有运行实际上安装~/GoogleDrive文件夹:

$ sudo systemctl status mount-google-drive.service 
○ mount-google-drive.service - Run shell script to launch google-drive-ocamlfuse at startup
     Loaded: loaded (/etc/systemd/system/mount-google-drive.service; enabled; vendor preset: disabled)
     Active: inactive (dead) since Sun 2021-12-05 22:36:19 CST; 2min 58s ago
    Process: 1360 ExecStart=/usr/local/bin/mount-google-drive-ocamlfuse.sh (code=exited, status=0/SUCCESS)
   Main PID: 1360 (code=exited, status=0/SUCCESS)
        CPU: 67ms

Dec 05 22:36:11 fedora systemd[1]: Started Run shell script to launch google-drive-ocamlfuse at startup.
Dec 05 22:36:11 fedora mount-google-drive-ocamlfuse.sh[1382]: ping: connect: Network is unreachable
Dec 05 22:36:12 fedora mount-google-drive-ocamlfuse.sh[1573]: ping: connect: Network is unreachable
Dec 05 22:36:13 fedora mount-google-drive-ocamlfuse.sh[1614]: ping: connect: Network is unreachable
Dec 05 22:36:14 fedora mount-google-drive-ocamlfuse.sh[1616]: ping: connect: Network is unreachable
Dec 05 22:36:15 fedora mount-google-drive-ocamlfuse.sh[1618]: ping: connect: Network is unreachable
Dec 05 22:36:16 fedora mount-google-drive-ocamlfuse.sh[1620]: ping: connect: Network is unreachable
Dec 05 22:36:17 fedora mount-google-drive-ocamlfuse.sh[1622]: ping: connect: Network is unreachable
Dec 05 22:36:19 localhost-live.lan systemd[1]: mount-google-drive.service: Deactivated successfully.

为了检查以确保myusername确实正在运行脚本,我运行$ su - myusername -c 'bash /usr/local/bin/mount-google-drive-ocamlfuse.sh'并按照预期完美安装了驱动器。

为什么 systemd 似乎“成功”运行了脚本,但实际上它没有像我手动运行 bash 脚本时那样安装~/GoogleDrive文件夹?google-drive-ocamlfuse

答案1

将所有“ ~”替换为完整路径。 “ ~”是一个$SHELL特征。例如 mount/home/user240467/GoogleDrive而不是~/GoogleDrive.确保 ( man mkdir)/home/user240467/GoogleDrive存在。

欢迎您下载、使用和修改我的net-o-matic脚本。

我编写了一个 bash 脚本来帮助解决此问题:https://github.com/waltinator/net-o-matic它监视连接,当连接断开时,执行用户指定的操作来尝试重新连接。

相关内容