LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How we can run script for muliple home(users) (https://www.linuxquestions.org/questions/linux-newbie-8/how-we-can-run-script-for-muliple-home-users-4175424396/)

karan4141 08-28-2012 08:24 AM

How we can run script for muliple home(users)
 
Hi All,

I am very new to Linux scripting. I m trying to write one script to start/stop our SAP application . Below is my scenrio
1:- I have 4 instances running on same physical (diffrent virtual box) box

Below is the sample script which i have devloped for one instance

#!/sbin/sh

#

# Start / stop SAP

case "$1" in

"start")

su - oraepn -c "lsnrctl start LISTENER_EPN"

su - epnadm -c "startsap"

;;

"stop")

su - epnadm -c "stopsap"

su - oraepn -c "lsnrctl stop LISTENER_EPN"

;;

*)

echo "Usage: $0 { start|stop }"

exit 1

;;

esac

exit 0

The above script is working fine with one insatce call EPN .
Now the question is how can i add my other instance in this script for EX:- EPP,EPX (application owner "eppadm" & "epxadm")

MensaWater 08-28-2012 09:24 AM

Since you say what you have is working I'm assuming it is running as root?

If the current "su - c" commands are working and the script is exiting when done you should be able to just add additional "su -c" commands below the ones you have and specify the alternate users.


Code:

#!/sbin/sh

#

# Start / stop SAP

case "$1" in

"start")

su - oraepn -c "lsnrctl start LISTENER_EPN"

su - epnadm -c "startsap"

su - oraepp -c "lsnrctl start LISTENER_EPP"

su - eppadm -c "startsap"

su - oraexp -c "lsnrctl start LISTENER_EXP"

su - expadm -c "startsap"

;;

"stop")

su - epnadm -c "stopsap"

su - oraepn -c "lsnrctl stop LISTENER_EPN"

su - eppadm -c "stopsap"

su - oraepp -c "lsnrctl stop LISTENER_EPP"

su - expadm -c "stopsap"

su - oraexp -c "lsnrctl stop LISTENER_EXP"

;;

*)

echo "Usage: $0 { start|stop }"

exit 1

;;

esac

exit 0

In above I've assumed your naming is consistent. A couple of caveats.

1) sh is often a symbolic link to other shells (e.g. bash or dash). You might want to make your interpreter line specify the shell you expect (the one linked to) rather than the symbolic link - that way if you put it on another system later it is clear which shell you want.

2) If any of your su commands hung above then the next ones wouldn't execute. For that reason you might want to have separate scripts that did just the listener start and SAP start or stop for each instance and call those scripts from within this master script putting an ampersand (&) at the end of each line. The & tells it to run the command in the background rather than waiting for it to complete. The reason I suggest separate scripts called from master is you want to start two processes (listener and SAP) rather than one for each instance. If you did & after each individual of those then they might not start in the order you want. Presumably you care about whether listener or SAP starts first but don't care which instance starts first.

3) You don't have the commands being output to a log. Often it is a good idea to do that. If you add something like ">> /var/log/eppstart.log 2>&1" to the end of your epp start commands then you'll capture any output from those commands into the file and it will help you debug failure if any occurs. If you did this then you'd put a final & after the redirects to do the backgrounding discussed above.

4) Start/Stop implies you're doing this as an init script for automatic stop/start at boots. Be aware that often init scripts have different requirements to run automatically at boot then they do at command line. You should do test boots to verify they work now as opposed to waiting for a system crash later to find out they don't if you intend to use them for automatic stop/start for boots. The syntax needed for init scripts can very with your distribution and version of Linux.

szboardstretcher 08-28-2012 09:34 AM

Nice job MensaWater, I'm giving you rep just for the sheer Volume of information you helped with.

Just wondering if OP was asking how to integrate "nested case"? So they can run a command like...

Code:

blah start thing-1
blah start thing-2
blah stop thing-1


karan4141 08-28-2012 09:53 AM

Hi MensaWater,

Thanks for REply!!

My script is working fine now.

Now as per your point 2 if i have to call sript only for one system call EPP from master script than can you please guide me how can i put all needed steps in master script.

Please also let me know how can i reward you points for this help.

MensaWater 08-28-2012 10:12 AM

Basically you'd create your 3 individual scripts to look very much like your original so that each starts only a single instance (SAP and Listener) of course using the different su commands for each as appropriate to the instance. Call them say, epn_init.sh, exp_init.sh and epp_init.sh. You'd then modify your mast to have:

Code:

#!/sbin/sh

#

# Start / stop SAP

case "$1" in

"start")

epp_init.sh start >>/var/log/epp_start.log 2>&1  &

exp_init.sh start >>/var/log/exp_start.log 2>&1  &

epn_init.sh start >>/var/log/epn_start.log 2>&1  &

;;

"stop")

epp_init.sh stop >>/var/log/epp_stop.log 2>&1  &

exp_init.sh stop >>/var/log/exp_stop.log 2>&1  &

epn_init.sh stop >>/var/log/epn_stop.log 2>&1  &

;;

*)

echo "Usage: $0 { start|stop }"

exit 1

;;

esac

exit 0

To start all 3 instances you runt he master script.

To start only epp you'd then run the "epp_init.sh start" rather than the master script.

To give points you can just click on Yes for finding this post useful or you can kick on the little scales symbol and say you approve.

karan4141 08-29-2012 04:38 AM

Hi MensaWater,

I realy appricate your help!!

I will make one master script (Start_stop_SAP.sh) for all system like EPP, EPX,EPN..etc & also i will make single script for each system ex:- start_stop_EPP.sh.

Now my concern is how i integrate all these single script with master script. I want to make script like below

If i want to start EPP than i execute "./Start_stop_sap.sh stop/start EPP" than only EPP stop/start.

karan4141 08-29-2012 10:38 AM

Can anyone please guide me to devlop this script

MensaWater 08-29-2012 12:21 PM

You can have two variables at command line which will be $1 and $2.

In the script set those to new variables near the start of the script (after the interpreter line of course).
ACTION=$1
INST=$2

You change your existing case for "in $ACTION" instead of "in $1".

You can nest a new case within each of the actions (start and stop) and give the options for that case (e.g.)
case $INST in
EPP|epp) ...
;;
EXP|exp) ...
;;
EPN|epn) ...
;;
ALL|all) ...
;;
*) Usage...
esac

Within ALL you do what the current script does. Within the other options you do only the two su commands to start the Listener and SAP Application for that instance. The UPPER|lower format allows the user to enter the instance name (or all) in either upper or lower and get the same results.

karan4141 08-30-2012 06:54 AM

Hi MensaWater,

Thannks for help!!

As per your guidance. I have made changes in my script but it is giving me syntax error. For example i have done only for one command. Please have a look if it is correct or need to be modify.

#!/bin/sh

#

# Start / stop SAP

Action=$1
Inst=$2

case "$Action" in

"start")

case "$Inst" in
"PIN")
su - pinadm -c "startsap r3"

;;

"BPN")
su - bpnadm -c "startsap r3"

;;

"ECN")
su - ecnadm -c "startsap r3"

;;

"ALL")
su - pinadm -c "startsap r3"
su - bpnadm -c "startsap r3"
su - ecnadm -c "startsap r3"
;;
*)





"stop")


case "$Inst" in
"PIN")
su - pinadm -c "stopsap r3"

;;

"BPN")
su - bpnadm -c "stopsap r3"

;;

"ECN")
su - ecnadm -c "stopsap r3"

;;

"ALL")
su - pinadm -c "stopsap r3"
su - bpnadm -c "stopsap r3"
su - ecnadm -c "stopsap r3"
;;
*)

echo "Usage: $0 { start|stop }"

exit 1

;;

esac

karan4141 08-30-2012 07:51 AM

Hi MeansWater,

Thanks for your help!!

My script is working now. I forget to write *) & esac end of the second case statment.

Now i am working on this script to make more efficative.


All times are GMT -5. The time now is 05:20 AM.