Mount First, Then Run

by TheOuterLinux (https://theouterlinux.gitlab.io)

Last updated: 2024/07/07

[ Click to visit TheOuterLinux's 'Support Me' page ] [ QRCode ] [ RSS ]

Table of Contents

  1. How to make sure that an external hard drive is mounted before running a program
    1. Step 01: Create a BASH script
    2. Step 02: Create a *.desktop file

How to make sure that an external hard drive is mounted before running a program

Let us say that you do not have enough hard drive space on your computer to run what is known as a "source port," a program, usually created from source code released by a video game company, and so you need to place the bulk of the required files on an external hard drive. However, some of these sorts of programs slightly panic when they cannot find required files and may ask you to go through the setup process all over again -- this is very annoying. To prevent this, all you have to do is prevent the program from running unless the the USB/SD card is mounted.

Step 01: Create a BASH script

The first thing we need to do is create a BASH script that looks to see if a directory exists; if this directory does not exist, then have it display a a message saying so and that the program will not run until it does. Like so...

        
        #!/bin/bash
        if [ ! -d "/media/[username]/####-####" ]
        then
            if [ -f '/usr/bin/notify-send' ]
            then
                notify-send 'Hard drive not mounted.'
                notify-send '[Program Name] will not start.'
            else
                xterm -T '[Program Name]: Whoops...' -e dialog --msgbox "Hard drive not mounted.\nP[Program Name] will not start." 0 0
            fi
        else
            sh -c "/path/to/program-launcher/or/binary"
        fi
    

Replace "[username]" with your username and "[Program Name]" with the name of the program or source-port. The "####-####" part is relative to whatever your URL bar says in your file-manager in regards to the external hard drive; this will be much longer if the partition is anything other than FAT-related. You can save this script anywhere, but it would probably be best just to keep it where the *.desktop file is going to be.

Step 02: Create a *.desktop file

Now that we have our BASH script, perhaps it would be a good idea to add a menu item to more easily run it. Go to the "~/.local/share/applications" directory, or create one if it does not exist, and add the file "[ProgramName].desktop" (change as needed) with something similar to the following contents:

        
        [Desktop Entry]
        Name=Program Name
        Version=1.0
        Comment=Something that quickly describes the program
        Exec=sh -c "~/.local/share/applications/ProgramName.sh"
        Terminal=false
        Icon=/full/path/to/icon.png
        Categories=Game;
    

And that is it. Now if you were to try to run the program without the hard drive mounted, the system should only display messages and do nothing else.





Disclaimer