LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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, 07: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, 07: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: 108
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, 01: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, 01:40 PM   #4
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
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.
 
1 members found this post helpful.
Old 06-22-2015, 01:51 PM   #5
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Have a look at http://serverfault.com/questions/444...different-user
 
Old 06-22-2015, 02: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, 02: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, 02: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



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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

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
Open Source Consulting | Domain Registration