title: How to log the live output of a running process
body: <p>I want to run a game server inside my Ubuntu machine. I want to run it in the background and write the live output of that process inside a log file. I tried using nohup and running the game server using "&amp;" at the end but I couldn't make it work the way I wanted. </p>
  
  <p>Then I started reading about named pipes and actually gave it a go. I made a simple script that in theory should work. But, of course I am missing something.</p>
  
  <p>First, I made a pipe using the mkfifo command.</p>
  
  <pre><code>mkfifo testpipe
  </code></pre>
  
  <p>Then I created a small script:</p>
  
  <pre><code>#!/bin/bash
  ./mta-server64 &gt; pipe &amp;
  
  pid=$!
  
  echo $pid // so I know the pid of the process
  
  cat &lt; pipe &gt; log.txt &amp;
  </code></pre>
  
  <p>(Note: I wrote this code from memory.)</p>
  
  <p>The code works only when there is an error and the process stops. It actually records the game console error. But when the game server is running I get no output in the log file.</p>
  
  <p>I want to read the output (stdout and stderr if I am not mistaken) of a process running in background and record it those inside a log file. </p>
  
  <p>I also thought about using screen as it logs everything inside a file but I would prefer not using it if there is a better solution.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>First of all: thank you for the interest you had in helping me. In the same way, I have to apologize for only giving scarce details about what I intend to do with this small project and for my limited understanding of stdout and stderr.</p>
  
  <p>Let's go to the first base.</p>
  
  <p>I want to run a game server named Multi Theft Auto (<a href="https://multitheftauto.com/" rel="nofollow noreferrer">https://multitheftauto.com/</a>). This is GTA San Andreas but multiplayer.</p>
  
  <p>I can easily run this game server in my Ubuntu server by calling the executable <code>./mta-server-64</code>. After calling it the game server console appears:</p>
  
  <pre><code>[|] MTA: San Andreas :: 0/32 players :: 196 resources :: 125 fps (25)
  MTA:BLUE Server for MTA:SA
  ==================================================================
  = Multi Theft Auto: San Andreas v1.5.6 [64 bit]
  ==================================================================
  = Server name      : Default MTA Server
  = Server IP address: auto
  = Server port      : 22884
  =
  = Log file         : /root/mta/mods/deathmatch/logs/server.log
  = Maximum players  : 32
  = HTTP port        : 22564
  = Voice Chat       : Disabled
  = Bandwidth saving : Medium
  ==================================================================
  [09:49:07] Resource 'mapmanager' requests some acl rights. Use the command 'aclrequest list mapmanager'
  [09:49:07] Resources: 196 loaded, 0 failed
  [09:49:07] Starting resources...
  [09:49:07] Server minclientversion is now 1.5.6-9.16588.0
  [09:49:07] INFO: MAPMANAGER: Some important ACL permissions are missing. To ensure the correct functioning of Mapmanager, please write: aclrequest allow mapmanager all
  [09:49:07] Gamemode 'play' started.
  [09:49:07] Authorized serial account protection is enabled for the ACL group(s): `Admin`  See http://mtasa.com/authserial
  [09:49:07] WARNING: &lt;owner_email_address&gt; not set
  [09:49:07] Server started and is ready to accept connections!
  [09:49:07] To stop the server, type 'shutdown' or press Ctrl-C
  [09:49:07] Type 'help' for a list of commands.
  [09:49:07] Querying MTA master server... success! (Auto detected IP:xxx.xxx.xxx.xxx)
  </code></pre>
  
  <p>I am using the following script to run the process in the background and (try to) get the live output from:</p>
  
  <pre><code>#!/bin/bash
  
  newport=$(shuf -i 22003-22900 -n 1)
  newip=$(shuf -i 22003-22900 -n 1)
  
  rm -rf ~/server/*
  
  cp -r /home/user*/ftp/server/mtaserver/serverfiles/* ~/server
  
  sed -i "s/&lt;httpport&gt;[0-9][0-9][0-9][0-9][0-9]&lt;\/httpport&gt;/&lt;httpport&gt;$newport&lt;\/httpport&gt;/g" ~/server/mods/deathmatch/mtaserver.conf
  sed -i "s/&lt;serverport&gt;[0-9][0-9][0-9][0-9][0-9]&lt;\/serverport&gt;/&lt;serverport&gt;$newip&lt;\/serverport&gt;/g" ~/server/mods/deathmatch/mtaserver.conf
  
  ~/server/mta-server64 2&gt;&amp;1 | tee -a outfile &amp;
  
  mta_pid=$!
  
  echo $mta_pid
  
  sleep 6
  
  pkill $mta_pid
  </code></pre>
  
  <p>(Note: Because of some technical problems I had to add the first few lines of script which automatically replace the game files with new ones and also replace the existing ports with random ones.)</p>
  
  <p>This script starts the server and tries to log the output of the process. The process is automatically killed after few seconds so there is only one instance of the game server at any given time.</p>
  
  <p><strong>THE ISSUE:</strong></p>
  
  <p>This script only logs the output if there is an error. I still cannot get the live output of the process when it is still running. Maybe this is an issue with the game server but truly believe there should be a way to make it work the way I intend.</p>