I have following bash script which runs two process in parallel (two bash scripts internally), I need two bash scripts two run in parallel once both are finished I need total time execution. But the issue is the first bash script ./cloud.sh doesn't run but when I run it individually it runs successfully, and I am running main test bash script with sudo rights.
Test
#!/bin/bash
start=$(date +%s%3N)
./cloud.sh &
./client.sh &
end=$(date +%s%3N)
echo "Time: $((duration=end-start))ms."
Client.sh
#!/bin/bash
sudo docker build -t testing .'
Cloud.sh
#!/bin/bash
start=$(date +%s%3N)
ssh kmaster@192.168.101.238 'docker build -t testing .'
end=$(date +%s%3N)
echo "cloud: $((duration=end-start)) ms"
A background process won't be able to get keyboard input from you. As soon as it tries so, it will receive a SIGTTIN
signal which will stop it (until it is brought back to the foreground).
I suspect that one or both of your scripts asks you to enter something, typically a password.
Solution 1: configure sudo and ssh in order to make them password-less. With ssh this is easy (ssh key), with sudo this is a security risk. If docker build
needs you to enter something, you are doomed.
Solution 2: make only the ssh script (Cloud.sh
) password-less and keep the sudo script (Client.sh
) in foreground. Here again, if the remote docker build
needs you to enter something, this won't work.
How to wait for your background processes? Just use the wait
builtin (help wait
).
An example with solution 2:
#!/bin/bash
start=$(date +%s%3N)
./cloud.sh &
./client.sh
wait
end=$(date +%s%3N)
echo "Time: $((duration=end-start))ms."