/
bric_apachectl
executable file
·218 lines (168 loc) · 5.18 KB
/
bric_apachectl
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/usr/bin/perl -w
use strict;
=pod
=head1 NAME
bric_apachectl
=head1 SYNOPSIS
bric_apachectl start
bric_apachectl stop
bric_apachectl restart
bric_apachectl debug
=head1 DESCRIPTION
This script controls the Bricolage Apache server. The usage of the script is
the same as the normal apache "apachectl" script but only "start", "stop" and
"restart" are supported. Also, "restart" performs a hard stop and start since
a SIGHUP isn't enough for mod_perl. The "debug" command starts the server
under the Perl debugger - see L<Bric::Hacker|Bric::Hacker> for details.
Usage information can be obtained by running the script with no arguments.
=head1 AUTHOR
Sam Tregar <stregar@about-inc.com>
=cut
# how long to wait for stop on restart (in seconds)
use constant MAX_RESTART_WAIT => 10;
$|++;
use File::Spec::Functions qw(catdir);
BEGIN {
# $BRICOLAGE_ROOT defaults to /usr/local/bricolage
$ENV{BRICOLAGE_ROOT} ||= "/usr/local/bricolage";
# use $BRICOLAGE_ROOT/lib if exists
$_ = catdir($ENV{BRICOLAGE_ROOT}, "lib");
unshift(@INC, $ENV{PERL5LIB} = defined $ENV{PERL5LIB} ?
"$ENV{PERL5LIB}:$_" : $_)
if -e $_;
# make sure Bric is found
eval "use Bric";
die <<'END' if $@;
######################################################################
Cannot locate Bricolage libraries. Please set the environment
variable BRICOLAGE_ROOT to the location of your Bricolage
installation or set the environment variable PERL5LIB to the
directory where Bricolage's libraries are installed.
######################################################################
END
}
use Bric::Config qw(:apachectl);
# the httpd command
my $httpd = APACHE_BIN . ' -f ' . APACHE_CONF;
# the pid file
my $pid_file = PID_FILE;
# get status
my $running = 0;
my $pid;
if (-e $pid_file) {
$pid = `cat $pid_file`;
chomp $pid;
if ($pid and kill(0, $pid)) {
$running = 1;
}
}
# check for args
usage() unless @ARGV;
my $command = shift @ARGV;
if ($command eq 'start') {
if ($running) {
print "bric_apachectl $command: httpd (pid $pid) already running\n";
exit;
}
# clear the cache before starting the server to avoid stale cache data
require Bric::App::Cache;
Bric::App::Cache->clear();
print "bric_apachectl $command: starting httpd\n";
unless (system($httpd)) {
print "bric_apachectl $command: httpd started\n";
exit;
} else {
print "bric_apachectl $command: httpd could not be started\n";
exit 3;
}
}
if ($command eq 'stop') {
unless ($running) {
print "bric_apachectl $command: httpd not running\n";
exit;
}
if (kill 15, $pid) {
print "bric_apachectl $command: httpd stopped\n";
exit;
} else {
print "bric_apachectl $command: httpd could not be stopped\n";
exit 3;
}
}
if ($command eq 'restart') {
# stop
if ($running) {
if (kill 15, $pid) {
print "bric_apachectl $command: waiting for httpd to stop";
} else {
print "bric_apachectl $command: httpd could not be stopped\n";
exit 3;
}
# wait for stop - check pid file for removal
my $stopped = 0;
for my $wait (0 .. MAX_RESTART_WAIT) {
if (not -e $pid_file) {
$stopped = 1;
last;
}
print ".";
sleep 1;
}
if ($stopped) {
print "\nbric_apachectl $command: httpd stopped\n";
} else {
print "\nbric_apachectl $command: httpd not stopped in time\n";
exit;
}
}
# clear the cache before starting the server to avoid stale cache data
require Bric::App::Cache;
Bric::App::Cache->clear();
# start
print "bric_apachectl $command: starting httpd\n";
unless (system($httpd)) {
print "bric_apachectl $command: httpd started\n";
exit;
} else {
print "bric_apachectl $command: httpd could not be started\n";
exit 3;
}
}
if ($command eq 'single') {
if ($running) {
print "bric_apachectl $command: httpd (pid $pid) already running\n";
exit;
}
# clear the cache before starting the server to avoid stale cache data
require Bric::App::Cache;
Bric::App::Cache->clear;
print "bric_apachectl $command: starting httpd -X\n";
# use exec since with -X this command won't return till the server
# is killed.
system $httpd, '-X';
}
if ($command eq 'debug') {
if ($running) {
print "bric_apachectl $command: httpd (pid $pid) already running\n";
exit;
}
# clear the cache before starting the server to avoid stale cache data
require Bric::App::Cache;
Bric::App::Cache->clear();
print "bric_apachectl $command: starting httpd -X\n";
# use exec since with -X this command won't return till the server
# is killed.
exec "$httpd -X -D BRICOLAGE_DEBUG";
}
usage();
sub usage {
print <<END;
usage: bric_apachectl (start|stop|restart|debug)
start - start httpd
stop - stop httpd
restart - restart httpd if running
debug - start httpd in the debugger
single - start httpd in -X mode, no debugger
END
exit;
}