LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 06-22-2015, 08:08 AM   #1
rajthampi
Member
 
Registered: May 2008
Location: Kuwait
Distribution: RHEL5, Ubuntu, PSlinuxOS
Posts: 35

Rep: Reputation: 1
Question How to switch users & echo script outputs using shell script


Hi guys
I want to build a shell script like following

#!/bin/sh
# Change user to applmgr
su - applmgr
#change to a exported directory
cd $D
#run a script to stop the apps instance
./adstpall.sh apps/apps
#echo the script outputs to the user
#check whether a particular process called FNDLBR has no running processes
#then switch to another user called oracle
su- oracle
#change to a exported directory
cd $D
#run a script to shutdown listener process
./addlnctl.sh stop PROD
#Check whether the listener process is successfully terminated
#then shutdown the database
./addbctl.sh stop immediate
#Check the status and echo
echo "All Done"

Please help

regards,
 
Old 06-22-2015, 08:26 AM   #2
translator1111
Member
 
Registered: Jun 2010
Location: Slovakia
Distribution: Debian 8, Ubuntu 10.04 and 12.04; SLAX 6.0; ConnochaetOS 0.9.; LFS; Natty chip: VT1708S
Posts: 93
Blog Entries: 2

Rep: Reputation: 7
Dear rajthampi,
you have already built the script, what kind of help are you looking for?
Regards,
M.
 
Old 06-22-2015, 02:22 PM   #3
rajthampi
Member
 
Registered: May 2008
Location: Kuwait
Distribution: RHEL5, Ubuntu, PSlinuxOS
Posts: 35

Original Poster
Rep: Reputation: 1
Smile

Quote:
Originally Posted by translator1111 View Post
Dear rajthampi,
you have already built the script, what kind of help are you looking for?
Regards,
M.
Hello there
My problem is, I am running this script as root, the su - switches the user, however unless I execute exit, rest of the commands like switching to the script directory doesn't happen.

What I want to do is, switch the user, then switch the directory.

Regards and thanks
 
Old 06-22-2015, 02:40 PM   #4
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,260

Rep: Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948
That's because you need the "-c" flag in su, which tells it to switch users and run a command. You may need to split the script up into a few different parts, one per user, and call them with -c. So the main script would look like:
Code:
#!/bin/sh

su applmgr -c "script_2.sh $D"
su oracle -c "script_3.sh $D"
echo "All Done"
then script_2.sh:
Code:
#!/bin/sh
cd $1
./adstpall.sh apps/apps
and script_3.sh:
Code:
#!/bin/sh
cd $1
./addlnctl.sh stop PROD
./addbctl.sh stop immediate
Or some variant of that.
 
Old 06-22-2015, 02:51 PM   #5
Habitual
LQ Addict
 
Registered: Jan 2011
Location: Youngstown, Ohio
Distribution: LM17.1/Xfce4.11.8
Posts: 7,190
Blog Entries: 10

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Have a look at http://serverfault.com/questions/444...different-user
 
Old 06-22-2015, 03:20 PM   #6
rajthampi
Member
 
Registered: May 2008
Location: Kuwait
Distribution: RHEL5, Ubuntu, PSlinuxOS
Posts: 35

Original Poster
Rep: Reputation: 1
Thumbs up

Quote:
Originally Posted by suicidaleggroll View Post
That's because you need the "-c" flag in su, which tells it to switch users and run a command. You may need to split the script up into a few different parts, one per user, and call them with -c. So the main script would look like:
Code:
#!/bin/sh

su applmgr -c "script_2.sh $D"
su oracle -c "script_3.sh $D"
echo "All Done"
then script_2.sh:
Code:
#!/bin/sh
cd $1
./adstpall.sh apps/apps
and script_3.sh:
Code:
#!/bin/sh
cd $1
./addlnctl.sh stop PROD
./addbctl.sh stop immediate
Or some variant of that.
Hi suicidaleggroll

Thank you very much. I'll sure try the examples provided by you & post the results.

regards,
 
Old 06-22-2015, 03:22 PM   #7
rajthampi
Member
 
Registered: May 2008
Location: Kuwait
Distribution: RHEL5, Ubuntu, PSlinuxOS
Posts: 35

Original Poster
Rep: Reputation: 1
Thumbs up

Quote:
Originally Posted by Habitual View Post
Hello Habitual

I've gone through the above provided link, yet was somehow obsessed with doing "everything" using a single .sh file. I think it is too much for a newbie like me to demand in very beginning itself

Anyway thank you very much the response.

regards,
 
Old 06-25-2015, 03:49 PM   #8
rajthampi
Member
 
Registered: May 2008
Location: Kuwait
Distribution: RHEL5, Ubuntu, PSlinuxOS
Posts: 35

Original Poster
Rep: Reputation: 1
Thumbs up

Hello guys

Finally I cooked up something, that just does my job. I used some codes from serverfault.com thread in addition to the suggestions received here.

Here comes the script
Code:
#!/bin/sh

su - applprod -c "/u01/applprod/PROD/inst/apps/PRODBAK_erp-prodbak/admin/scripts/adstpall.sh apps/apps"

#http://stackoverflow.com/questions/7...h-shell-script


# PROCESS_NUM=$(ps -ef | grep "$1" | grep -v "grep" | wc -l)

PROCESS_NUM=$(ps -ef | grep "FNDLIBR" | wc -l)

echo "$PROCESS_NUM Concurrent Programs are running right now, you should wait for the jobs to finish prior shutting down the database"



check_process() {
  echo "$ts: checking $1"
  [ "$1" = "" ]  && return 0
  [ `pgrep -n $1` ] && return 1 || return 0
}


while [ 1 ]; do 
  # timestamp
  ts=`date +%T`
  echo "$ts: begin checking..."
  check_process "FNDLIBR"
  
 # [ $? -eq 0 ] && echo "$ts: not running, restarting..." && `dropbox start -i > /dev/null`
	[ $? -eq 0 ] && echo "$ts: No more concurrent programs running." && break
  sleep 60
done


echo "All Pending Concurrent Programs are closed now, going ahead with Database Shutdown"

su - oraprod -c "/u01/oraprod/PROD/db/tech_st/10.2.0/appsutil/scripts/PRODBAK_erp-prodbak/addlnctl.sh stop PRODBAK"
su - oraprod -c "/u01/oraprod/PROD/db/tech_st/10.2.0/appsutil/scripts/PRODBAK_erp-prodbak/addbctl.sh stop immediate"

echo "Database shutdown completed! All Done!!"
Thank you all!

regards,
 
  


Reply


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
What is the @Echo Off alternative for a shell script? replica88 Linux - Newbie 10 10-01-2012 01:09 PM
shell script, which should diff between ps outputs? m4rtin Programming 3 02-18-2010 08:29 AM
Requesting shell script that only outputs directories that contain subdirectories cyrilic Programming 4 01-08-2010 10:26 AM
How do i switch users in a shell script? koobi Programming 8 01-05-2008 03:19 PM
crontabbed shell script, trying to echo/cat something zaubara Programming 2 06-13-2004 08:18 PM


All times are GMT -5. The time now is 09:38 PM.

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