I have a handy perl script I use to shut the server down. Basically, I run several different gameservers online and I rotate them out. However, nobody likes it very much when a server shuts down automatically (via a cronjob) when it's timeslot is up and people are in the server.
So, to compensate, I built a perl script to query the server and only shut it down if it's unoccupied. If it is occupied, it will try every 5 minutes until it's unoccupied. Maybe someone can use the query part for other good uses (building a nice little webpage for your server, for instance).
<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">
#!/usr/bin/perl
use Socket;
$ofpserverstatus = "\\status\\";
$ip = $ARGV[0] || "12.34.56.78";
$port = $ARGV[1] || 2303;
$paddr = sockaddr_in($port, inet_aton($ip));
$proto = getprotobyname("udp");
socket(SOCKET, PF_INET, SOCK_DGRAM, $proto) or die "socket: $!";
connect(SOCKET, $paddr) or die "connect: $!";
send(SOCKET, "$ofpserverstatus\n", 0);
recv(SOCKET, $line, 65000,0);
@lines = split "\n", $line;
#print "$line\n";
@output = split /\\/, $lines[0];
#@pairs = split /\\/, $lines[0]; for($i=1; $i < scalar @pairs; $i+=2) { print "$pairs[$i]: $pairs[$i+1]\n"; };
$numplayers = @output[16];
#print "$numplayers\n";
sub decision {
if ($numplayers == 0) {
print "no players on server, terminating server\n";
system("killall ofp_server");
exit;
} else {
print "server populated with $numplayers players, waiting 5 minutes\n";
sleep(600);
print "slept for 5 minutes\n";
&decision
}
}
while (1) { exit if &decision; }
There's a few commented out lines in there that were left in there as good examples of other stuff you can do. I copied much of this from some other guy's ofp server query script I saw online somewhere. Sorry for not-more-proper-credit-giving. :-(