This one looks at your active log file and checks the last instance of the player list. It then outputs the player count.
Because it reports no users as 0, you can run your existing Linux scripts against this to know if it's [for example] safe to restart Minetest for a backup. Eg., PLAYERS=`php player_count.php` in a bash script will set $PLAYERS accordingly. Or, do what we do: comment out the two "echo" lines and include() the script in our main PHP script, and then refer to $playercount any time you want to make a decision based on number of online players. if ($playercount > 0) { // do nothing } else { // do something } kind of idea.
All you have to do is change the log location, and be sure to install PHP (doesn't need to be a web server, just need the interpreter... php5 for example)
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
- Code: Select all
<?php
$logfile = '/home/robbie/.minetest/logs/server.log';
$logdata = file($logfile);
if (is_array($logdata)) {
$logdata = array_reverse($logdata);
foreach ($logdata as $logline) {
if (strstr($logline,'List of players:')) {
$tmp = explode('List of players:',$logline);
$players = trim($tmp[1]);
if (strstr($players,' ')) {
$tmp2 = explode(' ',$players);
$playercount = count($tmp2);
} elseif (strlen($players) > 0) {
$playercount = 1;
} else {
$playercount = 0;
}
break;
}
}
echo $playercount;
} else {
echo 'Unable to load log.';
}
?>
Enjoy! If you use this in your project, please link to it below -- we'd love to know.
We use this script to know if it's safe to restart Minetest and sync our player skins which have been uploaded to our web site, which is a fully automated process.