Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for systemd unit #32

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/Core/TcpService.pm
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,15 @@ 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);
return { 'status' => ['connection failed', 's'] };
}
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);
Expand Down
15 changes: 13 additions & 2 deletions resmon
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,6 +26,7 @@ GetOptions(
"d" => \$debug,
"f=s" => \$status_file,
"u" => \$update,
"r=s" => \$pid_file,
);

if ($update) {
Expand All @@ -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();
Expand Down Expand Up @@ -77,7 +79,16 @@ unless($debug) {
open(STDIN, "</dev/null");
open(STDOUT, ">/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 = [];
Expand Down
8 changes: 5 additions & 3 deletions resources/resmon-systemd.service
Original file line number Diff line number Diff line change
Expand Up @@ -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