$ ps -p 31690 -o %cpu,%mem
%CPU %MEM
80.3 0.0
鉴于上述结果,我想要一个 csv,如下所示:
AAA,80.3,0.0
在这样的 shell 脚本中使用:
for i in { 1..31 }
do
aaa=$i
// call ps and write result to a csv
done
答案1
#!/bin/bash
for aaa in {1..31} ;do
echo $aaa$(ps --no-headers -p "$aaa" -o %cpu,%mem | tr -s ' ' ',') #>>/tmp/output.csv
done
答案2
一个可能的解决方案 - 我相信这个稍微更优雅一些(重要的是我(简要地)测试了它以确保它确实有效)。
#! /bin/bash
# This allows you to specify the PID you are interested in at the command prompt, as I expect hard coding is not a good idea.
if [ "$1." == "." ]
then
echo Usage: $0 pid
exit 1
fi
for each in {1..31}
do
echo ${each}$(ps -p $1 --noheaders -o %cpu,%mem| tr -s ' ' ',') >> /tmp/file.name
done