diff --git a/lib/Core/TcpService.pm b/lib/Core/TcpService.pm index 30ed1b1..996a122 100644 --- a/lib/Core/TcpService.pm +++ b/lib/Core/TcpService.pm @@ -99,7 +99,7 @@ sub handler { connect($h, $s); $c->add($h); my ($fd) = $c->can_write($timeout); - if ($fd == $h) { + if (defined($fd) && (fileno($fd) == fileno($h))) { my $error = unpack("s", getsockopt($h, Socket::SOL_SOCKET, Socket::SO_ERROR)); if ($error != 0) { close($h); @@ -107,7 +107,7 @@ sub handler { } print $h $config->{'prepost'}."\r\n" if ($config->{'prepost'}); ($fd) = $c->can_read($timeout); - if ($fd == $h) { + if (defined($fd) && (fileno($fd) == fileno($h))) { chomp($banner = <$h>); print $h $config->{'post'} if ($config->{'post'}); close($h); diff --git a/resmon b/resmon index fd78b85..ac04326 100755 --- a/resmon +++ b/resmon @@ -13,7 +13,7 @@ use POSIX qw( :sys_wait_h setsid ); use Getopt::Long; use Data::Dumper; use vars qw($config_file $debug $status_file $interface $port $config -$status $update); +$status $update $pid_file); use Resmon::Config; use Resmon::ExtComm; @@ -26,6 +26,7 @@ GetOptions( "d" => \$debug, "f=s" => \$status_file, "u" => \$update, + "r=s" => \$pid_file, ); if ($update) { @@ -42,6 +43,7 @@ sub configure { $config->{statusfile} = $status_file if($status_file); $config->{port} = $port if($port); $config->{interface} = $interface if($interface); + $config->{pidfile} = $pid_file if ($pid_file); } configure(); @@ -77,7 +79,16 @@ unless($debug) { open(STDIN, "/dev/null"); open(STDERR, ">/dev/null"); - fork && exit; + my $pid = fork; + if ($pid) { + if ($config->{pidfile}) { + open(my $pidfh, '>', $config->{pidfile}) or + die "can't open >$config->{pidfile}: $!"; + print $pidfh $pid; + close $pidfh or die "can't close $config->{pidfile}: $!"; + } + exit; + } } my $list = []; diff --git a/resources/resmon-systemd.service b/resources/resmon-systemd.service index 4da1e1c..2bc61e0 100644 --- a/resources/resmon-systemd.service +++ b/resources/resmon-systemd.service @@ -5,8 +5,10 @@ After=network.target [Service] Type=forking -ExecStart=/opt/resmon/resmon -Restart=always +ExecStart=/opt/resmon/resmon -r /run/resmon.pid +Restart=on-failure +PIDFile=/run/resmon.pid +ExecReload=/bin/kill -HUP $MAINPID [Install] -WantedBy=multy-user.target +WantedBy=multi-user.target