0

In my .profile script I want to export a variable if an external USB hardisk is mounted. I need this variable in later Perl scripts to determine if the device is mounted. Especially, if the device is not mounted, the later scripts behave differently.

Since these Perl scripts are also run in a Windows environment, I cannot simply check for the existence of the directory when the scripts are run. Also, the script run on different machines, some of which sometimes have harddisk attached. When the harddisk is attached it won't be removed until the machine is shut down.

Besides, the scripts do their job perfectly and I'd rather not change them.

The device gets mounted to /media/rene/DEVICE_NAME.

In my home directory, I have a symbolic link to the mount point:

ln -s /media/rene/DEVICE_NAME ~/ext_hd

So, in my ~/.script file I have the following test

if [[ -d ~/ext_hd ]]; then
   export device_mounted=yes
fi

Now, apparently, these lines are executed before the mount process is finished mounting the external harddisk. Thus, the variable device_mounted is not assigned.

So, is there a way to wait for the mount process to finish before I execute the test?

2

How about running a script as a daemon?

#!/bin/sh
while true; do
    if mountpoint -q /media/rene/DEVICE_NAME; then
        export device_mounted=yes
    else
        unset device_mounted
    fi
    sleep 30
done

Edit:

With the clarification in your edited question, we can simplify the script a bit:

#!/bin/sh
while true; do
    if mountpoint -q /media/rene/DEVICE_NAME; then
        export device_mounted=yes
        break
    fi
    sleep 30
done

Save that somewhere as e.g. device-check.sh and make the script executable:

chmod +x /path/to/device-check.sh

Then add this line in ~/.profile:

/path/to/device-check.sh &

The trailing & character is essential. It makes the script start as a separate process, allowing the login process to continue.

After this the script starts at login and checks every 30th second if the device is mounted. If/when the script finds that the device is mounted, it sets the environment variable and ends.

  • I've never used a script as a daemon. Where and how would I start this script? Does export ... influence other unrelated processes? – René Nyffenegger Feb 3 '18 at 10:30
  • @RenéNyffenegger: Please consider the script as a draft, an idea. If you need more detailed help, I think you need to let us know more about the circumstances. How/when is the device mounted? Once mounted, will it keep being mounted through the session, or may it be unmounted later? What kind of machine are you running - desktop, server, ...? I'd suggest that you edit your question and try to cover those questions. (Setting or unsetting an environment variable does not affect already running processes. New processes which don't query that specific variable won't be affected either.) – Gunnar Hjalmarsson Feb 3 '18 at 10:49
  • @RenéNyffenegger: Answer edited. – Gunnar Hjalmarsson Feb 4 '18 at 0:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.