0

I am currently working on a bash script which should mount a veracrypt encrypted hdd on sundays to run backups. Since I want to rotate between two different HDDs every week, I have to check whether /dev/sdb1 or /dev/sdc1 is currently connected to the device before it gets mounted by the script:

hdd_sdb1="$(fdisk -l | grep /dev/sdb1)"
hdd_sdc1="$(fdisk -l | grep /dev/sdc1)"
if [ -n "$hdd_sdb1" ]
then
    hdd_name=/dev/sdb1
fi

if [ -n "$hdd_sdc1" ]
then
    hdd_name=/dev/sdc1
fi

Last weekend I've just changed the variable in the script and the mount worked fine. This weekend, when the script was run, none of the above fdisk commands returned a line, so the if query wasn't working. Now I wonder if the operating system deactivates the hard disk after a while if it has not been mounted and if there is a way to deactivate this?

6
  • Do the disks show up if yu run fdisk interactively from the command line? Commented Jan 20, 2020 at 9:52
  • (hard disk) devices are dynamic. You can't rely on their value/direct name. Order could change upon reboot or replug. Use properties of the disks in /dev/disk/by-*/ Commented Jan 20, 2020 at 10:23
  • @A.B Thanks for the information. I'll do that. But that was not connected to the issue I am having, because I've checked the value again and for now, it is still the same. Commented Jan 20, 2020 at 11:09
  • @RudiC Yes, I see the disks when I use fdisk, but won't see them with df, unless I mount one of them. But I don't know why the hdd wasn't listed in the fdisk list yesterday, when the script was running. I've checked the log file of my script and the $hdd_name variable was empty, so I guess the hdd wasn't found by the fdisk command at this time. Commented Jan 20, 2020 at 11:19
  • Have the script list the entire fdisk output so you can see what it sees. Commented Jan 20, 2020 at 11:26

1 Answer 1

0

I found the problem.

It wasn't because the hard drive wasn't mounted properly or it shut down, but because of the fdisk command. Since the script was executed with a cronjob and no user is logged in, fdisk does not return any output, so the variable always remains empty.

I now simply test if the file sdb1 or sdc1 is in the /dev folder and bypass the fdisk command.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.