1

I want to create a script that runs two programs. That part's simple, but there's a catch: I want to kill the second one if the first one exits. How can I do that?

Edit

I tried killing the program by it's PID after the other one exits, but the program is hamster-time-tracker, which is a Python application that exits immediately, apparently spawning another process. How can I get around this? Is there some way to get the other PID spawned?

Edit 2

Figured it out. I had to run python /usr/bin/hamster-time-tracker instead of hamster-time-tracker, and it stayed running.

Jonah
  • 169
  • 2
  • 9

2 Answers2

5
#!/bin/bash

cmd-a &
a=${!}

cmd-b &
b=${!}

wait $a
kill $b

I used yes a and yes b as commands when testing this.

wfaulk
  • 6,878
  • 7
  • 46
  • 75
3

There's wait command in the bash to wait for termination of first application and then kill second app.

Since waits are inserted automatically after commands not ending with &, the right order may save you from going into manual wait trouble:

A &
B          # waits
kill $!    # then kills A
yrk
  • 2,487
  • 17
  • 22
OJ278
  • 95
  • 1
  • 6