I got a script that is supposed to reformat and mount /dev/disk/by-label/backup_disk into /mnt/backup_disk. Like this:

/sbin/mkfs.ext4 -q -L backup_disk /dev/disk/by-label/backup_disk
mount -t ext4 /dev/disk/by-label/backup_disk /mnt/backup_disk -o noatime,data=writeback

Bash output is:

mount: special device /dev/disk/by-label/backup_disk does not exist

But right after execution, if I type on terminal:

mount /dev/disk/by-label/backup_disk /mnt/backup_disk/

Mount is fine without any kind of error.

What is causing the error when using the script?

EDIT: Solved adding a few seconds sleep like suggested in comments.

  • 2
    Have you tried putting a sleep 10 or so before the mount in your script? – Anthon May 4 '16 at 9:39
  • @Anthon that actually solved problem. Thanks. – realpclaudio Aug 22 '16 at 10:55

You should probably do some sort of delay loop (with a maximum iterations) that sleeps for 1 second, then checks whether the directory /dev/disk/by-label/backup_disk now exists. The following should work, but you may need to debug it.

set -o errexit
set -o unset

# do your reformat here
until [ -d "$DIRECTORY" ]; do 
  sleep 1 
# now continue with the mount and other operations...

I like to write defensive bash scripts. That's what the two "set -o" statements are for.

You can also add a counter to keep track of how many times you go through the loop and then break out after 30/60/90 seconds.


You should give the OS a few seconds to actually detect that the newly formatted partition is available. Start with a

sleep 10

before the line with mount .. (and maybe reduce that if you don't want to wait that long while running the script).

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.