Saturday, January 05, 2008

Fun with linux: get system info with /proc(/proc filesystem)

One fun thing on linux, in a way, is that, it is easy to access system information. And even write script to read it.

The way to see this is via the proc file system. Which is in /proc.
/proc file system is a virtual filesystem that reflect kernel states.

like any filesystem you can "ls" in it. First thing you see is a set of numbers, that is actually the process of the system. And a few thing as well. One thing about the /proc filesystem is that, some make sense to human, other is not quite.

Take an example, inside /proc, have cpuinfo.
cat /proc/cpuinfo
You should see the cpu information.
Another is
cat /proc/uptime
the information is not quite obvious.

Just now I show one can do with it already. In /proc, files act like files, text file to be precise, which is editable, but not recommended, it can mess up the system. So you can cat it, you can write script to read it. For example, in python
for l in f:
print l
And it applies to all other programming language that run linux, and can read files. So you can write program for it. Or get system information. For example, to get cpu temperature for a laptop, in my case, it runs a amd64 x2, I do something like this
cat /proc/acpi/thermal_zone/tsz0/temperature
And some desklets use this to get system info too.

No comments:

Post a Comment