How to effectively kill defunct child processes generated by esrally tool?

I am using the esrally tool, which generates a large number of child processes. I've noticed that some of these child processes become defunct. I am looking for an effective way to kill these defunct processes. Any suggestions or solutions would be greatly appreciated.

if I don't kill the defunct processes, esrally attempts to kill them every time it runs, resulting in significant time consumption, Sometimes it blocks forever。
Any suggestions on how to handle this situation effectively?

I've tried everything, but it doesn't work.

  1. two fork
(esrally race .....  & ) &
  1. pkill
#!/bin/bash
set -x
esrally $@ &
pid=$!
wait $pid
while pkill -P $pid; do sleep 1; done

Sometimes it blocks forever, very terrible!

edian cumulative merge throttle time across primary shards,,0.0,min^M
Max cumulative merge throttle time across primary shards,,0,min^M
Cumulative refresh time of primary shards,,10.526233333333332,min^M
Cumulative refresh count of primary shards,,15428,^M
Min cumulative refresh time across primary shards,,0,min^M
Median cumulative refresh time across primary shards,,0.0,min^M
Max cumulative refresh time across primary shards,,9.475216666666666,min^M
Cumulative flush time of primary shards,,0.0007833333333333334,min^M
Cumulative flush count of primary shards,,12,^M
Min cumulative flush time across primary shards,,0,min^M
Median cumulative flush time across primary shards,,0.0,min^M
Max cumulative flush time across primary shards,,0.0006333333333333333,min^M
Total Young Gen GC time,,0,s^M
Total Young Gen GC count,,0,^M
Total Old Gen GC time,,0,s^M
Total Old Gen GC count,,0,^M
Store size,,0.6287000393494964,GB^M
Translog size,,0.053181735798716545,GB^M
Heap used for segments,,0,MB^M
Heap used for doc values,,0,MB^M
Heap used for terms,,0,MB^M
Heap used for norms,,0,MB^M
Heap used for points,,0,MB^M
Heap used for stored fields,,0,MB^M
Segment count,,105,^M
Total Ingest Pipeline count,,0,^M
Total Ingest Pipeline time,,0,s^M
Total Ingest Pipeline failed,,0,^M
Min Throughput,bulk_data,217.27,docs/s^M
Mean Throughput,bulk_data,223.17,docs/s^M
Median Throughput,bulk_data,223.35,docs/s^M
Max Throughput,bulk_data,226.17,docs/s^M
50th percentile latency,bulk_data,2.6274919509887695,ms^M
90th percentile latency,bulk_data,4.151593893766403,ms^M
99th percentile latency,bulk_data,4.394316300749779,ms^M
99.9th percentile latency,bulk_data,5.896883897483616,ms^M
100th percentile latency,bulk_data,9.055107831954956,ms^M
50th percentile service time,bulk_data,2.6274919509887695,ms^M
90th percentile service time,bulk_data,4.151593893766403,ms^M
99th percentile service time,bulk_data,4.394316300749779,ms^M
99.9th percentile service time,bulk_data,5.896883897483616,ms^M
100th percentile service time,bulk_data,9.055107831954956,ms^M
error rate,bulk_data,0.00,%^M

2024-06-07 02:52:43,549 ActorAddr-(T|:42425)/PID:3406 esrally.metrics INFO Closing metrics store.
2024-06-07 02:52:43,550 ActorAddr-(T|:42425)/PID:3406 esrally.actor INFO Asking mechanic to stop the engine.
2024-06-07 02:52:43,552 ActorAddr-(T|:42425)/PID:3406 esrally.actor INFO Mechanic has stopped engine successfully.
2024-06-07 02:52:46,555 -not-actor-/PID:3383 esrally.rally INFO Attempting to shutdown internal actor system.
2024-06-07 02:52:46,557 -not-actor-/PID:3405 root INFO ActorSystem Logging Shutdown
2024-06-07 02:52:46,578 -not-actor-/PID:3404 root INFO ---- Actor System shutdown
2024-06-07 02:52:46,578 -not-actor-/PID:3383 esrally.rally INFO Actor system is still running. Waiting...
2024-06-07 02:52:47,579 -not-actor-/PID:3383 esrally.rally INFO Shutdown completed.