Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a network attached storage (NAS) at home where I store pretty much all my media stuff (songs, videos, etc). I would like to mount it to a local directory in my notebook automatically every time I login at home so before mounting it I need to do a ping to see if it is answering (which means I'm home).

I already have the script to mount it (see below) but I have two questions:

1) How to use the answer from ping in order to see if its answering? I believe I have to use grep together with the ping command, but since I'm a new *nix user I'm not very fluent with it yet.

2) Where is the best place to put this script?

The mount command I have is this:

sudo mount -t cifs //myIpAddress/remoteDir /media/MyLibrary -o username=myUsername,password=myPassword,domain=myDomain,iocharset=utf8,file_mode=0777,dir_mode=0777


share|improve this question
up vote 1 down vote accepted

No need for grep or anything like that, just do this:

ping -c 4 foo && sudo mount ...

This basically uses the fact that ping returns true if the ping works, the -c limits it to 4 pings before returning. You can also do something if ping fails like this:

ping -c 4 foo || echo "FAILED!"

The other thing to consider is why you're mounting on login and not in your fstab, there are some good guides to mounting smb file storage online:

share|improve this answer
I thought that I needed to test to see if the server is online before mounting otherwise I would have problems. However, I just added the stuff in fstab and tested with wireless off or my NAS off and it worked pretty well. Thanks a lot! – tucaz Nov 16 '10 at 1:35
If you re-edit your question to be less about smb, we can make this question and answer more useful for future readers. There is already I think questions for smb mounting. – Martin Owens -doctormo- Nov 16 '10 at 3:34

Your Answer


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.