How I can I unmount a usbstick right from the script which does kill itself.

Because i'm running the script from the stick it's not possible to just unmount it because theres always the error: device or ressource busy. So what is the easiest way: Should I execute a script on the machine through my script right before it kills itself?

How can I accomplish this?

  • Do you need to run the script on the disk or is running it from the disk enough? Is there any reason to have an open shell session accessing files or directories on the disk? – terdon Dec 1 '15 at 14:42

you can't umount a filesystem if you are using it.

you might try

(echo fuser -ku /dev/xxx ; echo umount/dev/xxx ) |
at now +5 sec
  • this piece of code musn't be run from usb device of course.
  • pipe symbol (|) must be last char of the line.
  • I suppose you know what to use in place of xxx.

Run the umount command from a process that doesn't have any file open on the removable drive. In particular, this must not be a shell script on the drive. A script passed on the sh command line is fine. The script stored on the drive must have terminated; the easy way is to use exec to replace the shell running the script by the shell running the unmount sequence.

mount_point=$(df -P "$0" | awk 'NR==2 {print $6}')
… # do stuff involving the files on the drive
exec sh -c 'cd /; umount "$0"' "$mount_point"

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.