LinuxQuestions.org
Review your favorite Linux distribution.
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 07-20-2009, 08:53 AM   #1
ccapone11
LQ Newbie
 
Registered: Sep 2008
Distribution: RHEL5
Posts: 8

Rep: Reputation: 0
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!!!
 
Old 07-20-2009, 09:20 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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.
 
Old 07-20-2009, 09:25 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Hello ccapone11
Quote:
Originally Posted by ccapone11 View Post
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 View Post
Does anyone know how I could just have this part of the script use that user?
Have a look at the su command.

Best

Charles
 
Old 07-20-2009, 09:53 AM   #4
ccapone11
LQ Newbie
 
Registered: Sep 2008
Distribution: RHEL5
Posts: 8

Original Poster
Rep: Reputation: 0
Quote:
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!
 
Old 07-20-2009, 10:13 AM   #5
Hobbletoe
Member
 
Registered: Sep 2004
Location: Dayton, Oh
Distribution: Linux Mint 17
Posts: 150

Rep: Reputation: 18
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.

You can check out the Advanced Bash Scripting Guide, specifically the section on Here Documents.
 
Old 07-20-2009, 10:28 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by Hobbletoe View Post
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.
 
Old 07-20-2009, 11:57 AM   #7
ccapone11
LQ Newbie
 
Registered: Sep 2008
Distribution: RHEL5
Posts: 8

Original Poster
Rep: Reputation: 0
Thank you all for the help! I am going to be rolling this out to UAT later this week and then Production. Wish me luck!!
 
Old 07-20-2009, 12:25 PM   #8
Hobbletoe
Member
 
Registered: Sep 2004
Location: Dayton, Oh
Distribution: Linux Mint 17
Posts: 150

Rep: Reputation: 18
@colucix

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.
 
Old 07-20-2009, 12:38 PM   #9
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
@Hobbletoe

Thank you. Indeed I had problems with spaces even in /bin/sh on Solaris. I will investigate a bit more, too.
 
  


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
Running Script on Startup jCash Linux - Newbie 3 05-20-2007 08:45 PM
Running a bash script at startup linker3000 Linux - Software 2 05-17-2007 10:00 PM
running .pl script on startup knowram Other *NIX 2 03-09-2007 09:50 PM
Running Perl Script upon startup andy7t Linux - Software 1 04-20-2005 04:43 PM
Running a script at systsem startup logo Linux - Newbie 1 09-21-2004 02:29 PM

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

All times are GMT -5. The time now is 01:46 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