Capturing first output from 'top'-likes command

Is this a stupid code??

top > top.out &
sleep 2
kill %1
cat top.out

Thanks,

May be you need this?

top -n 1 > top.out

--ahamed

1 Like

Yes. Also "kill %1" is a syntax error.
Please post your Operating System and version and if it didn't come with "top", where you got "top" from.
There are many and various versions of "top" but most of them have command line options. A properly installed one will have "man top".

e.g. In my version the "-d 1" parameter makes "top" exit after one interation.

top -d 1

In practice you can usually get anything which is a displayed my "top" from standard unix commands (and in an easier to process format).

Now: ahmed101 must have a different version of "top" because in my version "-n 1" would only display one process.

1 Like

The command is not 'top' and no command line options. Just top-like command.
It displays third-party app. engine's status and re-display every 1-second....

Thanks,

top and other interactive commands like it are prone to create outputs full of escape sequences that are difficult to view without a terminal, no matter how they're captured.

top itself can do this, so you don't need to kill it:

top -n 1 > output

---------- Post updated at 11:21 AM ---------- Previous update was at 11:19 AM ----------

If it works, it's not stupid, but whether and how well it will work depends on the application. The output might not be very readable without a terminal. It'd be better to get the output with the application's cooperation, if possible.

'cat myapp.out' may be misleading. When you do that in a terminal, the terminal will interpret escape sequences for you and present the output properly. But if you try sending that to a printer, or process it with commandline tools, you could find it full of gibberish.

myapp > myapp.out &
PID=$!
sleep 2
kill $PID
less myapp.out
1 Like

If this application has a keyboard stop command, you could try typeahead:
The "cat -v" command demonstrates that the output from "top" contains all sorts of screen control characters.

(sleep 1;echo "q") | top > top.out
cat -v top.out

Aside: In correcting your "kill" command, Corona688 probably meant:

myapp > myapp.out &
PID=$!
sleep 2
kill ${PID}
less myapp.out
2 Likes

I'm really appreciate your replies.