How can I make an application start only after the external hard drive is mounted upon boot?

I use qbittorrent for P2P services and have set it to start on boot. However, it should start after my external hard drive is mounted so it can access the content on the external drive. I’ve already enabled the “Automatically mount removable media when attached” and “Automatically mount all removable media at login” features in the system, but unfortunately, qbittorrent always starts before the external hard drive is mounted after each boot. Is there a way to achieve this?
Thank you for all your help!

You could create a systemd service for doing both, i.e. first mount the external drive, and only after that, start qbittorrent. This is a much more solid approach than having your external drive automounted by your desktop environment.

3 Likes

Easy. Launch qBittorrent via a script that will sleep for a few minutes before starting qBittorrent. You just need to save the following in your local bin folder (make sure the script is executable), and then add the script (eg: /home/[user]/bin/qbittorrentdelaystart) to Autostart (make sure you also remove qBittorrent from autostart):

#!/bin/bash

# qbittorrentdelaystart
# a script to start qBittorrent after a couple of minutes

#Do nothing for 2 minutes
sleep 2m

#Touch a file on the external drive to make sure it is mounted
touch /path/to/external/drive/qbtouchfile.txt

#Wait 30 seconds for the external drive to fully spin up (if HDD)
sleep 30

#Launch qBittorrent as its own process
qbittorrent &

exit 0

If you are familiar with systemd, then I would recommend @Aragorn’s suggestion, maybe as a systemd timer with OnStartupSec= set to something like a minute or 2 to give the external drive time to mount & spin up.

2 Likes

I’ve considered using systemd, but since currently in Manjaro, autostarting apps is done by placing their desktop files in the autostart folder, I’ve been trying to find a self-contained way. However, for now, I’ll use systemd to achieve this functionality. Thank you!

1 Like

Oh, by the way, is it possible to make all the desktop files under the autostart folder start only after the hard drive is loaded?

Not the .desktop files, but you could replace them with shell scripts that invoke a timeout — via sleep — or that check whether the volume is mounted or not.

The easiest way of doing things would be to have your external drive automatically mounted at boot time from within /etc/fstab — but with the nofail mount option, so that the system doesn’t hang if you happen to boot up without the drive attached — and to then have qbittorrent started upon login.

This should normally make sure that the volume is mounted before your desktop applications are started.

1 Like

What desktop environment are you using? If using Plasma (edit: sorry - I just noticed that you are), you should be able to automount your external drives upon user login (not boot) via System Settings > Removable Storage > Removable Devices. And adding autostart jobs is easy via System Settings > Startup and Shutdown > Autostart, but to delay them until the external drive is mounted you may have to go down the path of using scripts with sleep at the start like the one in my earlier post.

2 Likes

I have done all you mentioned, but it seems not good for I needed, however thanks!

I will do that, thank for your help! :smiley_cat:

1 Like

Considering that we’re dealing with a race condition, I think it would be wiser to have the volume in question mounted at boot time, and not after the user has logged in. That way, no sleep commands will be necessary. :wink:

2 Likes

I added the following line to the /etc/fstab file, and now everything is working well.

/dev/sda2                         /run/media/ling/新加卷 ntfs3   defaults,user,nofail 0 2

Thanks again! :grimacing:

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.