System idle time under Linux

H

Hugo Léveillé

I have found it for windows and mac, but no luck under linux. Any idea?
Thanks
 
H

Hugo Léveillé

Thanks, will take a closer look on that

But to get me started, how would you get, via python, the info from that
?

Thanks alot
 
G

Grant Edwards

Thanks, will take a closer look on that

But to get me started, how would you get, via python, the info from that?

Good grief. They're text files. You open them, you read them,
you parse the contents for the stuff you want.
 
T

Thomas Jollans

Thanks, will take a closer look on that

But to get me started, how would you get, via python, the info from that
?

Parse the files. They may be very special files, but they are just files.
 
G

Grant Edwards

Sorry, I am not a linux guy. Did not know it was a text file

And the "file" command (the usual way to figure that out) doesn't
appear to be helpful:

$ file /etc/passwd
/etc/passwd: ASCII text

[That's helpful]

$ file /proc/stat
/proc/stat: empty

[That's not]

However, as usual, Google comes to rescue. The first hit for
"/proc/stat" has a detailed description:

http://www.linuxhowtos.org/System/procstat.htm
 
S

Seebs

I have found it for windows and mac, but no luck under linux. Any idea?

I don't think it's semantically well-defined. What makes a system "idle"?

Is the machine in my basement idle? I don't think anyone's touched the
keyboard in a week, but it's spent a big chunk of that time with 100% CPU
load across all eight processors, and I was running a bunch of work on
it yesterday, including interactive sessions.

Windows and Mac systems *typically* have a well-defined "console" on which
the primary user is active... But as a counterexample, my news reader is
actually running on an OS X box that's about fifty feet from me, which I
connect to via ssh.

I would be very curious to see whether your test for "system idle time"
would realize that the machine I'm currently working on is actively in use,
even though I don't think the console is even logged in...

Basically, I can't help you, but I can tell you that you are quite possibly
asking the wrong question.

-s
 
H

Hugo Léveillé

Good point

One I am looking for, is time since last user mouse or keyboard action.
So I guess I am looking for the exact same thing a screensaver is
looking for
 
S

Seebs

One I am looking for, is time since last user mouse or keyboard action.
So I guess I am looking for the exact same thing a screensaver is
looking for

You can probably get it from X somehow, but... Basically, be aware that
it is entirely possible for a Linux user to invoke python scripts in an
enviromnent where this information cannot be obtained, and may not even
meaningfully exist.

So be sure you fail back gracefully when that happens. Decide in advance
what you will infer from "there is no way to obtain this information". Also
provide an override. Imagine how helpful it would be to a user to have your
script act as though the user had gone idle based on what some OTHER user
was doing!

-s
 
G

Grant Edwards

One I am looking for, is time since last user mouse or keyboard action.
So I guess I am looking for the exact same thing a screensaver is
looking for

Oh. That's not what "idle" generally means in a Unix/Linux context,
so you can disregard previous answers involving /proc/stat et al.
 
L

Lawrence D'Oliveiro

Hugo said:
Sorry, I am not a linux guy. Did not know it was a text file

That’s why I said to check the proc(5) man page for further details.
 
I

Ifrit

Hugo Léveillé heeft het volgende neergekrabbeld:
Thanks, will take a closer look on that

But to get me started, how would you get, via python, the info from that

From a unix command prompt use the cat command to view their contents.
You'll notice that they plain text files with very informative content:

# cat /proc/stat
cpu 3799492 103198 3546212 82899838 1183162 137849 118698 0 0
cpu0 1071799 22089 1141874 20155255 262512 2714 1704 0 0
cpu1 926425 27349 694583 21016324 302881 2488 4253 0 0
cpu2 938331 29131 736412 21113333 202243 123636 104475 0 0
cpu3 862937 24629 973343 20614926 415526 9011 8266 0 0
intr 153564472 28 10 0 0 0 0 0 7 1 3018879 0 0 173 0 0 0 62 55962320 0 0 0 0
0 9620148 749 0 0 0 0 0 0 0 0 0 0 3136950 4742352 320982 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0
ctxt 478141455
btime 1285609820
processes 45111
procs_running 1
procs_blocked 0
softirq 195073836 0 34148873 30536 22265404 4426357 0 55946604 22390786
370319 55494957
 
I

Ifrit

Ifrit heeft het volgende neergekrabbeld:
Hugo Léveillé heeft het volgende neergekrabbeld:


From a unix command prompt use the cat command to view their contents.
You'll notice that they plain text files with very informative content:

# cat /proc/stat

And the man proc command gives info on the format of the content:

/proc/stat

kernel/system statistics. Varies with architecture. Common
entries include:

cpu 3357 0 4313 1362393
The amount of time, measured in units of USER_HZ
(1/100ths of a second on most architectures,
use sysconf(_SC_CLK_TCK) to obtain the right value),
that the system spent in user mode, user
mode with low priority (nice), system mode, and the
idle task, respectively. The last value
should be USER_HZ times the second entry in the uptime
pseudo-file.

In Linux 2.6 this line includes three additional
columns: iowait - time waiting for I/O to
complete (since 2.5.41); irq - time servicing
interrupts (since 2.6.0-test4); softirq - time
servicing softirqs (since 2.6.0-test4).

Since Linux 2.6.11, there is an eighth column, steal -
stolen time, which is the time spent in
other operating systems when running in a virtualized
environment

Since Linux 2.6.24, there is a ninth column, guest,
which is the time spent running a virtual
CPU for guest operating systems under the control of
the Linux kernel.

page 5741 1808
The number of pages the system paged in and the number
that were paged out (from disk).

swap 1 0
The number of swap pages that have been brought in and
out.

intr 1462898
This line shows counts of interrupts serviced since
boot time, for each of the possible system
interrupts. The first column is the total of all
interrupts serviced; each subsequent column
is the total for a particular interrupt.

disk_io: (2,0):(31,30,5764,1,2) (3,0):...
(major,disk_idx):(noinfo, read_io_ops, blks_read,
write_io_ops, blks_written)
(Linux 2.4 only)

ctxt 115315
The number of context switches that the system
underwent.

btime 769041601
boot time, in seconds since the Epoch (January 1,
1970).

processes 86031
Number of forks since boot.

procs_running 6
Number of processes in runnable state. (Linux 2.5.45
onwards.)

procs_blocked 2
Number of processes blocked waiting for I/O to
complete. (Linux 2.5.45 onwards.)
 
J

John Pinner

Good point

One I am looking for, is time since last user mouse or keyboard action.
So I guess I am looking for the exact same thing a screensaver is
looking for

The command

who -Hu).


will give you idle time for each logged-in user

( H - print headers; u - list users )

and you could run this command via subprocess, then read and parse its
output.

Alternatively you could parse the same file(s) as who - I think it
reads /var/run/utmp - but that would be a lot more work, as it is a
binary file (run man utmp to see its format, use the struct module to
decode)

Best wishes,

John
--
 
H

Hugo Léveillé

Ok after some testing, what the who -Hu is giving me is the idle time of
each running open shell. The first line always return a "?" as the IDLE
time.

ex:

NAME LINE TIME IDLE PID COMMENT
vg0619hl :0 2010-09-30 06:10 ? 13091
vg0619hl pts/1 2010-09-30 06:27 00:11 14084 :)0.0)
vg0619hl pts/2 2010-09-30 06:54 . 14084 :)0.0)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top