I want to run multiple dd commands in background, but be able to see the status.
I have the following script.sh:
#!/usr/bin/env bash
for drive in $@
do
echo "Wiping $drive"
dd if=/dev/zero of=$drive status=progress &
done
wait
echo "Done."
Which results in the following output:
$ sudo bash ./script.sh /dev/sda /dev/sdb
Wiping /dev/sda
Wiping /dev/sdb
288788992 bytes (289 MB, 275 MiB) copied, 10 s, 28.9 MB/s 14404864 bytes (114 MB, 109 MiB) copied, 4 s, 28.6 MB/s
Is there any way how to output the respective dd statuses below the drive paths? For example:
$ sudo bash ./script.sh /dev/sda /dev/sdb
Wiping /dev/sda
288788992 bytes (289 MB, 275 MiB) copied, 10 s, 28.9 MB/s
Wiping /dev/sdb
14404864 bytes (114 MB, 109 MiB) copied, 4 s, 28.6 MB/s
I tried various redirects, named pipes etc. but wasn't able to achieve such (or similar) output.
I tried the coprocesses approach which seems to be the way to go, but now I'm unable to make it work with the for cycle.
This works fine:
coproc dd_sda { dd if=/dev/zero of=/dev/sda status=progress 2>&1; }
echo "sda PID: $dd_sda_PID"
coproc dd_sdb { dd if=/dev/zero of=/dev/sdb status=progress 2>&1; }
echo "sdb PID: $dd_sdb_PID"
sda PID: 12494
./wipe.sh: line 86: warning: execute_coproc: coproc [12494:dd_sda] still exists
sdb PID: 12496
However this:
for drive in sda sdb
do
coproc_name=dd_${drive}
coproc $coproc_name { dd if=/dev/zero of=/dev/$drive status=progress 2>&1; }
pid_var="${coproc_name}_PID"
echo "$drive PID: ${!pid_var}"
done
doesn't work for the second coprocess:
sda PID: 12759
./wipe.sh: line 39: warning: execute_coproc: coproc [12759:dd_sda] still exists
sdb PID:
When hardcoding the name using if condition, it also works:
for drive in sda sdb
do
coproc_name=dd_${drive}
if [[ "$drive" == 'sda' ]]
then
coproc dd_sda { dd if=/dev/zero of=/dev/$drive status=progress 2>&1; }
elif [[ "$drive" == 'sdb' ]]
then
coproc dd_sdb { dd if=/dev/zero of=/dev/$drive status=progress 2>&1; }
fi
pid_var="${coproc_name}_PID"
echo "$drive PID: ${!pid_var}"
done
sda PID: 12998
./wipe.sh: line 39: warning: execute_coproc: coproc [12998:dd_sda] still exists
sdb PID: 13000
Here is my final solution, massively inspired by @Diego Torres Milano (thanks again).
Which results in such output: