LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 02-06-2013, 04:54 AM   #1
charithsrng
LQ Newbie
 
Registered: Jan 2013
Posts: 19

Rep: Reputation: Disabled
su as different user from shell script


Hi Guys,

I have an issue with my shell script.my requirement is run a specific command as separete user and execute rest as root.
here is ma script

su applclone
grep "adstpall.sh:Exiting with status 0" /home/applclone/error_log.txt
if [ $? -eq 0 ]
then
echo "apps down"
fi
exit


when running this i cant see the output unless i enter
exit command manually.Pls help!
 
Old 02-06-2013, 05:27 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
When using just su <username> a shell is started, which isn't automatically closed.

Have a look at su's -c flag:

Code:
su - applclone -c "grep \"adstpall.sh:Exiting with status 0\" /home/applclone/error_log.txt
if [ $? -eq 0 ]
then
  echo \"apps down\"
fi"
It might be a better idea to put the commands in a script an start the script with the -c option (then there's no need to escape the " ):
Code:
su - applclone -c /full/path/to/script

Last edited by druuna; 02-06-2013 at 05:32 AM.
 
Old 02-07-2013, 03:20 AM   #3
charithsrng
LQ Newbie
 
Registered: Jan 2013
Posts: 19

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by druuna View Post
When using just su <username> a shell is started, which isn't automatically closed.

Have a look at su's -c flag:

Code:
su - applclone -c "grep \"adstpall.sh:Exiting with status 0\" /home/applclone/error_log.txt
if [ $? -eq 0 ]
then
  echo \"apps down\"
fi"
It might be a better idea to put the commands in a script an start the script with the -c option (then there's no need to escape the " ):
Code:
su - applclone -c /full/path/to/script
I used below command as you sujjest bt it also prompts for password.but if i enter su applclone in terminal it wont prompts for password. Is there a way i can avoid this?

su - applclone -c /ex_apps_shut.sh
 
Old 02-07-2013, 04:43 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
In your first post you mention this:
Quote:
my requirement is run a specific command as separete user and execute rest as root.
I do assume that all is done as root user and that the specific user is the su - user -c .... part. In that case you do not need the password (root is all powerful).

I did wonder why a check to see if an application is still running needs to be done as a specific user (why not root).

Anyway: Who is the owner of the script (is it executable by applclone) and what is inside it?
 
Old 02-07-2013, 04:49 AM   #5
charithsrng
LQ Newbie
 
Registered: Jan 2013
Posts: 19

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by druuna View Post
In your first post you mention this: I do assume that all is done as root user and that the specific user is the su - user -c .... part. In that case you do not need the password (root is all powerful).

I did wonder why a check to see if an application is still running needs to be done as a specific user (why not root).

Anyway: Who is the owner of the script (is it executable by applclone) and what is inside it?
Thanks a lot for you replys.
this script owns to root and needs to run by root itself.Only one command of script should run as applclone.
this is what i wanted to do exactly.
 
Old 02-07-2013, 04:55 AM   #6
charithsrng
LQ Newbie
 
Registered: Jan 2013
Posts: 19

Original Poster
Rep: Reputation: Disabled
Thumbs up

Quote:
Originally Posted by charithsrng View Post
Thanks a lot for you replys.
this script owns to root and needs to run by root itself.Only one command of script should run as applclone.
this is what i wanted to do exactly.
Finally It works for me with grep command.i'll try with my other commands.
Thanks alot for your timely help
 
Old 02-07-2013, 04:56 AM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
I don't think you understood what I mentioned in the above replies.

In your first post you show part of a script, this part:
Code:
su applclone
grep "adstpall.sh:Exiting with status 0" /home/applclone/error_log.txt
if [ $? -eq 0 ]
then
echo "apps down"
fi
exit
The commands used in the above (the bold and italic parts) should be put in a separate script and that script should be called from the original script with the su - applclone -c /full/path/to/separate.script
 
Old 02-07-2013, 05:15 AM   #8
charithsrng
LQ Newbie
 
Registered: Jan 2013
Posts: 19

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by druuna View Post
I don't think you understood what I mentioned in the above replies.

In your first post you show part of a script, this part:
Code:
su applclone
grep "adstpall.sh:Exiting with status 0" /home/applclone/error_log.txt
if [ $? -eq 0 ]
then
echo "apps down"
fi
exit
The commands used in the above (the bold and italic parts) should be put in a separate script and that script should be called from the original script with the su - applclone -c /full/path/to/separate.script
Got your point.I have created my script as below
#/bin/bash
su - applclone -c ". /ebiz1/QAL/apps/apps_st/appl/QALNEW_ct-eagorasvr1.env
.$ADMIN_SCRIPTS_HOME/adstpall.sh apps/apps > /home/applclone/error_log.txt
sleep 300
grep \"adstpall.sh:Exiting with status 0\" /home/applclone/error_log.txt
if [ $? -eq 0 ]
then
echo \"apps down\"
fi"
echo "run as root"


If i run above script as root it will run all the commands in double quotes as applclone and only echo "run as root" will run as root.Am i correct?
 
Old 02-07-2013, 05:22 AM   #9
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Quote:
Originally Posted by charithsrng View Post
Got your point.I have created my script as below
#/bin/bash
su - applclone -c ". /ebiz1/QAL/apps/apps_st/appl/QALNEW_ct-eagorasvr1.env
.$ADMIN_SCRIPTS_HOME/adstpall.sh apps/apps > /home/applclone/error_log.txt
sleep 300
grep \"adstpall.sh:Exiting with status 0\" /home/applclone/error_log.txt
if [ $? -eq 0 ]
then
echo \"apps down\"
fi"
echo "run as root"


If i run above script as root it will run all the commands in double quotes as applclone and only echo "run as root" will run as root.Am i correct?
Yes, correct (not entirely, but to keep things simple it is).
 
  


Reply

Tags
linux users, shell script


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do i change to super user then revert back to ordinary user ,using shell script? wrapster Solaris / OpenSolaris 6 03-18-2009 03:37 AM
shell script for many user add cmontr Programming 1 03-03-2008 10:19 AM
Shell script user input tuckermaddox Linux - Newbie 5 08-12-2004 03:14 AM
Shell script that changes user davholla Linux - General 2 03-23-2004 09:18 PM
shell script user not ok slam Linux - General 4 07-24-2003 06:40 AM


All times are GMT -5. The time now is 08:56 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration