4 # 2003/2004 - Jay Bonci <jaybonci@debian.org>
5 # This script handles the parsing of the /etc/memcached.conf file
6 # and was originally created for the Debian distribution.
7 # Anyone may use this little script under the same terms as
13 if($> != 0 and $< != 0)
15 print STDERR
"Only root wants to run start-memcached.\n";
19 my $params; my $etchandle; my $etcfile = "/etc/memcached.conf";
21 # This script assumes that memcached is located at /usr/bin/memcached, and
22 # that the pidfile is writable at /var/run/memcached.pid
24 my $memcached = "/usr/bin/memcached";
25 my $pidfile = "/var/run/memcached.pid";
27 if (scalar(@ARGV) == 2) {
28 $etcfile = shift(@ARGV);
29 $pidfile = shift(@ARGV);
32 # If we don't get a valid logfile parameter in the /etc/memcached.conf file,
33 # we'll just throw away all of our in-daemon output. We need to re-tie it so
34 # that non-bash shells will not hang on logout. Thanks to Michael Renner for
36 my $fd_reopened = "/dev/null";
41 $fd_reopened = $logfile;
48 open *STDERR
, ">>$logfile";
49 open *STDOUT
, ">>$logfile";
50 open *STDIN
, ">>/dev/null";
51 $fd_reopened = $logfile;
54 # This is set up in place here to support other non -[a-z] directives
56 my $conf_directives = {
57 "logfile" => \
&handle_logfile
,
60 if(open $etchandle, $etcfile)
62 foreach my $line (<$etchandle>)
69 next if $line =~ /^\-[dh]/;
73 my ($directive, $arg) = $line =~ /^(.*?)\s+(.*)/;
74 $conf_directives->{$directive}->($arg);
85 push @
$params, "-u root" unless(grep "-u", @
$params);
86 $params = join " ", @
$params;
90 open PIDHANDLE
, "$pidfile";
91 my $localpid = <PIDHANDLE
>;
95 if(-d
"/proc/$localpid")
97 print STDERR
"memcached is already running.\n";
109 # setsid makes us the session leader
111 reopen_logfile
($fd_reopened);
112 # must fork again now that tty is closed
115 if(open PIDHANDLE
,">$pidfile")
117 print PIDHANDLE
$pid;
121 print STDERR
"Can't write pidfile to $pidfile.\n";
125 exec "$memcached $params";