1748226 Members
4553 Online
108759 Solutions
New Discussion юеВ

Re: script question

 
SOLVED
Go to solution
navin
Super Advisor

script question

Could you please let me know if i can use su -in the startup script as below.I need to use as su - wbs .Thanks Much

#!/bin/sh

if [ $# -ne 1 ]; then
echo "Usage: $0 {start|stop|restart}"
exit 1
fi

WBSBIN=/export/home/appl/abot/sw/wbs/wb07/bin:/export/home/appl/abot/sw/wbs/ws07/bin


case "$1" in
'start')
echo "Starting wbs"
sleep 8
/bin/su - wbs -c "$WBSBIN/wbs start"
;;
'stop')
echo "Stopping wbs"
/bin/su - wbs -c "$WBSBIN/wbs stop"
;;
esac
#
Learning ...
8 REPLIES 8
Victor BERRIDGE
Honored Contributor

Re: script question

Greetings,

Yes... if its root executing the script otherwise it will ask for the passwd and so the only alternative would be to use sudo and give that user the root priviledge to execute this script...

All the best
Victor
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: script question

Yes, you can be you will probably not get the results you want and the command may hang.

I know you want to use "su -" so that user wbs's .profile will be sourced and the environment is set. The problem is that on HP-UX, most .profile's have commands which are terminal-related and expect stdin to be a terminal --- which it isn't under rc.

There are two approaches that will work:
1) Edit .profile and surround commands like tset, tput, tabs with:
if [[ -t 0 ]]
then
tset ..
tput ..
fi

2) Don't use the "-" in the su command instead use "su wbc -c ...." and thus don't souce the .profile. I typically setup a special file (e.g. /usr/local/bin/wbsenv.sh) and let this file set and export any needed environment variables. I then let wbs's .profile and any rc'ed or cron'ed scripts source the file using the "." (dot) operator.
e.g.

. /usr/local/bin/wbsenv.sh
case "${1}" in
start)

This way, you change the environment variables in one place and all scripts work.
NOTE: Do not use an exit or return statement in the sourced file because this will exit the foreground process. The "." command does not spawn a child process but rather includes the file just as though it were part of the main script.
If it ain't broke, I can fix that.
Robert-Jan Goossens
Honored Contributor

Re: script question

Hi,

I don't know if you made a typo, but

$WBSBIN
will give you

/export/home/appl/abot/sw/wbs/wb07/bin:/export/home/appl/abot/sw/wbs/ws07/bin/wbs

I think this should be

WBSBIN=/export/home/appl/abot/sw/wbs/wb07/bin

Regards,
Robert-Jan
Victor BERRIDGE
Honored Contributor

Re: script question

And I would test if interactive or batch shell by adding a test of the sort in .profile of wbs:
if [[ $- = *i* ]]
then
#Inetracitve shell so set term etc...
stty erase "^H" kill "^U" intr "^C" eof "^D"
stty hupcl ixon ixoff opost
fi

All the best
Victor
navin
Super Advisor

Re: script question

I agree with the secnd approch ..but coupd help me with more detail.
Thanks
Learning ...
navin
Super Advisor

Re: script question

if i do not use the "-" as su - wbs , is the path is set to what's in /etc/profile?
Thanks
Learning ...
A. Clay Stephenson
Acclaimed Contributor

Re: script question

The PATH in rc is rather sparse and if you examine the parent script /sbin/rc you will find that PATH is set to /sbin. However, your sourced file can set or augment PATH in any way that you choose.
If it ain't broke, I can fix that.
Nitin Kumar Gupta
Trusted Contributor

Re: script question

Ofcourse you can use su -wbs

#!/bin/sh

if [ $# -ne 1 ]; then
echo "Usage: $0 {start|stop|restart}"
exit 1
fi

WBSBIN=/export/home/appl/abot/sw/wbs/wb07/bin:/export/home/appl/abot/sw/wbs/ws07/bin

case "$1" in
'start')
echo "Starting wbs"
sleep 8

su - wbs <
"$WBSBIN/wbs start"
exit
END
;;
'stop')
echo "Stopping wbs"

su - wbs <"$WBSBIN/wbs stop"
exit
END
;;
esac
#


You can easily see the format

su - < execute the commands with the user
exit

END

With best regards
-NKG-