[SOLVED] How to switch users & echo script outputs using shell script
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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"
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.
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
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.