SUMMARY: Running a command as another user.

From: David McCall (
Date: Tue Feb 06 1996 - 12:13:47 CST

The following were given as solutions: [the 1st being the easiest to
implement with success]

su - oracle7 -c '/oracle/oracle7/bin/dbstart'

su <username> -c "<cmd> options" if there are no options you don't need the

/bin/su user -c "command"

su oracleuser -c script This will change read, effective
and saved uid to oracleuser and
                                                               exec script
(the -c flag tells su to run 'this' instead of $SHELL).


Inside the script is:


case "$1"
      su - $ORA_OWNER -c $ORA_HOME/bin/dbstart
      su - $ORA_OWNER -c $ORA_HOME/bin/dbshut


You can use osh "Operator Shell" to start a csh script as another user from
I have forgeted the ftp site of them, but you can find it with xarchie or
create a special oracle-stopper user account. Then either edit
/etc/passwd to give them your script instead of a shell as their start-up
program (given in last field of /etc/passwd entries) or put your script
in the .login .profile or .cshrc file in the users home directory,
setting file permissions in such a way that the oracle-stopper user can't
modify their .profile

The first method would make it difficult for your script to send
any messages to the oracle-stopper, because they would have no shell.


rsh localhost -l oracle "$ORACLE_HOME/bin/dbstart"


su user -c /foo/bar/file

As an example, CNEWS runs a bunch of things out of root's crontab,
but they need to be run as the user "news". So there are lines like
this in root's cron:

10 8 * * * su news -c '/usr/lib/newsbin/maint/newsdaily'

This actually runs the script as if the user "news" was running it.
Just make sure /bin/csh is defined at the top of the script and it'll
run using csh.


Use sudo. (I assume this can be found with 'archie')


# Start Oracle database
ORACLEHOME=`ypcat passwd | grep oracle | awk -F: '{print $6}'`

if [ -f $ORACLEHOME/bin/dbstart ]; then
        su - oracle -c $ORACLEHOME/bin/dbstart &
        echo " starting Oracle database"

================================ cut here ==========================

# Stop Oracle database
ORACLEHOME=`ypcat passwd | grep oracle | awk -F: '{print $6}'`

if [ -f $ORACLEHOME/bin/dbshut ]; then
        echo " stopping Oracle database"
        su - oracle -c $ORACLEHOME/bin/dbshut


su - oracle7 <<EOF


# Startup for Oracle


case "$1" in
        if [ -f $ORACLE_HOME/bin/dbstart -a -d $ORACLE_HOME ] ; then
                echo "Starting Oracle server."
                su - $ORACLE_OWNER -c $ORACLE_HOME/bin/dbstart 1>/dev/null
2>&1 &
        if [ -f $ORACLE_HOME/bin/dbshut -a -d $ORACLE_HOME ] ; then
                echo "Shutting down Oracle server."
                su - $ORACLE_OWNER -c $ORACLE_HOME/bin/dbshut 1>/dev/null 2>&1 &
        echo "Usage: /etc/init.d/oracle { start | stop }"
exit 0

Thanks again to everyone who gave their input........

David C. McCall /
A. Systems Analyst
Sonoma State University
1801 E. Cotati Avenue
Rohnert Park, California 94928-3613
707-664-3099 / 2348

This archive was generated by hypermail 2.1.2 : Fri Sep 28 2001 - 23:10:53 CDT