Latest LQ Deal: Latest LQ Deals
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 11-07-2005, 08:05 AM   #1
Registered: Feb 2003
Location: Boston, MA
Distribution: RedHat 9
Posts: 39

Rep: Reputation: 15
Check to see if a drive is mounted


Is there a way (command or script) that the system can automatically check to see if a drive is mounted ?

I have an external USB drive that functions as a back-up drive where everything gets copied over to once per day via a "CP" command that is fired off by cronjobs.

Here is my cronjobs line:

45 02 * * 1-5 cp -ru --reply=y /mnt/share /media/usbdisk

Is there a way the system can verify the USB drive is mounted prior to running the "CP" job and if not, cancel the copy process?

Thanks for the advice.

Old 11-07-2005, 08:24 AM   #2
Registered: Oct 2005
Location: Oxford, UK
Distribution: Debian Etch (w/ dual-boot XP for gaming)
Posts: 282

Rep: Reputation: 31
The mount command with no arguments will show all filesystems that are mounted. So, you could issue mount on its own, grep the output for say, "/media/usbdisk", and then count the number of lines in the output. 0 means it's not mounted, 1 means it is.

As a little extra, the && operator between commands actually means "return the result of the first command ANDed with the result from the second command"; but since its lazy, what it means in practice (especially if you discard its return) is that it will execute the second command only if the first one returned successfully. You could thus have your cron job running "check_mounted && cp...", which would bypass the copying if the check_mounted command didn't return successfully.

Last edited by Dtsazza; 11-07-2005 at 08:28 AM.
1 members found this post helpful.
Old 11-07-2005, 08:56 AM   #3
Registered: Feb 2003
Location: Boston, MA
Distribution: RedHat 9
Posts: 39

Original Poster
Rep: Reputation: 15
Thanks for the response.

Can you please expand on the check_mounted command? I'm assuming it's a script.

If so, and I understand you, the check_mounted file should look like this....
mount | grep /media/usbdisk

Yes or No?

How do I make the check_mounted file a function or utility file that executes?

Thanks again.

P.S. Again, I'm very new at this (but Im learning) so I appoligize if I'm not following simple suggestions.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash-Check If FS is Already Mounted On Directory fortezza Programming 15 04-27-2013 07:06 PM
drive mounted noobtesting Mandriva 3 10-05-2004 07:51 PM
script check to see if device is mounted Warmduvet Programming 14 09-03-2004 06:17 PM
Boot disk; check. CD in drive; check. Doesn't work; check. Hal DamnSmallLinux 7 02-04-2004 02:10 AM
Check if it IS mounted sourceman Linux - General 11 02-27-2002 08:31 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:08 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration