Running part of startup script as a different user
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.
Running part of startup script as a different user
I am very new to Linux and apologize if this has been answered already, but I could not find it anywhere. I have been tasked with automating the reboot of our servers and making sure our application starts up automatically. I have created a CRON job for the reboot, so I am all set there. When the server reboots, I need to have a few different things start in a certain order DB-->Weblogic Admin Server-->Weblogic Managed Server. I know I can use a sleep command in between each to allow for enough time for each part to startup, but the issue I am having is that the DB needs to startup using the oracle user ID. Does anyone know how I could just have this part of the script use that user? My scripting skills are not the greatest either BTW, I am using RHEL5. Thank you for all of the help!!!
I know I can use a sleep command in between each to allow for enough time for each part to startup.
Safer and faster, if possible, to wait for each component to fully start. Ideally there's a form of the initiating command that does not return until the component is fully started -- or you may have a commands to test their status that you could run in a delayed loop until they are ready before moving on to the next one.
Quote:
Originally Posted by ccapone11
Does anyone know how I could just have this part of the script use that user?
You can try using su - and put the commands to run as different user in a "here document", for example
Code:
su - oracle << EOS
command here
another command here
another command here
more commands
EOS
Being root, the su command will not ask for oracle's password.
Thank you for all the help! Please pardon the stupid question, but what is EOS? And if I wanted to run another command as root after this one, would I just use "exit" to leave the oracle user session? Thanks again!
EOS in this example is simply a tag. When you use <<EOS, it says use the following as standard input until you hit that EOS tag. You can call it whatever you want. I generally use EOF or EOF1 if I have more than that.
Please note, if you put a space between the << and the tag, you will need to have that space on the tag line as well.
You will not need to 'exit' to get back to root. After the EOS tag line, you automatically exit the oracle account at that point and are back at root.
Please note, if you put a space between the << and the tag, you will need to have that space on the tag line as well.
Nope. You can't put leading spaces in the end tag. It will be an error. If you put an hyphen before the tag, you can use TABS to indent the code, but not spaces. For example:
Code:
su - oracle <<-LimitString
command
command
LimitString
The leading spaces before command and the closing LimitString are tabs.
I'm not sure that that is the case. I just did the following on a Red Hat Enterprise Linux 4 box (bash version 2.05b.0)
Code:
# ls -l
total 4
-rwx------ 1 root root 112 Jul 20 12:55 test.sh
# cat test.sh
#!/bin/bash
vi abc.txt <<EOF
iabcwq
EOF
vi def.txt << EOF1
idefwq
EOF1
vi ghi.txt << EOF2
ighiwq
EOF2
# ./test.sh
Vim: Warning: Input is not from a terminal
Vim: Warning: Input is not from a terminal
Vim: Warning: Input is not from a terminal
# ls -l
total 16
-rw-r--r-- 1 root root 4 Jul 20 12:56 abc.txt
-rw-r--r-- 1 root root 4 Jul 20 12:56 def.txt
-rw-r--r-- 1 root root 4 Jul 20 12:56 ghi.txt
-rwx------ 1 root root 112 Jul 20 12:55 test.sh
# cat abc.txt
abc
# cat def.txt
def
# cat ghi.txt
ghi
#
Please note that there is a 'Ctrl-V Esc:' before each wq. Those don't cat out well.
This would seem to say that blank spaces don't matter much for here documents. I think that I will look into this a bit more when I get the time. I will try the <<- thing though as it was a reason that I didn't particularly care for here documents because you couldn't indent them. Truthfully, I would have probably fixed this problem with another script called from the su line ... 'su - oracle -c script.sh'
I think I ran into my problem a few years ago on a Solaris box. It may have even have been a Bourne script that I was running at the time.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.