Operating System - HP-UX
1847253 Members
2998 Online
110263 Solutions
New Discussion

Shell script for multiple users or multiple su commands.

 
Gulam Mohiuddin
Regular Advisor

Shell script for multiple users or multiple su commands.

I would like to make one shell script which will be run by root and su to four different users to execute the following commands in sequence on the same server.

For user1 user2 user3 user 4 run following commands after setting up environment variables:

psadmin -p kill -d $PS_DOMAIN
psadmin -c shutdown -d $PS_DOMAIN
stopWebLogic.sh PIA
stopWebLogic.sh

Thanks,

Gulam.
Everyday Learning.
4 REPLIES 4
Pete Randall
Outstanding Contributor

Re: Shell script for multiple users or multiple su commands.

Something like this:

for USER in user1 user2 user3 user4 do
su - $USER -c myscript
done

Where myscript contains the commands you wish to have run.


Pete

Pete
Pete Randall
Outstanding Contributor

Re: Shell script for multiple users or multiple su commands.

Sorry, make that:


for USER in user1 user2 user3 user4
do
su - $USER -c myscript
done


Pete

Pete
Shannon Petry
Honored Contributor

Re: Shell script for multiple users or multiple su commands.

Logically the same thing, but maybe easier to follow.

for USER in user1 user2 user3 user4 ; do
su - $USER -c 'psadmin -p kill -d $PS_DOMAIN ; psadmin -c shutdown -d $PS_DOMAIN ; stopWebLogic.sh PIA ; StopWebLogic.sh'
done

Microsoft. When do you want a virus today?
Sandman!
Honored Contributor

Re: Shell script for multiple users or multiple su commands.

Yet another way of doing the same thing:

# echo "user1\nuser2\nuser3\nuser4" | xargs -n1 -i su - {} -c Script.sh

...where Script.sh contains

psadmin -p kill -d $PS_DOMAIN
psadmin -c shutdown -d $PS_DOMAIN
stopWebLogic.sh PIA
stopWebLogic.sh