I am trying to make a program that parse output from the Linux command ps aux
where the output can look like this:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 2 0.0 0.0 0 0 ? S 20:04 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? I< 20:04 0:00 [rcu_gp]
root 4 0.0 0.0 0 0 ? I< 20:04 0:00 [rcu_par_gp]
root 397 0.0 0.1 95112 15236 ? S<s 20:04 0:00 /lib/systemd/systemd-journald
root 431 0.0 0.0 46588 4640 ? Ss 20:04 0:01 /lib/systemd/systemd-udevd
and so on...
我像这样运行ps aux:
def run_ps_aux()
ps_aux = `ps aux`
return run_ps_aux
end
puts run_ps_aux()
但是我只对CPU,MEM和COMMAND感兴趣。我想要这样的输出:
命令:CPU:内存
命令:CPU:内存
命令:CPU:内存
等等...
另外,是否可以排序,所以我只会得到CPU使用率最高的3个命令?
感谢所有帮助!