Linux – Piping PS output into a Text file

linuxps

So what im wanting to do is pretty basic…..but im not sure how to go about it.
Lets say I have a program running, but I want to pipe the output of ps -C "ProgramXX" into a file.

Normally Running ps -C "ProgramXX" Produces something to the effect of:

PID TTY TIME            CMD
123 ?   00:00:00    Program1
142 ?   00:00:00    Program1
165 ?   00:00:00    Program1
221 ?   00:00:00    Program1

Is their any "simple" way to just get the 1st columns and 4th column (ignoring the top heading line) into a text file.

I figure you could run ps -C "Program1" >> Data.txt…..then maybe run a script to cut out the fat? This would be done in Bash btw. Maybe using grep or something…..but I wouldn't really know. This would be ran multiple times btw……so data.txt would continue to grow.

Best Answer

You can control the columns that get output by ps. Note that the exact commandline does vary between the various flavours of Linux/Unix but I believe the following will do what you want.

ps --no-headers -o pid,comm  -C "ProgramXX" >> Data.txt

The man page for ps(1) will list all of the options available to you.

Related Question