user10055243
user10055243

Reputation:

How to store ps command output in csv format?

I am making a process monitoring application. Now my need is

  1. To get process related data (pid, process name, cpu usage, memory usage and virtual memory usage) for all the running processes.

  2. After completing the first step I want to store the retrieved data into csv format.

My code part is:

ps -e -o pid,lstart,%cpu,%mem,cmd >> output.csv

But it is storing all values in only one cell. Meaning it is not being separated by a comma.

output.csv example:

  PID                  STARTED %CPU %MEM CMD
    1 Mon Feb 25 00:00:01 2019  0.0  0.1 examplecommand1
    2 Mon Feb 25 00:00:01 2019  0.0  0.0 examplecommand2
                          (...)

Any help would be appreciated.

Upvotes: 6

Views: 7181

Answers (4)

zefreey
zefreey

Reputation: 1

ps aux >> output.csv.

This will 100% work

Upvotes: 0

chiwoohan
chiwoohan

Reputation: 1

Please use the command below.

You'll probably get the results you want.

-o %% -o Due to the corresponding options between each column % is inserted, It is replaced by , again.

Now, the columns are intuitively separated by ,, but since there are still invalid spaces, the spaces are further removed with tr -d ' '.

However, %mem and pcpu in the header line are This can be confusing because %MEM, %CPU are separated by another "space", creating two empty columns in the header line.

So if we move the column names one space after the two empty columns in the header line, we get: You can get the results you want.

 ps -e -o group, -o %% -o gid, -o %% -o user,  -o %% -o uid, -o %% -o pid, -o %% -o ppid, -o %% -o %mem, -o %% -o rss, -o %% -o vsz, -o %% -o time, -o %% -o stime, -o %% -o etime, -o %% -o nice, -o %% -o pcpu, -o %% -o pmem, -o %% -o lstart, -o %% -o priority, -o %% -o start, -o %% -o userns, -o %% -o vsize, -o %% -o vsz, -o %% -o wchan, -o %% -o command | grep -v grep | sed 's/%/,/g' | tr -d ' '>>output.csv

Upvotes: 0

Marcin Ślusarz
Marcin Ślusarz

Reputation: 11

csv-ps -c pid,cmd,%cpu,vm_rss_KiB,vm_size_KiB

csv-ps is from https://github.com/mslusarz/csv-nix-tools.

Upvotes: 1

user11087555
user11087555

Reputation:

You could try something like the following code I wrote:

ps -e -o %p, -o lstart -o ,%C, -o %mem -o ,%c > output.csv

Brief explanation:

The -o option can be used multiple times in a ps command to specify the format. In order to control which separator is used we can use AIX format descriptors. We can specify our needed separators like, e.g. %p,. Since AIX format descriptors are not available for every piece of data, but only for some of the data (for example in our case there are no AIX format descriptors for %mem and for lstart), we plant %mem and lstart around the available AIX format descriptors to achieve the comma separation. For example this site provides information about the ps command for further readings.

output.csv example:

  PID,                 STARTED,%CPU,%MEM,COMMAND
    1,Mon Feb 25 00:00:01 2019, 0.0, 0.1,examplecommand1
    2,Mon Feb 25 00:00:01 2019, 0.0, 0.0,examplecommand2
                           (...)

Upvotes: 8

Related Questions