シェルスクリプトは手動で実行されますが、systemdから起動すると期待どおりに機能しません。

シェルスクリプトは手動で実行されますが、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/systemsystemdサービスを作成しました。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.shGoogle Driveフォルダが正しくマウントされます。しかし、起動時にスクリプトを正常に実行するためにsystemdサービスを取得できないようです。

私の.Iで実行して再起動するように~/.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.servicesystemdはスクリプトを実行しているようですが、何らかの理由でスクリプトは実行されません。実際にインストール~/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'ドライブが期待どおりに完全にマウントされました。

~/GoogleDrivesystemdがスクリプトを「成功的に」実行しているように見えますが、実際にはbashスクリプトを手動で実行したときのようにフォルダをマウントしないのはなぜですか?google-drive-ocamlfuse

ベストアンサー1

すべての " ~"をフルパスに置き換えます。 " ~"は機能です$SHELL。たとえば、.make verify()の代わり/home/user240467/GoogleDriveにマウントが存在することを確認してください。~/GoogleDriveman mkdir/home/user240467/GoogleDrive

私のスクリプトをダウンロード、使用、変更できますnet-o-matic

この問題を解決するためにbashスクリプトを作成しました。https://github.com/waltinator/net-o-matic接続を監視し、接続が失われた場合は、ユーザーが指定した操作を実行して再接続を試みます。

おすすめ記事