Skip to content

Instantly share code, notes, and snippets.

@gizlu
Last active June 19, 2022 14:33
Show Gist options
  • Save gizlu/ef2919ea1aa455f159694f8753e5cdec to your computer and use it in GitHub Desktop.
Save gizlu/ef2919ea1aa455f159694f8753e5cdec to your computer and use it in GitHub Desktop.
simple, "smart" child kiling scheme. Perl snippet copy pasted from 20120122 release of GNU parallel.
# Copyright (C) 2007,2008,2009,2010,2011,2012 Ole Tange and Free Software
# Foundation, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>
# or write to the Free Software Foundation, Inc., 51 Franklin St,
# Fifth Floor, Boston, MA 02110-1301 USA
sub kill {
# kill the jobs
my $self = shift;
my @family_pids = $self - > family_pids();
# Record this jobs as failed
$self - > set_exitstatus(1);
# Send two TERMs to give time to clean up
for my $signal("TERM", "TERM", "KILL") {
my $alive = 0;
for my $pid(@family_pids) {
if (kill 0, $pid) {
# The job still running
kill $signal, $pid;
$alive = 1;
}
}
# Wait 200 ms between TERMs - but only if any pids are alive
if ($signal eq "TERM" and $alive) {
::usleep(200);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment