1#!/usr/bin/perl -w
2
3# start-memcached
4# 2003 - Jay Bonci <[email protected]>
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
8# memcached itself.
9
10use strict;
11
12if($> != 0 and $< != 0)
13{
14	print STDERR "Only root wants to run start-memcached.\n";
15	exit;
16}
17
18my $params; my $etchandle; my $etcfile = "/etc/memcached.conf";
19my $memcached = "/usr/bin/memcached";
20my $pidfile = "/var/run/memcached.pid";
21
22if(open $etchandle, $etcfile)
23{
24	foreach my $line (<$etchandle>)
25	{
26		$line ||= "";
27		$line =~ s/\#.*//g;
28		$line =~ s/\s+$//g;
29		$line =~ s/^\s+//g;
30		next unless $line;
31		next if $line =~ /\-[dvh]/;
32
33		push @$params, $line;
34	}
35
36}else{
37	$params = [];
38}
39
40	push @$params, "-u root" unless(grep "-u", @$params);
41	$params = join " ", @$params;
42
43if(-e $pidfile)
44{
45	open PIDHANDLE, "$pidfile";
46	my $localpid = <PIDHANDLE>;
47	close PIDHANDLE;
48
49	chomp $localpid;
50	if(-d "/proc/$localpid")
51	{
52		print STDERR "memcached is already running.\n";
53		exit;
54	}else{
55		`rm -f $localpid`;
56	}
57
58}
59
60my $pid = fork();
61
62if($pid == 0)
63{
64		exec "$memcached $params";
65		exit(0);
66
67}else{
68	if(open PIDHANDLE,">$pidfile")
69	{
70		print PIDHANDLE $pid;
71		close PIDHANDLE;
72	}else{
73
74		print STDERR "Can't write pidfile to $pidfile.\n";
75	}
76}
77
78