LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   su as different user from shell script (http://www.linuxquestions.org/questions/linux-general-1/su-as-different-user-from-shell-script-4175448848/)

charithsrng 02-06-2013 05:54 AM

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!

druuna 02-06-2013 06:27 AM

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

charithsrng 02-07-2013 04:20 AM

Quote:

Originally Posted by druuna (Post 4885417)
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

druuna 02-07-2013 05:43 AM

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?

charithsrng 02-07-2013 05:49 AM

Quote:

Originally Posted by druuna (Post 4886122)
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.

charithsrng 02-07-2013 05:55 AM

Quote:

Originally Posted by charithsrng (Post 4886125)
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

druuna 02-07-2013 05:56 AM

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

charithsrng 02-07-2013 06:15 AM

Quote:

Originally Posted by druuna (Post 4886130)
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?

druuna 02-07-2013 06:22 AM

Quote:

Originally Posted by charithsrng (Post 4886136)
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).


All times are GMT -5. The time now is 07:26 AM.