Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Previously, I asked this question about how to suspend Linux after a set amount of time.

I would like to ask a similar question. Supposing I have a USB device attached to my system (OS = Fedora 13), are there commands that can:

  • detach the USB device and,

  • after detaching it, shutdown the system after a specified interval.

To be more precise, detaching means a command that safely removes the device.

share|improve this question
    
By "it should shutdown" do you mean that the computer should shut down or something else? –  Steven D Oct 11 '10 at 21:12
    
@StevenD: shutdown only! –  Chandrasekhar Oct 12 '10 at 5:06
    
I took the liberty of editing your question a bit. –  Steven D Oct 12 '10 at 5:28
    
sound weird to me, why do you need to unmount? what's wrong with just shutdown? –  phunehehe Oct 12 '10 at 10:46

2 Answers 2

up vote 4 down vote accepted

I assume by USB you mean a pendrive or external harddisk mounted to your file system. You "detach" this by unmounting the device. For that you will have to use the umount command. You can use the device or the mountpoint, for example:

umount /dev/sdb1 or umount /mnt/usb

See man umount for more details.

For shutting down your system, you use the shutdown command. -h will "Halt or power off after shutdown". The manpage says:

SYNOPSIS
       /sbin/shutdown [-akrhPHfFnc] [-t sec] time [warning message]

So you can use it to shutdown your system after a specific amount of time. The following command will halt your system after 30 minutes:

shutdown -h 30

Now you have one command which should only executed after the other one was succesfull. This is done with &&, shorthand for a conditional statement and a feature of your shell (Note: || exists also). The second command will only be executed if the first one returned without any errors. This is indicated by a return code of 0. For example:

umount /dev/sdb1 && shutdown -h 15 will detach your USB and halt your system after 15 minutes.

If this doesn't answer your question, please be more specific.

share|improve this answer
    
This answers my question thanks a lot! –  Chandrasekhar Oct 12 '10 at 5:06
    
You have given an answer where the system detachs USB Instantly. I am in need of the command where, USB Gets detached after a specific amount of time and the system shutsdown simulatenously –  Chandrasekhar Oct 12 '10 at 5:09
1  
@Chandru1 If you look at Giles answer to your last question(unix.stackexchange.com/questions/1759/fedora-auto-suspend) you should be able to use the same method he used there. For instance: sleep 15 && umount /dev/sdb1 && shutdown now –  Steven D Oct 12 '10 at 5:30

It sounds like you want your machine to shutdown automatically when you remove a USB pendrive.

I haven't done this myself, but the new Upstart service (which is supported in Fedora 9 onwards) does have the ability to run scripts based on an event. Here's an article discusses how an event can be triggered when how a hotplug device or when a USB printer is plugged in.

In theory, an event could also be generated when you unplug a USB device, and that event could call an arbitrary script , like shutdown -h now or lock the screen.

I've seen Windows and Mac systems which automatically lock the screen when a Bluetooth device leaves the proximity of the computer, and this is probably possible in Linux using Upstart.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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