0

I have a script that outputs a lot of debug data that I would like to avoid to save on disk (specially since the file system is on a flash memory card). So I run it on the background with nohup + & disposing stdout and stderr to /dev/null.

But when it begins to fail I would like be able to get that debug output on its stdout live. In the manner as when one connects through a serial port of a device to get its output. A stream that can be monitored but it's not stored on the device.

I was thinking in redirect the output to a local UDP socket so I could connect and disconnect from the "stream" without affecting in anyway the script and the very same script wouldn't bother if there's someone listening to the data. But I'm interested in the different techniques I could use, and the tools that are already available.

How can I stream the output of a running process?

Restriction

I can't modify the library that outputs the debug information to stdout. I have to pipe the script's stdout to a program that can stream it (not saving to disk the whole output).

  • 1
    Would it work to run it inside screen or tmux instead of with nohup? That way if you want to see it's output you just reconnect tot he screen session – Eric Renouf Mar 23 '16 at 20:52
  • I think that management would be harder. I use a script to control the running status of the script and execute some cleaning procedures. I think that in a server that uses several screen sessions it would turn complicated get the right screen session to shutdown on my stop command. And if I only kill the process, then on each start command I would get another screen session, and empty sessions would pile up fast. – Sdlion Mar 23 '16 at 21:09
  • Screen generally closes the session when there are no running processes left – Eric Renouf Mar 23 '16 at 21:12
1

Pipe the output to NetCat which can stream the data to a UDP socket.

  • If I close the connection, netcat won't shutdown the socket automatically finishing its execution? – Sdlion Mar 23 '16 at 20:56
  • Tested nc -l -u -p 20123 localhost as an UDP server. When I connect with nc -u localhost 20123 on another terminal, first I have to write something from the client side. At that moment I get the data that it wasn't read before from the buffer; and if I disconnect from the client side, the netcat server dies. – Sdlion Mar 23 '16 at 21:16
0

As pointed out by @Eric Renouf, screen is enough to pull the trick. The logging options of screen are really helpful too, since in any moment inside the session a log file can be turned on and off with C-a H or with the :log command as documented on the manual. In this way is easy to control with precision how much of the output is recorded.

Starting the script:

screen -dmS $SESSION_NAME "$SCRIPTDIR/$SCRIPT_NAME" 2>&1

Stopping the script:

pkill -f "$BIN .+/$SCRIPT_NAME"

I wanted a little more simple and "everything-is-a-file" linux style to be able to use simpler tools like tee; but for the moment this is good enough and answers the question. Different implementations are still welcome.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.