tags:

views:

71

answers:

4

I'm writing a bash script which calls a lot of other scripts.
Several scripts have to be executed as user_1 but several ones as user_2.
The scripts should be called in strict sequence. I start my script as user_1 and use su many times to become user_2. These times su requires a password so I have to retype it many times. I'd like to avoid that, but su has no password parameter.
sudo and expect are not installed.

+1  A: 
  • Make a fork (using '&') for user_1 and for user_2. They should communicate (similar as client-serwer) using 'lock file' (semaphore). For example user_1 doing something and user_2 checking is 'lock file' exist in while ...sleep loop

  • Maybe that is stupid, but you can use ssh user@localhost end keys

That was also my first idea and it would be surely a greatsolution. I found it too risky. It could cause problems in the future when the script became more comlex.
Attila Zobolyak
+1  A: 

If you happen to have zsh, I think you can do this easily - something like:

#!/bin/zsh
coproc user1_script
su user2
do_stuff
echo "your_turn" >&p
read MY_TURN
do_more_stuff
echo "your_turn" >&p
read MY_TURN
...

user1_script:

#!/bin/zsh
read MY_TURN
do_stuff
echo your_turn
read MY_TURN
do_more_stuff
...

If you're stuck, and have zsh, it's worth a try anyway.

Tony
Bash 4 and ksh also have coprocesses.
Dennis Williamson
+1  A: 

You could run as root. Sounds risky? Not if you are careful and precede each command with su and add the username like so:

su -c 'script1' user_1 && su -c 'script2' user_2

This will (assuming you start as root) change the user to user_1 before running script1 then back to root then change to user_2 to run script2.. all without asking for multiple passwords.

Martin Thomas
Sounds good, but my script has to run on a company server. The root password is known only for the system admin.
Attila Zobolyak
Can you give some more detail about why you need the two users? You have the passwords for both.. Could you perhaps chown the results files at the end?
Martin Thomas
This will be an install script. We are using 2 different tools for our appliacation. Both tool needs to be installed as special user. This rule is required by the producer of tools. The components of the tools are built on each other. That means I have to install the first tool than start some components. Next I have to install the 2. tool, start other components from the first tool, do some other operations. This is the rough scenario.
Attila Zobolyak
A: 

I have solved the problem using ssh. I have generated the authentication key for user_1 and published to user2. Password is not needed any more.

ssh -l user_1 hostname  "command_1; command_2"
Attila Zobolyak