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.

- 739
- 2
- 10
- 15
4 Answers
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

- 46
- 2
-
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 Sep 10 '10 at 11:42
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.

- 102,968
- 15
- 177
- 252
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.

- 6,835
- 3
- 28
- 42
-
Sounds good, but my script has to run on a company server. The root password is known only for the system admin. – Attila Zobolyak Sep 10 '10 at 11:36
-
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? – Spaceghost Sep 10 '10 at 15:36
-
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 Sep 13 '10 at 08:50
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"

- 739
- 2
- 10
- 15