package Drweb Daemon use strict sub new shift my self bless self- path

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package Drweb::Daemon;
use strict;
sub new {
shift;
my $self = bless {};
($self->{'path'}, $self->{'pidfile'}, $self->{'init_script'}) = @_;
return $self;
}
sub version {
my $self = shift;
my $version = `$self->{'path'} -v`;
if ($version =~ /version\s+([\d\.\-]+)/) {
return $1;
}
}
sub __pid {
my $self = shift;
open PID, $self->{'pidfile'} || return 0;
my $pid = <PID>;
close PID;
return $pid;
}
sub status {
my $self = shift;
my $pid = $self->__pid();
if ($pid && kill(0, $pid)) {
return 1;
} else {
unlink($self->{'pidfile'});
return 0;
}
}
sub start {
my $self = shift;
return (1, _('Daemon is already running')) if $self->status();
warn(`$self->{'init_script'} start`);
my $res;
if ($?) {
$res = 'Error starting: '.$?;
}
return ($self->status(), $res);
}
sub stop {
my $self = shift;
return (0, _('Daemon is not running')) unless $self->status();
warn(`$self->{'init_script'} stop`);
my $res;
if ($?) {
$res = 'Error stopping: '.$?;
}
return ($self->status(), $res);
}
sub hup {
my $self = shift;
return $self->status() if kill('HUP', $self->__pid());
}
1;