Operating System - HP-UX
1819882 Members
2660 Online
109607 Solutions
New Discussion юеВ

Run a command as another user??? please help

 
Michel Martineau
New Member

Run a command as another user??? please help

Hi im tryng to make a script but in the process i have to change use "su - oraweb"
but when i switch user the script stop unless i type exit to go back in root mode

All i want is to run that sort of script but the script stop after the su - oraweb goes in... won't do anything unless i do exit manualy

#!/bin/sh
su - oraweb
owsctl stop
owsctl stop -nodemgr
exit

kill -9 `pgrep oraweb`

su - oraweb
owsctl start -nodemgr &
owsctl start



Any help is apreciated thanks alot

-mIKE
7 REPLIES 7
Tim Nelson
Honored Contributor

Re: Run a command as another user??? please help

su - oraweb "owsctl stop;owsctl stop -nodemgr"
Steven E. Protter
Exalted Contributor

Re: Run a command as another user??? please help

shalom,

su - oracle -c "command"

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
spex
Honored Contributor

Re: Run a command as another user??? please help

Hi Mike,

If you really must run this script as different user instead of as oraweb, then I would recommended saving:

#!/bin/sh
owsctl stop
owsctl stop -nodemgr
exit 0

and:

#!/bin/sh
owsctl start -nodemgr &
owsctl start
exit 0

as scripts, say /scripts/a.sh and /scripts/b.sh.

You may then run these scripts like this:

#!/bin/sh
su - oraweb -c "/scripts/a.sh"
kill -9 `pgrep oraweb`
su - oraweb -c "/scripts/b.sh"
exit 0

PCS
DCE
Honored Contributor

Re: Run a command as another user??? please help

The general syntax is su - userid -c "command"

or in this case su - oraweb -c "owsctl stop"

consider making the series of commands a script and calling the script via the su command.

A. Clay Stephenson
Acclaimed Contributor

Re: Run a command as another user??? please help

... and I would beat someone over the head with a baseball bat for using a kill -9 routinely.

kill -9 `pgrep oraweb`

The correct procedure is to kill -15 ${PID}, kill -1 ${PID}, kill -2 ${PID}, kill -3 ${PID}, kill -11 ${PID}, and finally and only if necessary, kill -9 ${PID} ---- in that order and doing a kill -0 ${PID} between each command and then testing the result; if 0, the process still exists. Doing a kill -9 can leave temporary files, shared memory, and other resources dangling because no cleanup is possible.


Also, pgrep is not a standard command so you may need to craft a substitute.
If it ain't broke, I can fix that.
Michel Martineau
New Member

Re: Run a command as another user??? please help

Wow thanks alot for all the help.. ill work on my script today and test it!!! Very apreciated :P Thanks
rmueller58
Valued Contributor

Re: Run a command as another user??? please help

create the script and run it using sudo

sudo -u username command