Operating System - HP-UX
1827460 Members
4128 Online
109965 Solutions
New Discussion

Script running root but one command need to be run with another user

 
SOLVED
Go to solution
Leo The Cat
Regular Advisor

Script running root but one command need to be run with another user

Hi

I've a script running with root to execute some root commands and in this same script some commands needs to be run under another user.

My script:
1. root command
2. root command
3. here user2 command
4. root command
5. root command
6. here user2 command


I need to run 3 and 6 with the user2 because some processes will be started and the owner of the process must be user2 !

How to do this ?
note: I don't want two scripts.

Is it possible ?

Regards
Den
9 REPLIES 9
James R. Ferguson
Acclaimed Contributor

Re: Script running root but one command need to be run with another user

Hi Den:

For your non-root tasks, you can do:

# su - someuser -c "some_command;another_command"

Regards!

...JRF...
Mel Burslan
Honored Contributor

Re: Script running root but one command need to be run with another user

did you try this ?

1. root command
2. root command
3. su - user2 -c "command1; command2; command3"
4. root command
5. root command
6. su - user2 -c "command4; command5; command6"

since you are root to start running this script, there should not be any problem su'ing into user2 account without a password.

Hope this helps
________________________________
UNIX because I majored in cryptology...
SKR_1
Trusted Contributor

Re: Script running root but one command need to be run with another user

You can make the entry in your script to switch over to perticular user.

su - username -c "command to be executed"

You can refer manpages for su

Thanks

SKR
Leo The Cat
Regular Advisor

Re: Script running root but one command need to be run with another user

a little problem

In my script I've try with:
su - oracle -c "$ORACLE_HOME/mycommand.ksh"

The problem is that $ORACLE_HOME is not defined and of course I've the error: ksh: /mycommand.ksh: not found.

How to fix this. I Need to work with ORACLE_HOME to have only one generic script ...

Regards
Den
Heironimus
Honored Contributor

Re: Script running root but one command need to be run with another user

Using double quotes means that the variable $ORACLE_HOME is interpolated by the (root) shell running the script instead of the shell started by su as oracle. If you have ORACLE_HOME defined in the oracle user's .profile or something similar then you can enclose the command in single quotes or escape the dollar sign with a backslash.
TTr
Honored Contributor
Solution

Re: Script running root but one command need to be run with another user

If you only have one ORACLE_HOME in this server, you can set it in the root's environment. If you have several oracle versions installed, you need to calculate the value of the "ORACLE_HOME" based on the ORACLE_SID that you are executing the script against. You should use the /etc/oratab to grep the line with the correct SID and then pick-up the ORACLE_HOME from there.

In addition to ORACLE_HOME and depending what the "su - oracle" command does, you may also need to setup the TNS_ADMIN, SHLIB_PATH, LD_LIBRARY_PATH variables.
Leo The Cat
Regular Advisor

Re: Script running root but one command need to be run with another user

I can't do this. (Admin Unix don't want!)
I need to have this.

Is it possible to load the user profile with something like that (but this does not work), so just in the idea :

su - applopus -c "(. /home/applopus/.profile >/dev/null; $ORACLE_HOME/backup_restore/bkp_restore.sh -m backup_instance_online)"

(huh!)

Regards
Den
OldSchool
Honored Contributor

Re: Script running root but one command need to be run with another user

"Is it possible to load the user profile with something like that (but this does not work), so just in the idea :

su - applopus -c "(. /home/applopus/.profile >/dev/null; $ORACLE_HOME/backup_restore/bkp_restore.sh -m backup_instance_online)"

Try it with single quotes (as noted previously) , and note that running the .profile in the command string isn't necessary here, as the "-" in the su causes that to happen.
Leo The Cat
Regular Advisor

Re: Script running root but one command need to be run with another user

It's ok with su - user and quote syntax !
Thanks