Reputation: 21884
I am using a tool that you can only interrupt using Ctrl-C
(or kill
). This is monitoring a certain component and reporting some info every few seconds. The tool does not have support for specifying a limit how many times check the component before exit. So it will run forever until it is stopped.
How can I capture a sample of this output into a variable for further processing? I also have to stop this monitoring tool and continue processing.
Upvotes: 0
Views: 229
Reputation: 2379
You could output the data to a file and just run tail
on that file that file to get current output. You can clear the file when it gets too big. If you want a more specific answer you'll need to provide more detail.
For instance if your process is called printJunk
, you can start it as
./printJunk >>afile.txt
then at any time or times while ./printJunk
is running, you can run the command below to get the most recent 10 lines into a variable:
someVariable=$(tail -n 10 afile.txt)
Viewing the output of the file and assigning it to a variable won't have any effect on the runtime of ./printJunk
.
When afile.txt gets too large, do echo >afile.txt to empty it. Again, ./printJunk
won't care that this happens.
Upvotes: 1
Reputation: 531175
If you pipe your tool to another program, and that program exits, your tool will receive the SIGPIPE
signal the next time it tries to write to the (now closed) pipe and exit.
Here's an example that captures the first 10 lines of output from your tool.
sample=$(your-tool | head -10)
Upvotes: 1