On 2015-07-03 21:46, wolfi323 wrote:
Ok, I’m trying this. I wrote the script this way:
#!/bin/bash
while true
do
# read Firefox's container plugin cpu usage
DATE=`date --rfc-3339=seconds`
DATE=`date --iso=ns`
ret=`top -b -n1 -u $(whoami) | awk '$12 ~ /plugin-containe/ {SUM += $9} END {print int(SUM)}'`
if -n "$ret" ] && "$ret" -gt 5 ]; then
echo "$DATE Simulando actividad ($ret)"
dbus-send --type=method_call --dest=org.freedesktop.ScreenSaver \
/org/freedesktop/ScreenSaver org.freedesktop.ScreenSaver.SimulateUserActivity
else
echo "$DATE No hago nada ($ret)"
fi
sleep 50
done
Then I started plying something, but it detects no activity. Thus I tried separately:
cer@minas-tirith:~> top -b -n1 -u $(whoami) | grep plugin-con
30058 cer 20 0 930416 132748 35216 0 132748 R 69.68 3.354 2:08.77 plugin-containe
cer@minas-tirith:~> top -b -n1 -u $(whoami) | awk '$12 ~ /plugin-containe/ {SUM += $9} END {print int(SUM)}'
0
cer@minas-tirith:~>
You see, CPU is about 70%, yet the code says it is zero.
I don’t know awk, so I can’t trace the issue :-?
I think I know. My “top” is configured to print other fields:
top - 22:55:17 up 11 days, 8 min, 30 users, load average: 0.49, 1.09, 1.02
Tasks: 288 total, 1 running, 287 sleeping, 0 stopped, 0 zombie
%Cpu(s): 13.2 us, 3.2 sy, 0.0 ni, 80.5 id, 3.2 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem: 3957996 total, 2170292 used, 1787704 free, 64372 buffers
KiB Swap: 6289412 total, 1270268 used, 5019144 free, 876920 cached
PID USER PR NI VIRT RES SHR SWAP USED S %CPU %MEM TIME+ COMMAND
8256 cer 20 0 2289440 532404 28488 295976 828380 S 12.88 13.45 185:56.74 firefox
So, alternatives:
- customizing the awk line so that it reads the proper field for “me”.
- same, but first it has to read columns headers to locate the right one.
- use something else altogether. Perhaps “ps --format”.
This one looks promising:
ps -xo pcpu,user,fname,comm
Further tuning is possible, but I can’t quite get it.
cer@minas-tirith:~> ps -xo pcpu,user,fname,comm | grep plugin
51.3 cer plugin-c plugin-containe
cer@minas-tirith:~>
cer@minas-tirith:~> ps -C plugin-containe -xo pcpu,user,fname,comm
error: must set personality to get -x option
Usage:
ps [options]
Try 'ps --help <simple|list|output|threads|misc|all>'
or 'ps --help <s|l|o|t|m|a>'
for additional help text.
For more details see ps(1).
cer@minas-tirith:~>
cer@minas-tirith:~> ps -C plugin-containe
PID TTY TIME CMD
30785 ? 00:02:16 plugin-containe
cer@minas-tirith:~>
You see, it is possible to tell ps to print data for a single process by name;
and it is also possible to print the fields we want and in the order we want;
But I can’t do both. :-?
–
Cheers / Saludos,
Carlos E. R.
(from 13.1 x86_64 “Bottle” (Minas Tirith))