PDA

View Full Version : QProcess to get CPU state



sergiofil
10th August 2011, 10:28
Hi,

i want show in my Qt Application the CPU state. So, i do:

. Create a script ( top -n 1 | grep Cpu | awk '{print $2}' ):
. when i execute the script in shell, i get ( for example): 5.9%us,

. In Qt, i create a QProcess when to get the CPU state:
. process->start("/home/script");

I receive Started() and finished(int, Qprocess::ExisStatus) Signals, but not the Signal readyReadStandardOutput() .





So, if when i execute the script in shell i get some output, when i execute process in Qt i should get outuput ??

To check if i was implement my process correct in Qt, i change my script to: (ls -lA), and in Qt i get a list of files as i execute the script in terminal.


Can anyone help me to fix this problem??

Thanks, Sergio Silva

sakya
10th August 2011, 14:07
Don't use pipe:
http://lists.trolltech.com/qt-interest/2002-05/msg00049.html

EDIT: Sorry, I did't read you're using a script.

sergiofil
26th August 2011, 16:53
hi, after return from vacation...

when the process was implemented i searched an read that were some problems with pipe, so i did a scipt.


Can anyone help me to fix this problem??

Thanks, Sergio Silva

wysota
26th August 2011, 23:25
Does your script contain a shebang? Which interpreter?

sergiofil
27th August 2011, 11:34
Does your script contain a shebang? Which interpreter?

My script contains the following: top -n 1 | grep Cpu | awk '{print $2}'
and it is a plain text document with permissions to all users.

As i said in first message, when i open a shell and execute the script, the output is: 4.8%us, (for example).

wysota
27th August 2011, 14:03
Well, here you miss one important step of the process -- you are not opening a shell. That's why I'm asking about a shebang.

sergiofil
27th August 2011, 18:56
wysota,

i did not know which was a shebang before you talk about it :p

so, after learn what is and what does, i change my script.
Now my script have:
#!/bin/bash
top -n 1 | grep Cpu | awk '{print $2}'

but unfortunately still doesn't work.

SixDegrees
27th August 2011, 19:42
What happens if you give top the 'b' switch?

top -nb 1 | grep Cpu | awk '{print $2}'

wysota
27th August 2011, 21:35
wysota,

i did not know which was a shebang before you talk about it :p

so, after learn what is and what does, i change my script.
Now my script have:
#!/bin/bash
top -n 1 | grep Cpu | awk '{print $2}'

but unfortunately still doesn't work.

It will not work, top requires a tty when ran like this (see output on stderr).

sergiofil
27th August 2011, 22:05
What happens if you give top the 'b' switch?

top -nb 1 | grep Cpu | awk '{print $2}'

i out b parameter:

#!/bin/bash
top -b -n 1 | grep Cpu | awk '{print $2}'

and it WORKS!!!

Thanks SixDegrees and wysota for help!