line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Daemon::DaemonizeLight; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
require Exporter; |
4
|
|
|
|
|
|
|
@ISA = qw(Exporter); |
5
|
|
|
|
|
|
|
@EXPORT_OK = qw(daemonize); |
6
|
2
|
|
|
2
|
|
29393
|
use strict; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
52
|
|
7
|
2
|
|
|
2
|
|
896
|
use FindBin; |
|
2
|
|
|
|
|
1658
|
|
|
2
|
|
|
|
|
80
|
|
8
|
2
|
|
|
2
|
|
802
|
use Proc::ProcessTableLight 'process_table'; |
|
2
|
|
|
|
|
468
|
|
|
2
|
|
|
|
|
1227
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
=head1 NAME |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
Proc::ProcessTableLight - New variant of Daemon::Daemonize based on nohup unix command |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=head1 SYNOPSIS |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
use strict; |
17
|
|
|
|
|
|
|
use Daemon::DaemonizeLight 'daemonize'; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
daemonize(); |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
... something useful code |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
=head1 DESCRIPTION |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
This module provides create simple daemon from your perl script. |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
Only add the daemonize procedure at the beginning of your script. |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
It provide 'start' 'stop' 'restart' arguments for you script in command string. |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
=cut |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub check_process{ |
34
|
0
|
|
|
0
|
0
|
0
|
my ($pid)=@_; |
35
|
|
|
|
|
|
|
|
36
|
0
|
|
|
|
|
0
|
my $t=process_table(); |
37
|
|
|
|
|
|
|
|
38
|
0
|
|
|
|
|
0
|
my $exists=0; |
39
|
0
|
|
|
|
|
0
|
my $script=$FindBin::RealScript; |
40
|
0
|
|
|
|
|
0
|
my $i=0; |
41
|
0
|
|
|
|
|
0
|
foreach my $p ( @{$t} ){ |
|
0
|
|
|
|
|
0
|
|
42
|
0
|
0
|
0
|
|
|
0
|
$exists=1 if (!$pid || $p->{PID}==$pid) && $p->{PID}!=$$ && $p->{COMMAND}=~/$script/; |
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
0
|
|
|
|
|
0
|
return $exists; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub stop{ |
49
|
0
|
|
|
0
|
0
|
0
|
my ($tmp)=@_; |
50
|
|
|
|
|
|
|
|
51
|
0
|
|
|
|
|
0
|
my $pid=read_pid($tmp); |
52
|
|
|
|
|
|
|
|
53
|
0
|
0
|
|
|
|
0
|
if ($pid){ |
54
|
0
|
0
|
|
|
|
0
|
if (check_process($pid)){ |
55
|
0
|
|
|
|
|
0
|
print "Try to killing process $FindBin::RealScript : $pid\n"; |
56
|
0
|
|
|
|
|
0
|
kill 'KILL', $pid; |
57
|
0
|
|
|
|
|
0
|
my $i=0; |
58
|
0
|
|
0
|
|
|
0
|
while(check_process($pid) && $i<3){ |
59
|
0
|
|
|
|
|
0
|
$i++; |
60
|
0
|
|
|
|
|
0
|
sleep(1); |
61
|
0
|
|
|
|
|
0
|
print "."; |
62
|
|
|
|
|
|
|
} |
63
|
0
|
0
|
|
|
|
0
|
if (check_process($pid)){ |
64
|
0
|
|
|
|
|
0
|
print "Kill process $FindBin::RealScript : $pid FAIL\n"; |
65
|
|
|
|
|
|
|
} else { |
66
|
0
|
|
|
|
|
0
|
print "Kill process $FindBin::RealScript : $pid SUCCESS\n"; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
} else { |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
} else { |
72
|
0
|
|
|
|
|
0
|
print "Nothing to kill\n"; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub start{ |
77
|
0
|
|
|
0
|
0
|
0
|
my ($tmp)=@_; |
78
|
0
|
0
|
|
|
|
0
|
if (check_process()){ |
79
|
0
|
|
|
|
|
0
|
print "Process $FindBin::RealScript already exists\n"; |
80
|
|
|
|
|
|
|
} else { |
81
|
0
|
|
|
|
|
0
|
my $script = $FindBin::Bin.'/'.$FindBin::RealScript; |
82
|
0
|
|
|
|
|
0
|
my $clear_script = $tmp.'/'.clear_ext($FindBin::RealScript); |
83
|
|
|
|
|
|
|
|
84
|
0
|
|
|
|
|
0
|
print "Starting the process $FindBin::RealScript\n"; |
85
|
|
|
|
|
|
|
|
86
|
0
|
|
|
|
|
0
|
my $cmd="nohup perl $script pid 1>$clear_script.log 2>$clear_script.err &"; |
87
|
0
|
|
|
|
|
0
|
`$cmd`; |
88
|
|
|
|
|
|
|
|
89
|
0
|
|
|
|
|
0
|
sleep(1); |
90
|
0
|
0
|
|
|
|
0
|
if (check_process()){ |
91
|
0
|
|
|
|
|
0
|
print "Starting process $FindBin::RealScript SUCCESS\n"; |
92
|
|
|
|
|
|
|
} else { |
93
|
0
|
|
|
|
|
0
|
print "Starting process $FindBin::RealScript FAILED\n"; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
sub restart{ |
99
|
0
|
|
|
0
|
0
|
0
|
my ($tmp)=@_; |
100
|
0
|
|
|
|
|
0
|
stop($tmp); |
101
|
0
|
|
|
|
|
0
|
start($tmp); |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub pid{ |
105
|
0
|
|
|
0
|
0
|
0
|
my ($tmp)=@_; |
106
|
|
|
|
|
|
|
|
107
|
0
|
|
|
|
|
0
|
my $script=clear_ext($FindBin::RealScript); |
108
|
0
|
|
|
|
|
0
|
open(F, ">$tmp/$script.pid"); |
109
|
0
|
|
|
|
|
0
|
print F $$; |
110
|
0
|
|
|
|
|
0
|
close(F); |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
sub read_pid{ |
114
|
0
|
|
|
0
|
0
|
0
|
my ($tmp)=@_; |
115
|
|
|
|
|
|
|
|
116
|
0
|
|
|
|
|
0
|
my $script=clear_ext($FindBin::RealScript); |
117
|
0
|
|
|
|
|
0
|
open(F, "$tmp/$script.pid"); |
118
|
0
|
|
|
|
|
0
|
my $pid=; |
119
|
0
|
|
|
|
|
0
|
$pid=~s/[\n\r]//gs; |
120
|
0
|
|
|
|
|
0
|
close(F); |
121
|
|
|
|
|
|
|
|
122
|
0
|
|
|
|
|
0
|
return $pid; |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
sub clear_ext{ |
126
|
0
|
|
|
0
|
0
|
0
|
my ($script)=@_; |
127
|
|
|
|
|
|
|
|
128
|
0
|
|
|
|
|
0
|
$script=~s/\..{1,3}$//gs; |
129
|
|
|
|
|
|
|
|
130
|
0
|
|
|
|
|
0
|
return $script; |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
sub daemonize{ |
134
|
1
|
|
|
1
|
1
|
8
|
my (%params)=@_; |
135
|
|
|
|
|
|
|
|
136
|
1
|
|
|
|
|
2
|
my $tmp=$params{tmp}; |
137
|
1
|
50
|
33
|
|
|
4
|
$tmp=$FindBin::Bin if (!$tmp || !(-d $tmp)); |
138
|
|
|
|
|
|
|
|
139
|
1
|
|
|
|
|
2
|
my $in=$ARGV[0]; |
140
|
|
|
|
|
|
|
|
141
|
1
|
|
|
|
|
5
|
my $funcs_in={ |
142
|
|
|
|
|
|
|
'start'=>\&start, |
143
|
|
|
|
|
|
|
'stop'=>\&stop, |
144
|
|
|
|
|
|
|
'restart'=>\&restart, |
145
|
|
|
|
|
|
|
'pid'=>\&pid |
146
|
|
|
|
|
|
|
}; |
147
|
|
|
|
|
|
|
|
148
|
1
|
50
|
|
|
|
3
|
if (!$funcs_in->{$in}){ |
149
|
1
|
|
|
|
|
2
|
delete $funcs_in->{pid}; |
150
|
1
|
|
|
|
|
3
|
die 'Dont`t know input parameter '."'$in'\nTry to ".join(",", sort(keys(%{$funcs_in})))."\n"; |
|
1
|
|
|
|
|
11
|
|
151
|
|
|
|
|
|
|
} else { |
152
|
0
|
|
|
|
|
|
&{$funcs_in->{$in}}($tmp); |
|
0
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
|
154
|
0
|
0
|
|
|
|
|
if ($in ne 'pid'){ |
155
|
0
|
|
|
|
|
|
die "EXIT\n"; |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=head2 daemonize(%params) |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
%options - only one parameter yet tmp=>'/some/tmp/dir', if not exists then tmp dir takes from directory where your script placed. |
163
|
|
|
|
|
|
|
=cut |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
=head1 SEE ALSO |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
L |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
=head1 AUTHOR |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
Bulichev Evgeniy, >. |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
=head1 COPYRIGHT |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
Copyright (c) 2017 Bulichev Evgeniy. All rights reserved. |
176
|
|
|
|
|
|
|
This module is free software; you can redistribute it and/or modify it |
177
|
|
|
|
|
|
|
under the same terms as Perl itself. |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
=cut |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
1; |