summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2012-03-14 23:23:17 +0100
committerPaul Buetow <paul@buetow.org>2012-03-14 23:23:17 +0100
commit2dc22d1efeb18c910c000c45b0366b6b5ec6cecc (patch)
tree690c7f5ec7e4ed56991daee458f296dbbeae4a5b
parentabdb55511c99ba087b7d55185c163c8dde366706 (diff)
instant process shutdown
-rw-r--r--CHANGELOG3
-rwxr-xr-xloadbars30
2 files changed, 29 insertions, 4 deletions
diff --git a/CHANGELOG b/CHANGELOG
index 3733304..1d19cbb 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,3 +1,6 @@
+* Dropped FreeBSD support / focus is Linux
+* Instant terminating of sub-processes on shutdown
+
Sat Feb 25 20:09:02 CET 2012
* Release v0.5.1
* Add config file support (~/.loadbarsrc) and it's possible to configure
diff --git a/loadbars b/loadbars
index 393cdfe..30a182b 100755
--- a/loadbars
+++ b/loadbars
@@ -22,6 +22,8 @@ use SDL::Font;
use Time::HiRes qw(usleep gettimeofday);
+use Proc::ProcessTable;
+
use threads;
use threads::shared;
@@ -77,7 +79,7 @@ my %I : shared;
factor => 1,
height => 230,
maxwidth => 1280,
- samples => 500,
+ samples => 1000,
showcores => 0,
showmem => 0,
showtext => 1,
@@ -189,11 +191,12 @@ sub stats_thread ($;$) {
my ( $host, $user ) = @_;
$user = defined $user ? "-l $user" : '';
- my ($sigusr1, $quit) = (0,0);
+ my ($sigusr1, $sigterm) = (0,0);
my $loadavgexp = qr/(\d+\.\d{2}) (\d+\.\d{2}) (\d+\.\d{2})/;
my $inter = INTERVAL;
+ my $pid;
- for (;;) {
+ until ($sigterm) {
my $bash = <<"BASH";
loadavg=/proc/loadavg
stat=/proc/stat
@@ -213,7 +216,7 @@ BASH
? $bash
: "ssh $user -o StrictHostKeyChecking=no $C{sshopts} $host '$bash'";
- my $pid = open my $pipe, "$cmd |" or do {
+ $pid = open my $pipe, "$cmd |" or do {
say "Warning: $!";
sleep 3;
next;
@@ -221,6 +224,8 @@ BASH
# Toggle CPUs
$SIG{USR1} = sub { $sigusr1 = 1 };
+ $SIG{TERM} = sub { $sigterm = 1 };
+
my $cpuregexp = qr/$I{cpuregexp}/;
# 1=cpu, 2=mem, 3=net
my $mode = 0;
@@ -260,10 +265,23 @@ BASH
# TODO: Use index instead of regexp for cpuregexp
$cpuregexp = qr/$I{cpuregexp}/;
$sigusr1 = 0;
+ } elsif ($sigterm) {
+ last;
}
}
}
+ my $proc_table = Proc::ProcessTable->new();
+ for my $proc (@{$proc_table->table()}) {
+ if ($proc->ppid == $pid) {
+ display_info "Terminating PID $proc->ppid";
+ kill 'TERM', $proc->pid if $proc->ppid == $pid;
+ }
+ }
+
+ display_info "Terminating PID $pid";
+ kill 'TERM', $pid;
+
return undef;
}
@@ -434,6 +452,10 @@ sub main_loop ($@) {
display_info 'Toggled number/hostname display';
} elsif ( $key_name eq 'q' ) {
+ display_info 'Shutting down sub-processes';
+ $_->kill('TERM') for @threads;
+ sleep 1;
+ display_info 'Shutting down done';
$quit = 1;
return;