bash script to check if mounted, and mount if not


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting bash script to check if mounted, and mount if not
# 1  
Old 03-03-2009
PHP bash script to check if mounted, and mount if not

I'd like to make a wrapper bash script that will make sure that an nfs mount is mounted before launching a program that depends on the mount being active. Basically:

1) Check to see if the mount is active
2) If it's not active, try to mount it
3) If it won't mount because the nfs server is down, wait 45 s then try again looping indefinitely until the nfs server is up
4) Once the mount is active, run a program and exit

Can someone help me out? Here is the line to the mount in question from my /etc/fstab

Code:
192.168.2.14:/share /mnt/share nfs defaults,auto,noatime 0 0

# 2  
Old 03-03-2009
Quote:
Originally Posted by graysky
1) Check to see if the mount is active
For this you could use 'grep'. But not for output, just the exit value (meaing "$?"). Check /proc/mounts. Mount a directory. Check /proc/mounts again. Try to grep the mounted partition line from /proc/mounts.


Quote:
Originally Posted by graysky
2) If it's not active, try to mount it
You know the mount command, right? So if the previous 'grep' fails you could use the exit value as cue to mount it. To test the mount bit you could use a command that always outputs exit value 1: 'false'. Test it with 'false || echo doSomething; false && echo doSomethingElse'. You'll see.


Quote:
Originally Posted by graysky
3) If it won't mount because the nfs server is down, wait 45 s then try again looping indefinitely until the nfs server is up
"Wait" here means 'sleep'. Try 'man sleep' to find out how to use it. Again you can use a "negative" (in shell) exit value.


Quote:
Originally Posted by graysky
4) Once the mount is active, run a program and exit
Same here: use the exit value.

Here's some Bash scripting guides that will help you glue those things together if you like: BASH Programming - Introduction HOW-TO, Bash Guide for Beginners,
Advanced Bash-Scripting Guide.

Once you have something to test out, posting it in code tags would make things easier in case things went awry somewhere.
# 3  
Old 03-03-2009
I figured I could just have it run 'mount | grep 192.168' and then check to see if that is present. I don't know how to pipe the output of that into a variable though.
# 4  
Old 03-03-2009
Quote:
Originally Posted by graysky
I figured I could just have it run 'mount | grep 192.168' and then check to see if that is present. I don't know how to pipe the output of that into a variable though.

To store the output of a command in a variable, use command substitution:

Code:
your_wish=$( my_command )

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to check if file systems are mounted

Hi I have the following piece of code, running on a solaris 10 O.S., that is not working for NFS file systems: for vol in `grep -E 'vxfs|ufs|nfs' /etc/vfstab | egrep -v '^#' | awk '{ print $3 }'` do if df -k $vol | grep $vol > /dev/null then outputOK "Filesystem: $vol mounted" else... (1 Reply)
Discussion started by: fretagi
1 Replies

2. Solaris

In Solaries 10 how to mount multiple volume on same mounted point

Hi , I am completely stuck and not getting any clue to come out this . So looking for help Q : I have salaries 10 in server with that Dell Equallogic storage connected. in dell Equlalogic in i have 70 TB storage . I created 7 volumes 10 TB each . In Solaries 10 i have syslog server i... (1 Reply)
Discussion started by: Roahn Tiwari
1 Replies

3. Shell Programming and Scripting

Bash script problem to mount

#!/bin/bash userid=$(id -u) options="rw,exec,users,uid="${userid}",gid="${userid}"" while ;do echo -n "Enter device you want to mount (Example: /dev/sda11): " read device echo "Selected device: ${device}" if ;then echo -n "Enter... (3 Replies)
Discussion started by: cola
3 Replies

4. Shell Programming and Scripting

How to check if a partition is mounted or not with bash?

How to check if a partition is mounted or not with bash? And when is $? variable one? Please give example. (10 Replies)
Discussion started by: cola
10 Replies

5. Shell Programming and Scripting

Shell script for Server Mount Point space check

Does anybody have anything I can use to help me out with this one? (4 Replies)
Discussion started by: lbone007
4 Replies

6. Shell Programming and Scripting

bash script to get all mounted devices

Hi, to all I'm writing script with zenity to benchmark selected disk with tools like; hdparm, seeker (to found in here > How fast is your disk? | LinuxInsight) With this piece of code i try to get all mounted devices to variables to use it with selection menu, but i stuck and don't know how... (5 Replies)
Discussion started by: m0z4rt
5 Replies

7. UNIX for Dummies Questions & Answers

mount points are already mounted

Hi, I have some issue with the mounting/unmounting on my sun solaris box. Actually their is one script that mount the file system take the backup of databases and unmount the file system.Last week this script failed to mount the file system with the below error message: + echo fs_check.sh:... (1 Reply)
Discussion started by: biju.mp
1 Replies

8. Solaris

Remote mount an already mounted nfs filesystem

Hello all, We're using JET to build our systems.. I'm in the process of needing to build a centrally located JET box with access to all our networks rather that 2 or 3 dotted around. Part of the means I need to locate the boot & OS images on an NFS mount (via NETAPP filer).. However in the... (1 Reply)
Discussion started by: itsupplies
1 Replies

9. SCO

/ partition is mounted but not present in mount

Hello , I 've got a problem with the root partition on my SCO 5.0.5 . When I check the disk with df or mount , I can 't see the root filesystem . # mount /stand on /dev/boot read only on Tue Sep 05 16:13:51 2006 /home on /dev/home read/write on Tue Sep 05 16:14:41 2006 But , if I try... (3 Replies)
Discussion started by: npn35
3 Replies

10. Filesystems, Disks and Memory

how to assign same mount point for file systems mounted on physical disks

We have 6 hard disks attached to the hardware. Of this 2 hard disks are of 9 GB each. Now I want combine both the same in such a way that i see a combined entry in the output of df -k . The steps I follow are 1. Create partition on hard disks (Using format partition) 2. Run newfs -v for... (6 Replies)
Discussion started by: Hitesh Shah
6 Replies
Login or Register to Ask a Question