Operating System - HP-UX
1834551 Members
4357 Online
110069 Solutions
New Discussion

su - user -c "script" As root but needs manual exit command to run script

 
SOLVED
Go to solution
Sammy_2
Super Advisor

su - user -c "script" As root but needs manual exit command to run script


I am testing the script below so that I can put it in rc scripts. It
su - peter
but stops at the command line then when I type
exit the script gets executed:
what is preventing it from su - peter and then
executing /usr/bin/appstart without my intervention.

# ./COG start
$ exit
(script runs now)

============excerpt of the script name:COG===========
#!/sbin/sh

APP_START="/usr/bin/appstart"
APP_ID="peter"
case "$1" in

'start')
if [ -x "$APP_START" ]; then
su - ${APP_ID} -c "$APP_START"

fi
good judgement comes from experience and experience comes from bad judgement.
5 REPLIES 5
Jeff Schussele
Honored Contributor

Re: su - user -c "script" As root but needs manual exit command to run script

Hi Sam,

You need as
esac
to terminate the case statement.

Rgds,
Jeff
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
Pete Randall
Outstanding Contributor
Solution

Re: su - user -c "script" As root but needs manual exit command to run script

First suggestion:

Don't use the minus sign with su. Put what ever environment variables are required in the script. You'll have a lot less difficulty with terminal oriented commands interfering with your results.


Pete

Pete
Mark Grant
Honored Contributor

Re: su - user -c "script" As root but needs manual exit command to run script

Two interesting things here.

The first is that you are using /sbin/sh instead of /bin/sh. I don't know what the difference is between these two but on my system,they are definately different. Tis may have some kind of impact.

The second thig is that after your "./COG start" which you seem to run as root, you get a "$" prompt back. This would suggest to me that the "su" is actually working but that "appstart" itself has got the problem or possibly peters .profile or /etc/.profile. Instead of typing exit, try "id" and see who you are.
Never preceed any demonstration with anything more predictive than "watch this"
Sammy_2
Super Advisor

Re: su - user -c "script" As root but needs manual exit command to run script

Jeff,
Actually, I have esac in the end.just did not paste the bottom of the script. Thanks
Pete,
I took your suggestion and did just that. Now it works. Guess, I was trying for a shortcut.Thanks a lot.
Mark,
The appstart was working fine if I run it as peter. After su, if id shows peter.
Thanks. The easiet way was to hard code the ENV VAR. in the script. The /sbin/sh and /bin/sh does not make a diff in my case.
Thanks to all. Jef, Pet, Mar
good judgement comes from experience and experience comes from bad judgement.
Michael Schulte zur Sur
Honored Contributor

Re: su - user -c "script" As root but needs manual exit command to run script

Hi Jeff,

I assume, the case and the rest is just out of post, the script would complain about a nomatch or so.

Hi Mark,

if I am not mistaken then /sbin/sh is linked static and /bin/sh is linked dynamic.

Hi Sam,

I am glad, that it worked out,

be happy,

Michael