Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read output from linux process status (ps) command in R?

here is the data.txt:

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND  
root         1  0.0  0.0   2280   728 ?        Ss   20:44   0:00 init [2]    
root         2  0.0  0.0      0     0 ?        S    20:44   0:00 [kthreadd]  
root       202  0.0  0.0      0     0 ?        S<   20:44   0:00 [ext4-dio-unwri  
root       334  0.0  0.1   2916  1452 ?        Ss   20:44   0:00 udevd --daemon  

how to read the data into a data.frame?
1.can not to decide separator
the last field is a problem,space can not be the separator,
init [2] ,udevd --daemon are the one field,can not be separated by space.
2.no fixed width
every line has different width.

so ,how can i read the data.txt into a data.frame?

like image 870
showkey Avatar asked Jan 21 '13 03:01

showkey


People also ask

What is the output of ps command in Linux?

ps displays status information about processes, and optionally, the threads running under each process. By default, for each process that is associated with the user's terminal, ps displays the process ID (PID), TTY, processor time used (TIME), and name of the command (COMM).

How can you find the status of a process in Linux?

Linux provides us a utility called ps for viewing information related with the processes on a system which stands as abbreviation for “Process Status”. ps command is used to list the currently running processes and their PIDs along with some other information depends on different options.

What does the letter Z in the status column of the ps command output indicate?

Under the -@ flag, the ps command displays the name of the workload partition in which the process is running. Specify the -@ flag with the wparname parameter to display the process information. (Z flag) The data page size of the process. Indicates that the process is operating in core memory.


1 Answers

I would do it like this:

library(stringr) # has a convenient function for splitting to a fixed length 

raw          <- system("ps aux", intern = TRUE)
fields       <- strsplit(raw[1], " +")[[1]]
ps           <- str_split_fixed(raw[-1], " +", n = length(fields))
colnames(ps) <- fields
like image 134
flodel Avatar answered Sep 30 '22 13:09

flodel