Lets say I want to extract current State
(D/R/S/T/Z) and command Name
from processes with given PID
.
For example I have this code:
pids=$(pidof firefox) #returns all PID-s of firefox processes (random example)
for v in ${pids[*]}
do
echo "PID: $v" #here I also want to write status and name of the command (PID, Name, Status)
done
more /proc/$$/status
returns
Name: bash
Umask: 002
State: S (sleeping)
....
Pid: 3234
...
From all that information I only need some. I want to know how to extract desired property (Name
, Status
,…) from proc
.
3
Answers
Using
awk
to pull out theName
andState
:Fixing issues with OP’s code and pulling the
awk
script into the mix:Running against OP’s single example this should generate:
To avoid repeated calls to
awk
in the possibility that you have a lot of elements (unlikely, but hey, maybe it’s a server of something…), but mostly as an exercise…One-liners are ugly – breaking it out –
(I don’t actually have pidof or firefox on this laptop, so for full transparency I made a file with a couple pids and used
cat lst
instead ofpidof firefox
, so will just use that below.)Using /proc/{pid1, pid2}/status and
grep
:Sample output: