3

I have a long running operation that affects some state. That state is shown by running a certain command, say showst.

I want to see have a log of all the possible outputs showst will display during that long running operation.

For a short operation I would use

watch -n0.1 showst

And would just stare at the terminal looking at what is going on. But if the operation runs for an hour that is not very practical. I want to have a log that would tell me that at a specific time showst output changed to this. And then it changed to that.

Is there a command that can help me?


Edit to add a bit of clarification.

Here is a specific example of what I would like to see. Let's say that the operation runs for an hour. showst outputs A for the first half an hour, then B for 10 minutes, then switches to A again.

I want to see a log similar to this:

2016-08-19 12:00: A
2016-08-19 12:30: B
2016-08-19 12:40: A

It would be great if A and B could be multiline as well.

It is kind of like what watch --differences can do on a terminal, if you are sitting and watching, but I want that in a log.

Illia Bobyr
  • 131
  • 4
  • does it output to STDOUT? You could try showst >log.txt & on the next line you could tail -f log.txt or grep the file or anything. – cybernard Aug 19 '16 at 02:00
  • The output is to stdout, but the command outputs current state and the exits. If I run 'showst >log.txt' I would get just the current state. I want to see all the possible states over a period of time. I want identical outputs "folded" and only when something in the output changes I want to see new entry in the log. – Illia Bobyr Aug 19 '16 at 02:21
  • showst >>log.txt is appending. cat log.txt|sort --unique hides and sorts. – cybernard Aug 19 '16 at 03:44
  • Appending is not helpful on it's own if 'showst' exist after it outputs current state. And if I run it in a loop, over a log period of time I would have multiple identical entries. I would not want that. I want to see when the output changed. Similar to 'watch --differences' but in a log, instead of on the terminal. – Illia Bobyr Aug 19 '16 at 20:30

1 Answers1

0

I don't know about a dedicated command, but I would have written something like that:

(while true; do sleep 0.1; showst; done) | uniq

Note: sleep implementations on Linux usually supports such floating arguments, but if you need a portable solution then you should use an integer argument there which limit the pooling to one second tick, or no sleep at all which will use your cpu a lot.

See also man pages of sleep and uniq.

A. Loiseau
  • 1,198
  • 7
  • 6
  • 1
    This only works if the 'showst' output is one line. And it does not tell me when the change occurred. I would not see the sequence of changes. If the state changes from A to B and then to A again, I would only have 'A' and 'B' in the output. – Illia Bobyr Aug 19 '16 at 20:27
  • 1
    As said by @drewbenn and by the man page, `uniq` prints the first occurrence of any new series as soon as it comes and ignores consecutive duplicates. You can `|grep` the showst call to extract wanted line or words and you can append `|while read NEW_STATE; do echo "$(date) - ${NEW_STATE}"; done` to prepend dates of detected changes on the result. – A. Loiseau Aug 19 '16 at 21:05