0

I am trying to write a script that starts rtmpsrv and waits for some output from it. rtmpsrv gives the desirable output and continues running, but the script is waiting for a termination of rtmpsrv. How do I gain access to the output of rtmpsrv without stopping it?

Saucier
  • 4,200
  • 1
  • 25
  • 46
quez
  • 1
  • 2
  • Would you please show us your script? – Saucier Mar 17 '14 at 15:09
  • @Ube, there is nothing to show. It could be something like `a=$(rtmpsrv); while true; do #do something with a` – quez Mar 17 '14 at 15:18
  • Can you be more precise about what you want? Do you just want to read the first few lines from the otherwise-running process? **Any** few lines? Can you afford the disk space to log all its output, or do you need to discard content after the capture? – Charles Duffy Mar 17 '14 at 15:24
  • Also, you say "already running" -- meaning the process was started before your script -- but in your text you indicate that this same script is what starts the process. Those are very different things. – Charles Duffy Mar 17 '14 at 15:25
  • @CharlesDuffy, `rtmpsrv` returns a line contains `rtmpdump`, it is a target of this script. I can use the disk space. – quez Mar 17 '14 at 15:32
  • If you can afford the disk space, Aaron Digulla's answer is a good one. There are some potential gotchas around detaching safely -- you might need to redirect stdin from /dev/null, and make sure you also redirect stderr to a log, and then use `disown` if you want to be sure the `rtmpsrv` instance survives the terminal the script was launched from exiting -- but it covers the basics. – Charles Duffy Mar 17 '14 at 15:35

3 Answers3

1

The most simple way is this:

rtmpsrv > logfile &

Then you can search logfile for the text that you're looking for. Meanwhile, rtmpsrv will do it's thing, completely unaware of your script.

This question contains examples how to wait in your script for a certain pattern to appear in the logfile (so you don't have to search it again and again): Do a tail -F until matching a pattern

Note: If you start the rtmpsrv process in the script and the script terminates, it will probably kill the rtmpsrv process. To avoid that use nohup.

Community
  • 1
  • 1
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • Generally looks good. You might extend your answer to cover making sure the `rtmpsrv` instance can survive its parent process exiting (and any terminal attached to that parent process disappearing). – Charles Duffy Mar 17 '14 at 15:36
1

Well, I'm not familiar with rtmpsrv, but unless necessary you should wait for it to finish. However, you can probably redirect its output to a file, and then grep the file to see if it contains the string you are looking for.

(fictional code... you can expect syntax hell, just want to give you an idea)

nohup rtmpsrv >log.rtmpsrv 2>&1 &
...
while :; do
    if ! result=$(grep "your desired line" log.rtmpsrv); then
        echo "success: found $result"
        break
    fi
done

Note: the if constructs should work as per http://www.tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html - just to have nicer code, as @Charles Duffy suggested.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
jimm-cl
  • 5,124
  • 2
  • 25
  • 27
  • Thank you. I tried to write output to file and tried to run with &, but separately. – quez Mar 17 '14 at 15:40
  • `value=$(grep "your desired line" log.rtmpsvr) && break` would be much more terse. Same for `if value=$(grep ...)`; then ...; fi`. Generally, checking `$?` for zero is considered an antipattern. – Charles Duffy Mar 17 '14 at 15:47
  • @Charles Duffy Does it looks better now? If not, please feel free to edit my answer - we all could benefit from it :) – jimm-cl Mar 17 '14 at 17:06
  • Made a few more tweaks, and demonstrated output capture. The outer `if` wasn't necessary, since redirections are performed before a program is started, even if that program is running in the background, so we don't need to worry about `log.rtmpsrv` not existing yet when we get to the loop. – Charles Duffy Mar 17 '14 at 17:24
  • I'd suggest http://mywiki.wooledge.org/BashGuide as a better reference, by the way. Freenode's #bash channel is constantly having to help folks unlearn bad habits they got from TLDP's "Advanced" Bash Scripting guide. – Charles Duffy Mar 17 '14 at 17:25
  • @Charles Duffy - Great, thanks for your comments. I will take a look at the site you mentioned. – jimm-cl Mar 17 '14 at 17:29
0

Just attach to the process using gdb -p <pid> where the pid is the process id of your script.

You can find the pid of your running script by doing smth like this ps ax | grep <My Script's Name>

http://etbe.coker.com.au/2008/02/27/redirecting-output-from-a-running-process/

deimus
  • 9,565
  • 12
  • 63
  • 107
  • See http://meta.stackexchange.com/questions/92505/should-i-flag-answers-which-contain-only-a-link-as-not-an-answer, coming to the conclusion that link-only answers should be flagged for close as not-an-answer. – Charles Duffy Mar 17 '14 at 15:26
  • ah ok, then I'll update my answer with some technical notes, thanks for the note @CharlesDuffy – deimus Mar 17 '14 at 15:27
  • So, that answers the question about an already-running process -- but while that's what the title of the question asks about, it's not what the *text* of the question asks about; the program for which only the first few lines are to be read before detaching and allowing it to continue to run is being launched by the script being written here, allowing *far* less invasive techniques to be used. – Charles Duffy Mar 17 '14 at 15:33
  • Your right, the title of the question is misleading. Also the text of the question is not clear, what exactly he wanted, at least he has already chosen what he was looking for. So far I tried to be useful. – deimus Mar 17 '14 at 15:41