Operating System - Linux
1753797 Members
7879 Online
108799 Solutions
New Discussion юеВ

Re: Script with different user

 
simon_164
Super Advisor

Script with different user

I am searching for a script that will run a command under a different user. which means i want the script to do an su - prdadm and then to run the script /usr/.../stopsap and then to exit. how to do this? does it exist, by the way i want to use it for a sap system with data protector.
7 REPLIES 7
Ivan Ferreira
Honored Contributor

Re: Script with different user

You can install and use SUDO. Once you installed, you can enable users to run commands in behalf other users. For exammple:

# visudo

dpuser ALL = (prdadm) NOPASSWD: /usr/../stopsap


Then, dpuser can run:

sudo -u prdadm -H /usr/../stopsap. In this way, the dpuser will run the script as prdadm
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
KapilRaj
Honored Contributor

Re: Script with different user

su - -c "/path/to/the/script/script>"
Nothing is impossible
DCE
Honored Contributor

Re: Script with different user



If you are doing it from the root id:
su - prdadm -c "/usr/.../stopsap

If you want to do it from another id, the you will have to use sudo

simon_164
Super Advisor

Re: Script with different user

This is not what i am looking for, since i want to log in to the user prdadm because of the paths and variables, i tried the
su - prdadm -c '...' but it always hangs before the user logs in and i need to press enter so that it will enter and do its job.
A. Clay Stephenson
Acclaimed Contributor

Re: Script with different user

Your problem is the "su - prdadm -c ..." rather than simply "su prdadm -c ...". I know that you want to setup the environemt by sourcing prdadm's .profile but that very .profile has commands like tset, tput, tabs, ... that expect stdin to be a terminal-like device --- which it ain't in this case. There are 2 approaches:

1) Surround all of these commands in .profile with:
if [ -t 0 ]
then
tput ..
tabs ..
fi
2) better:
Create a file that sets and exports any needed environment variables e.g. /usr/local/bin/prdadm_env.sh
and let both your su'ed script AND prdadm's .profile source this file using the "." dot operator.

e.g. . /usr/local/bin/prdadm_env.sh

This sourced file should not contain any exit or return statement because this file becomes part of the foreground process rather than a child process.
If it ain't broke, I can fix that.
Dennis Handly
Acclaimed Contributor

Re: Script with different user

>I tried the su - prdadm -c '...' but it always hangs before the user logs in and I need to press enter

Can you redirect stdin to /dev/null so it won't bother reading?
simon_164
Super Advisor

Re: Script with different user

The problem was with the user login and i correct it.