Verified Commit 200b3b16 authored by Fangyi Zhou's avatar Fangyi Zhou
Browse files

Fix an issue where processes are killed unexpectedly

Summary:
Fix #18

cd8fb2d0 introduced the trap on exits or
interrupts to kill the children processes. The original techniques uses SID
(Session ID), but didn't account for the issue that other processes may be
under the same session, which would get killed unexpectedly.

Considering that the new processes spawned by the script does not necessarily
belong to the same process group (sharing the same PGID). We use a variable to
store the PID of child processes and kill them at the trap.

Also add the missing untrap, which was missing due to an oversight in
d0073378

Signed-off-by: Fangyi Zhou's avatarFangyi Zhou <fangyi.zhou15@imperial.ac.uk>
parent ed4a537e
#!/bin/bash
set -e
SID=$(ps -p $$ --no-headers -o sid)
children=""
die () {
pkill -s $SID --signal SIGINT
kill $children
}
trap die EXIT
......@@ -106,8 +106,11 @@ do
if ! [[ $exclusions =~ $host ]]
then
process_output "$host" "$(timeout -k 0.1 $timeout ssh $ssh_opts $host "$command_to_execute" 2>/dev/null)" &
children="$children $!"
fi
done
wait >/dev/null 2>&1
popd > /dev/null
trap - EXIT
#!/bin/bash
set -e
SID=$(ps -p $$ --no-headers -o sid)
children=""
die () {
[ "$keep" = false ] && rm -f "$outputFile"
pkill -s $SID --signal SIGINT
kill $children
}
trap die EXIT
......@@ -122,6 +122,7 @@ if [ -t 1 ]; then
while true; do
printf 'Progress: %02d%%\r' \$(x=\$(grep '' -c "$outputFile" 2>/dev/null)00; expr \$x / $HOST_COUNT)
done" &
children="$children $!"
fi
for host in ${hostlist[*]}
......@@ -129,6 +130,7 @@ do
if ! [[ $exclusions =~ $host ]]
then
echo $(timeout -k 0.1 $timeout ssh -q -o StrictHostKeyChecking=no -o ConnectTimeout=$timeout $host "finger 2>/dev/null | tr -s ' ' | grep -E '$curr' | sed -n -E 's/(.*$curr).*/\1/p' | sort | uniq" 2>/dev/null) $host >> $outputFile &
children="$children $!"
fi
done
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment