Log in

View Full Version : Anyone managed to redirect linux server output?


Skum
July 21st, 2004, 12:55 PM
Hi guys,

I'm having exactly the same problem as this guy (http://www.shrapnelgames.com/cgi-bin/ultimatebb.cgi?ubb=get_topic;f=74;t=002425#000000) .

My question is, has anyone actually managed to get the dom2 server under linux to pipe its output to a file?

Thanks,
JM

Esben Mose Hansen
July 21st, 2004, 02:15 PM
Well, yes. My server rather relies on this. Let me have a look

I can't make redirection work, either... strange but true. My server uses a virtual terminal to capture it's output, so that is probably why it works http://forum.shrapnelgames.com/images/icons/icon7.gif

[ July 21, 2004, 13:32: Message edited by: Esben Mose Hansen ]

Skum
July 21st, 2004, 03:39 PM
Hmm.

The reason I ask is because I'd like to get the output of the server into a file, as I've been toying with the idea of an IRC bot that allows me to check status of the server (it already reports a new turn), something that shows me which players have already played their turn, and so on.

I reckon it's a bug! http://forum.shrapnelgames.com/images/icons/icon7.gif

[ July 21, 2004, 14:44: Message edited by: Skum ]

Skum
July 21st, 2004, 04:58 PM
Ahh well I worked round it by using screen and logging the output with ^a-H http://forum.shrapnelgames.com/images/icons/icon7.gif Weird!

BugRoger
July 21st, 2004, 08:36 PM
Here's the code I'm currently using for running and logging my games. Maybe you can salvage the redirection code... It's a modified Version of Mose's domwatcher script. It creates a log file and a special status file that I use for creating a game status page.

PS: Mose, I hope you don't mind me posting this...

</font><blockquote><font size="1" face="sans-serif, arial, verdana">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">#!/usr/bin/perl -w

my %nations = (
Aby =&gt; &quot;Abysia&quot;,
Atl =&gt; &quot;Atlantis&quot;,
Py =&gt; &quot;Pythium&quot;,
Man =&gt; &quot;Man&quot;,
Ul =&gt; &quot;Ulm&quot;,
Ct =&gt; &quot;C\'tis&quot;,
Arc =&gt; &quot;Arcoscephale&quot;,
Ca =&gt; &quot;Caelum&quot;,
Er =&gt; &quot;Ermor&quot;,
Mar =&gt; &quot;Marignon&quot;,
Pa =&gt; &quot;Pangaea&quot;,
Va =&gt; &quot;Vanheim&quot;,
Jo =&gt; &quot;Jotunheim&quot;,
Rl =&gt; &quot;R\'lyeh&quot;,
Mi =&gt; &quot;Mictlan&quot;,
Ti =&gt; &quot;T\'ien Ch\'i&quot;,
Mac =&gt; &quot;Machaka&quot;
);


sub playgame {
my $gamename = &quot;CradleOfDoom&quot;;
my $playerCount = 10;

# generate command
my $command = &quot;dom2 --postexec /usr/local/games/dominions2/notification_CradleOfDoom --tcpserver --port 6666 --noclientstart --mapfile cradle.map --textonly --quickhost --minutes 2880 -d $gamename&quot;;

# Fork off server
use IO::Pty; # This stuff is brilliant!
use POSIX &quot;:sys_wait_h&quot;; # WNOHANG

my $master = new IO::Pty;
my $pid = fork;
die &quot;Failed to fork: $!&quot; unless (defined($pid));
unless ($pid) {
#child
$master-&gt;make_slave_controlling_terminal();
select STDOUT; $| = 1; # make unbuffered
$fh = $master-&gt;slave-&gt;fileno;
open(STDOUT, &quot;&gt;&amp;$fh&quot;) or die &quot;Unable to redirect STDOUT: $!&quot;;

exec($command) or die &quot;Failed to execute ($command)&quot;;
}
$master-&gt;close_slave();

open(LOG, &quot;&gt;&gt;$gamename.log&quot;) or die &quot;Failed to $gamename.log: $!&quot;;
open(STDERR, &quot;&gt;&amp;LOG&quot;);
open(STDOUT, &quot;&gt;&amp;LOG&quot;);

select LOG; $| = 1;
print LOG &quot;domlogger Version 0.1 started\n&quot;;
print LOG &quot;$command\n&quot;;

#The game is well startet...record pid
print LOG &quot;pid=$pid\n&quot;;


my $server_quit = 0;
my $turn = -1;
my $timeleft = -1;
my $waitingFor = -1;
my $playersConnected = 0;
my $turnsTaken = 0;

PLAY_GAME: while (!$server_quit) {
if ($pid == waitpid($pid, WNOHANG)) {
$server_quit = 1;
print LOG &quot;Server quit, exiting\n&quot;;
`beep -r 2 -f 300 -l 1000`;
Last;
}

my $nextLineIsGameStatus = 0;
while (&lt;$master&gt;) {
print LOG $_;

if ($nextLineIsGameStatus) {
my $currentlyConnected = 0;
my $currentlyTakenTurns = 0;

open(STATUS, &quot;&gt;$gamename.stats&quot;) or die &quot;Failed to create $gamename.stats: $!&quot;;
print STATUS &quot;$pid\n&quot;;
print STATUS &quot;$gamename\n&quot;;
print STATUS &quot;$turn\n&quot;;
print STATUS &quot;$timeleft\n&quot;;

@nat = split;
for $nat (@nat) {
my ($computer_controlled, $connected, $shortName, $waiting_for) = $nat =~/(\(?)(\*?)(\w+)\)?([+-]?)/;
$waiting_for = ($waiting_for eq '-') ? 1 : 0;
$connected = ($connected eq '*') ? 1 : 0;
$computer_controlled = ($computer_controlled eq '(') ? 1 : 0;
$nationLongName = $nations{$shortName};

if ($connected) {
$currentlyConnected += 1;
}

if (!$waiting_for) {
$currentlyTakenTurns += 1;
}

print STATUS &quot;$shortName $nationLongName $waiting_for $connected $computer_controlled\n&quot;;
}

close(STATUS);

if ($currentlyConnected != $playersConnected) {
$playersConnected = $currentlyConnected;
`beep -r $playersConnected -l 10 -f 100`;
}

if ($currentlyTakenTurns != $turnsTaken) {
$turnsTaken = $currentlyTakenTurns;
my $beeps = $playerCount - $turnsTaken;
`beep -r $beeps -l 10`;
}


$nextLineIsGameStatus = 0;
} else {
if (/^fatherturn/) {
($turn) = /^fatherturn (.*\S)\s*$/;
next;
}

if (/^$gamename/) {
$nextLineIsGameStatus = 1;

($timeleft, $unit) = /Time (\d+)(\w)/;
if (defined($timeleft) &amp;&amp; defined($unit)) {
$timeleft *= 60 if ($unit eq 'h');
}

next;
}

if (/^\s*Generating next turn\s*$/) {
`beep -r 3`;
}

}

}
}




# Game has ended (or server crashed)
print LOG &quot;Update game status to ended\n&quot;;
close(LOG);
}

playgame();</pre><hr /></blockquote><font size="2" face="sans-serif, arial, verdana">And this is the script that generates the status page out of the statsfile. You can see it in action on http://cradleofdoom.dyndns.org/gamestatus/

</font><blockquote><font size="1" face="sans-serif, arial, verdana">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">
&lt;?php
$gameName = &quot;CradleOfDoom&quot;;
$stats = file($gameName . &quot;.stats&quot;);
$modified = stat($gameName . &quot;.stats&quot;);
$modifiedDate = date(&quot;l dS of F Y h:i:s A&quot;,$modified[9]);

$pid = $stats[0];
$isServerOnline = exec(&quot;ps aux | awk '{print $2}' | grep -c $pid&quot;);
?&gt;

&lt;html&gt;
&lt;head&gt;
&lt;title&gt;&lt;?php echo $stats[1] ?&gt; - Turn &lt;?php echo $stats[2] ?&gt;&lt;/title&gt;
&lt;meta http-equiv=&quot;refresh&quot; content=&quot;60; URL=http://cradleofdoom.dyndns.org/gamestatus/&quot;&gt;
&lt;style&gt;
body {
background: #222;
font-family: Arial, sans-serif;
color: #eee;
}

#status {
color: #000;
border: 2px solid #99835C;
background: url(bg.gif);
}

#status td {
padding: 5px 10px 5px 10px;
border-top: 1px solid #FFF1C5;
border-bottom: 1px solid #99835C;
}

#status th {
padding: 5px 30px 5px 10px;
border-bottom: 1px solid #99835C;
}

th {
text-align: left;
}


&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;h1&gt;&lt;?php echo $stats[1] ?&gt; - Turn &lt;?php echo $stats[2] ?&gt;&lt;/h1&gt;


&lt;table id=&quot;status&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot;&gt;
&lt;tr&gt;
&lt;th&gt;&amp;nbsp;&lt;/th&gt;
&lt;th&gt;Nation&lt;/th&gt;
&lt;th&gt;Turn Taken&lt;/th&gt;
&lt;th&gt;Connected&lt;/th&gt;
&lt;/tr&gt;
&lt;?php
for ($i=4; $i &lt; count($stats); $i++) {
list($short, $long, $waiting, $connected, $ai) = split(&quot; &quot;, $stats[$i]);

if ($waiting) {
$waiting = &quot;has &lt;b&gt;not&lt;/b&gt; played its turn&quot;;
} else {
$waiting = &quot;has played its turn&quot;;
}

if ($connected) {
$connected = &quot;currently connected&quot;;
} else {
$connected = &quot;&amp;nbsp;&quot;;
}

if ($ai == 1) {
$waiting = &quot;is computer controlled&quot;;
}
?&gt;
&lt;tr&gt;
&lt;td&gt;&lt;img src=&quot;unwise_flags/&lt;?php echo strtolower($short) ?&gt;_icon.gif&quot; alt=&quot;&quot;&gt;&lt;/td&gt;
&lt;td&gt;&lt;?php echo $long ?&gt;&lt;/td&gt;
&lt;td&gt;&lt;?php echo $waiting ?&gt;&lt;/td&gt;
&lt;td&gt;&lt;?php echo $connected ?&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;?php
}
?&gt;
&lt;/table&gt;
&lt;br /&gt;
&lt;table&gt;
&lt;tr&gt;
&lt;th&gt;Server status:&lt;/th&gt;
&lt;td&gt;&lt;?php if ($isServerOnline) echo &quot;Online&quot;; else echo &quot;offline&quot;; ?&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;th&gt;Forced host in:&lt;/th&gt;
&lt;td&gt;&lt;?php echo $stats[3] ?&gt; minutes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;th&gt;Last update:&lt;/th&gt;
&lt;td&gt;&lt;?php echo $modifiedDate ?&gt; CEST&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;

&lt;/body&gt;
&lt;/html&gt; </pre><hr /></blockquote><font size="2" face="sans-serif, arial, verdana">