0

I'm trying to get the rclone service to mount a drive as soon as the system has received a network connection on boot/reboot.

So far I have all of the mounting working correctly via terminal. I have written a simple .sh file to execute it which basically is:

#!/bin/sh
! mountpoint -q /home/{user}/{location}/{location} || umount /home/{user}/{location}/{location}    

rclone mount {nameofservice}: /home/{user}/{location}/{location} --config /home/{user}/.config/rclone/rclone.conf

Running this in terminal works as expected.

I have followed an online tutorial to get this working after a network connection has been achieved using systemd and have created the following file with 755 +x permissions in /etc/systemd/system/{nameof.service}

[Unit]
Description=Starts {nameof.service} rclone service on startup
Wants=network-online.target
After=network-online.target
[Service]
Type=simple
User={user}
Group={group}
ExecStart=/home/{user}/{nameofsh}.sh
TimeoutStartSec=5
RemainAfterExit=yes
[Install]
WantedBy=network-online.target

I need it to run as that particular user. Following creation of this file I also:

systemctl daemon-reload
systemctl enable {nameof.service}

In the tutorial I expected the enable command to return a message about creating a symlink which I didn't get - it just retuned with a new line, but I didn't think this was major.

Regardless, when I run systemctl restart {nameof.service} I get the expected outcome, but not on start up or reboot.

At this point I'm not sure how I've gone wrong and would appreciate any help.

jamesrsg
  • 3
  • 1

1 Answers1

0

By setting WantedBy=default.target and leaving the rest unchanged, the service will be associated with the default.target, which represents the default system boot target. This ensures that the script runs during the boot process when the network is available.

Also, if it's only the symlink that is missing you can add it manually, with ln -s /etc/systemd/system/{nameof.service} /etc/systemd/system/multi-user.target.wants/{nameof.service}

Grigorios
  • 151
  • 10
  • Thank you this worked. The tutorial I followed indicated that I should have WantedBy=network-online.target which to me would indicate that is kind of a trigger for it to run correctly. Can you explain why default target worked? Is it because the [Install] section gets it ready for the Wants under the [Unit] section? – jamesrsg Jul 11 '23 at 11:07
  • It's hard to diagnose… Could just be, that your network-online target was never reached during the boot process. When the system reaches the default target during the boot process, it starts all the services that are associated with it. Since your service has a dependency on network-online.target through the Wants directive, systemd ensures that the network is online before starting your service. – Grigorios Jul 13 '23 at 12:15