Operating System - HP-UX
1830899 Members
3484 Online
110017 Solutions
New Discussion

Re: How to successfully su another user and run his scripts

 
SOLVED
Go to solution
yalin zhao
Advisor

How to successfully su another user and run his scripts

Hi friends,

I want to write a simple script with following purpose:

I want User A su User B
User A will run a script in User B's directory.

$ ll test
-rwxrwxrwx 1 ssprod sybase 86 Nov 8 14:07 test


This is what I wrote in a file "test"

"su - UserB -c /UserB/octacom/sqllist.sh
When I run it, nother happens. But If I run manually by su UserB and run it in UserB's diectory, it will work.
Can anyone give me suggestions?

Thanks

Yalin
12 REPLIES 12
Christopher McCray_1
Honored Contributor

Re: How to successfully su another user and run his scripts

Hello,

Try this instead:

su - UserB -c "/UserB/octacom/sqllist.sh
Hope this helps

Chris

It wasn't me!!!!
Jeff Schussele
Honored Contributor

Re: How to successfully su another user and run his scripts

Hi,

The quotes need to surround the -c command string - not the entire command.

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

Re: How to successfully su another user and run his scripts

Why not just change the permissions on the script?

Pete

Pete
yalin zhao
Advisor

Re: How to successfully su another user and run his scripts

I changed it to the following:

su - UserB -c "/UserB/octacom/sqqlist.sh" su - UserB -c "/UserB/octacom/sqqlist.sh
none of them works

Note: I changed the permission of the scripts
to 777.
the "Any suggestions or any other way that we can do this?


Ken Hubnik_2
Honored Contributor

Re: How to successfully su another user and run his scripts

su - userid. The - set the environment for that user. runs .profile
Pete Randall
Outstanding Contributor

Re: How to successfully su another user and run his scripts

If the permissions are 777, then why do you need to su to User B? User A can run the script as himself.

Pete

Pete
Martin Johnson
Honored Contributor

Re: How to successfully su another user and run his scripts

AFAIK, the password must be entered interactively. I tried this a couple of months back and was not able to get it to work. My work around was to submit an "at" job instead.

HTH
Marty
Christopher McCray_1
Honored Contributor

Re: How to successfully su another user and run his scripts

Another thing,

Are you calling this pwd file by it's absolute path or are you just calling the file name?

You either have to do this:


su - UserB -c "/UserB/octacom/sqllist.sh
or

PWD=/full/path/to/pwd
.
.
.
su - UserB -c "/UserB/octacom/sqllist.sh <$PWD"

Hope this helps

Chris
It wasn't me!!!!
Jeff Schussele
Honored Contributor

Re: How to successfully su another user and run his scripts

You're not going to be able to pass the password to the su command that way. I suggest you take Pete's advice & set the perms such that UserA can run the script.

Your other option would be for UserB to setup a .rhosts file that gives UserA equivalency rights to UserB.

Rgds,
Jeff
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: How to successfully su another user and run his scripts

You have a more fundamental problem. The su - rather than simply su is killing you. You want to source the user's .profile but almost certainly .profile expects an interactive (i.e. a terminal for stdin). Commands like stty, tset, and tabs will hang if stdin is not a tty device.

There are two ways to make this work:
1) surround any of the commands which deal with the terminal with
if [ -t 0 ]
then
tset ....
stty ....
tabs ...
fi

-t 0 is true if stdin (0) is a terminal device.

2) better
have a file that both .profile and your script sources (e.g) . /usr/local/bin/myenv.sh

myenv.sh would then set and export any needed vars.

There should be no exits or returns in this file.
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: How to successfully su another user and run his scripts

I just saw where you want a non-root user to be able to do this. NO way - it would be an unbelieable security hole if possible. What you can do is setup a sudo command. You can get sudo from:
http://gatekeep.cs.utah.edu/hppd/hpux/Sysadmin/sudo-1.6.6/

If it ain't broke, I can fix that.
Gerhard Roets
Esteemed Contributor

Re: How to successfully su another user and run his scripts

I use that su method on bootup in my rc scripts. I have a special Daemon user. Who runs network daemons i.e the cognos license manager. I do not want to run them as root, and the way described above is the way I do it with the only exception being that i nohup it.

/bin/nohup /bin/su - phlmgrd -c '/opt/cognos/license1/lmgrd /opt/cognos/license1/lmgrd.log2> /opt/cognos/license1/lmgrd.err & '

Regards
Gerhard