Gaurav Kumar
Gaurav Kumar

Reputation: 13

Exit second command based on exit of first command in parallelly executed commands

I want to run a command which is resource intesive and use another command to dump hardware stats while first command is running. I ofcourse want to stop the second one when first exits.

E.g.

ping 8.8.8.8 & dstat -T -f temp.csv

Obviously ping is not intensive but you get the picture.

With the above command the first exits but second part keeps on running. I want dstat to stop when ping ends. Also, important that this happens in one liner command.

Upvotes: 0

Views: 26

Answers (1)

Barmar
Barmar

Reputation: 782498

Run dstat in the background, and save its PID in a variable. Then run ping in the foreground, and kill dstat when it finishes.

dstat -T -f temp.csv &
dstat_pid=$!
long_running_command
kill $dstat_pid

Upvotes: 0

Related Questions