LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Shell script to mount SAN (https://www.linuxquestions.org/questions/linux-newbie-8/shell-script-to-mount-san-4175429043/)

linux_new_bie 09-25-2012 10:31 PM

Shell script to mount SAN
 
Hi experts,
Greetings.
I am a newbie in linux and i have to write a shell script to include the mount command to mount the SAN.
I dont know how to run the command as super user.Can anyone please help me by providing the shell script to do so.
Actually the shell script should check whether SAN is mounted and if not mount the SAN

Sample;
mount /dev/sda1 /iscsi/vol01

This is the command .I dont know how to include it in shell script and put it in cron .So the shell script should not ask for superuser password at runtime.

Please help me by giving your valuable suggestions and ideas.

Thanks in advance.

fernfrancis 09-26-2012 12:57 AM

Hi User

you can start with something like this .
create a file enter the code below and give it executable permission


#!/bin/bash
FS="/dev"

if cat /proc/mounts | grep -F "$FS" > /dev/null; then
echo " Filesystem is mounted"
else
echo " Filesystem is not mounted"
fi

linux_new_bie 09-26-2012 01:24 AM

So many Thanks for your reply.

i think this code only checks whether it is mounted or not .
I want code to mount it if the one is not mounted.
Please help me to provide the code to mount it .

thanks in advance .

sackboy 09-26-2012 01:35 AM

You should be creating this script as the super user (root).

If you insist on doing it as an unprivileged user, you'll need to:
1) allow passwordless sudo access to the command you want to run
2) create a script that runs the mount command through sudo

Why do you want to mount using an unprivileged user anyway?

fernfrancis 09-26-2012 01:43 AM

I agree with sackboy,

why do you need to mount using a unprivileged user?
execute the above script
just add the mount command to it that all i guess it should work fine, if nay issue post back

linux_new_bie 09-26-2012 01:47 AM

I want to put the script in cron so that it will run automatically.So i want to include the mount command as sudo command .
But i dont know how to implement it.

fernfrancis 09-26-2012 02:00 AM

which operating system are you using

If you execute the cron job as root user u do not need to add sudo

linux_new_bie 09-26-2012 02:21 AM

Red hat Linux OS .

Is it? If i add to the crontab of root user then i no need to run as sudo.

linux_new_bie 09-26-2012 02:24 AM

So can i have my code as below

#!/bin/bash
FS="/dev/sda1"
if cat /proc/mounts | grep -F "$FS" > /dev/null; then
echo " Filesystem is mounted"
else
mount /dev/sda1 /iscsi/vol01
fi

and add this script to root user crontab ?


Thanks in advance for all experts.

fernfrancis 09-26-2012 02:26 AM

yes!

login as root and add the job to its crontab it will mount at the specified time

if you want to auto mount on boot then add the mount point to fstab file located at /etc/fstab, please provide the exact parameters there

if you find this helpful please "Rep"

linux_new_bie 09-26-2012 02:29 AM

I think this will work bcos i tried in development server .I have to try in production server.

Thanks for your assistance.

fernfrancis 09-26-2012 02:41 AM

no problem ....

you can add me on gmail for quick assistance

linux_new_bie 09-26-2012 03:39 AM

Can i have your email id please .Thanks

fernfrancis 09-26-2012 04:20 AM

thanx

linux_new_bie 09-26-2012 04:36 AM

Thank you .I just send you an intro mail.


All times are GMT -5. The time now is 08:54 AM.