3

Let's assume we have two shell scripts running in parallel

driver.sh:

./proc1.sh&
./proc2.sh&

how can I terminate a process if the other one exited with non-zero code? (terminate driver.sh)

1

Using GNU Parallel it looks like this:

parallel --halt now,fail=1 ::: script1.sh script2.sh
0

Unless you have some logic inside wither process to control the other upon some status change, you need to track/control them using PIDs:

#!/bin/bash
./proc1.sh &
pid_1=$!  ## PID of proc1.sh

./proc2.sh &
pid_2=$!  ## PID of proc2.sh

while ps -p ${pid_1} &>/dev/null; do :; done ##Checking for existence of proc1.sh
kill ${pid_2}  ## Killing proc2.sh when proc1.sh is not running anymore
  • Thanks but this I guess does not check for non-zero exit status of process 1 and terminates the proc2 even if proc1 exited correctly. The question is to terminate proc2 if proc1 terminated with non-zero status. – Mahyar Apr 11 '17 at 15:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.