SUMMARY: Running a command as another user.

From: David McCall (cyberman@sonoma.edu)
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
quotes.

/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:

ORA_HOME=/opt/oracle
ORA_OWNER=oracle

case "$1"
  'start')
      su - $ORA_OWNER -c $ORA_HOME/bin/dbstart
  ;;
  'stop')
      su - $ORA_OWNER -c $ORA_HOME/bin/dbshut
  ;;
esac

********************************************************************************

You can use osh "Operator Shell" to start a csh script as another user from
root.
I have forgeted the ftp site of them, but you can find it with xarchie or
archie.
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')

******************************************************

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

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

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

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

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

**************************************************

#!/bin/sh
su - oracle7 <<EOF
whatever_command_stops_oracle
EOF

***********************************************

#
# Startup for Oracle
#

ORACLE_HOME=/u01/oracle/7.1.6
ORACLE_OWNER=oracle

case "$1" in
'start')
        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 &
        fi
        ;;
'stop')
        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 &
        fi
        ;;
*)
        echo "Usage: /etc/init.d/oracle { start | stop }"
        ;;
esac
exit 0

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

David C. McCall / cyberman@sonoma.edu
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