line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Daemon::Easy; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
33369
|
use 5.008008; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
39
|
|
4
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
83
|
|
5
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
5
|
|
|
1
|
|
|
|
|
36
|
|
6
|
1
|
|
|
1
|
|
1008
|
use POSIX; |
|
1
|
|
|
|
|
13852
|
|
|
1
|
|
|
|
|
7
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub import{ |
12
|
1
|
|
|
1
|
|
12
|
my $class = shift; |
13
|
1
|
50
|
33
|
|
|
11
|
my %args = ( (@_ and ref($_[0])) ? %{$_[0]} : @_ ) or (); |
|
0
|
50
|
|
|
|
0
|
|
14
|
|
|
|
|
|
|
|
15
|
1
|
50
|
|
|
|
5
|
$args{sleep} = 5 unless defined $args{sleep}; |
16
|
1
|
|
33
|
|
|
10
|
$args{pidfile} ||= "$0.pid"; |
17
|
1
|
|
33
|
|
|
6
|
$args{stopfile} ||= "$0.stop"; |
18
|
1
|
|
50
|
|
|
5
|
$args{callback} ||= 'worker'; |
19
|
|
|
|
|
|
|
|
20
|
1
|
|
|
1
|
|
3697
|
no strict 'refs'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
793
|
|
21
|
1
|
|
|
|
|
2
|
$args{worker} = \&{caller()."::$args{callback}"}; |
|
1
|
|
|
|
|
7
|
|
22
|
1
|
|
|
|
|
15
|
*{caller()."::run"} = sub{ |
23
|
0
|
|
|
0
|
|
|
unshift @_, \%args; |
24
|
0
|
|
|
|
|
|
goto &_run; |
25
|
1
|
|
|
|
|
5
|
}; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub _run{ |
29
|
0
|
|
|
0
|
|
|
my ($args,$cmd) = @_; |
30
|
0
|
|
0
|
|
|
|
$cmd ||= $ARGV[0]; |
31
|
|
|
|
|
|
|
|
32
|
0
|
|
|
|
|
|
$cmd = lc $cmd; |
33
|
0
|
0
|
|
|
|
|
if( $cmd eq 'start' ){ |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
34
|
0
|
0
|
|
|
|
|
if(my $status = status($args)){ |
35
|
0
|
|
|
|
|
|
print "the daemon is running with pid: $status\n"; |
36
|
0
|
|
|
|
|
|
exit; |
37
|
|
|
|
|
|
|
} |
38
|
0
|
|
|
|
|
|
start($args); |
39
|
|
|
|
|
|
|
}elsif( $cmd eq 'stop' ){ |
40
|
0
|
|
|
|
|
|
stop($args); |
41
|
|
|
|
|
|
|
}elsif( $cmd eq 'status' ){ |
42
|
0
|
0
|
|
|
|
|
if ( status($args) ){ |
43
|
0
|
|
|
|
|
|
print "the daemon is running with pid: ".status($args)."\n"; |
44
|
|
|
|
|
|
|
}else{ |
45
|
0
|
|
|
|
|
|
print "the daemon stopped\n"; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
}elsif( $cmd eq 'restart' ){ |
48
|
0
|
|
|
|
|
|
stop($args); |
49
|
0
|
|
|
|
|
|
sleep(3) while(status($args)); |
50
|
0
|
|
|
|
|
|
start($args); |
51
|
|
|
|
|
|
|
}else{ |
52
|
0
|
|
|
|
|
|
usage(); |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub usage{ |
57
|
0
|
|
|
0
|
0
|
|
print "usage:\n\t $0 [start stop restart status]\n" ; |
58
|
0
|
|
|
|
|
|
exit; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub start{ |
62
|
0
|
|
|
0
|
0
|
|
my $args = shift; |
63
|
0
|
|
|
|
|
|
my $pid = fork(); |
64
|
0
|
0
|
|
|
|
|
die "cant fork, $!\n" unless defined $pid; |
65
|
|
|
|
|
|
|
|
66
|
0
|
0
|
|
|
|
|
if($pid){ # parent, remember the child pid, and exit |
67
|
0
|
0
|
|
|
|
|
open PID,">$args->{pidfile}" or die "cant open $args->{pidfile}, $!\n"; |
68
|
0
|
|
|
|
|
|
print PID $pid; |
69
|
0
|
|
|
|
|
|
close PID; |
70
|
0
|
|
|
|
|
|
exit(0); |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
0
|
|
|
|
|
|
POSIX::setsid(); |
74
|
|
|
|
|
|
|
|
75
|
0
|
0
|
|
|
|
|
unlink ($args->{stopfile}) if( -e $args->{stopfile} ); |
76
|
|
|
|
|
|
|
|
77
|
0
|
|
|
|
|
|
while(1){ |
78
|
0
|
|
|
|
|
|
eval { $args->{worker}->(); }; |
|
0
|
|
|
|
|
|
|
79
|
0
|
0
|
|
|
|
|
if($@){ |
80
|
0
|
|
|
|
|
|
print $@; |
81
|
0
|
|
|
|
|
|
last; |
82
|
|
|
|
|
|
|
} |
83
|
0
|
0
|
|
|
|
|
sleep($args->{sleep}) if $args->{sleep}; |
84
|
0
|
0
|
|
|
|
|
if(-e $args->{stopfile} ){ |
85
|
0
|
|
|
|
|
|
unlink($args->{stopfile}); |
86
|
0
|
|
|
|
|
|
last; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
0
|
|
|
|
|
|
unlink ($args->{pidfile}); |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
sub stop{ |
94
|
0
|
|
|
0
|
0
|
|
my $args = shift; |
95
|
0
|
0
|
|
|
|
|
open FH, ">$args->{stopfile}" or die "cant create $args->{stopfile}, $!\n"; |
96
|
0
|
|
|
|
|
|
close FH; |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
# check status, 0 if stoped, pid if running |
100
|
|
|
|
|
|
|
sub status{ |
101
|
0
|
|
|
0
|
0
|
|
my $args = shift; |
102
|
0
|
0
|
|
|
|
|
if(-e $args->{pidfile} ){ |
103
|
0
|
0
|
|
|
|
|
open PID,$args->{pidfile} or die "cant open $args->{pidfile}, $!\n"; |
104
|
0
|
|
|
|
|
|
my $pid=; chomp $pid; |
|
0
|
|
|
|
|
|
|
105
|
0
|
|
|
|
|
|
close PID; |
106
|
0
|
|
|
|
|
|
return $pid; |
107
|
|
|
|
|
|
|
}else{ |
108
|
0
|
|
|
|
|
|
return 0; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
1; |
113
|
|
|
|
|
|
|
__END__ |