Discover the possibilities of the or proc directory

download Discover the possibilities of the or proc directory

of 6

Transcript of Discover the possibilities of the or proc directory

  • 8/7/2019 Discover the possibilities of the or proc directory

    1/6

    Tux Training

    A Community Software Documentation Repository

    Discover the possibilities of the / proc directoryApril 15, 2008, 8:41 am

    The /proc directory is a strange beast. It doesnt really exist, yet you canexplore it. Its zero-length files are neither binary nor text, yet you can

    examine and display them. This special directory holds all the details aboutyour Linux system, including its kernel, processes, and configurationparameters. By studying the /proc directory, you can learn how Linux commands work,and you can even do some administrative tasks.

    Under Linux, everything is managed as a file; even devices are accessed as files (in the/dev directory). Although you might think that normal files are either text or binary(or possibly device or pipe files), the /proc directory contains a stranger type: virtualfiles. These files are listed, but dont actually exist on disk; the operating system

    creates them on the fly if you try to read them.

    Most virtual files always have a current timestamp, which indicates that they areconstantly being kept up to date. The /proc directory itself is created every time youboot your box. You need to work as root to be able to examine the whole directory;some of the files (such as the process-related ones) are owned by the user wholaunched it. Although almost all the files are read-only, a few writable ones (notably in

    /proc/sys) allow you to change kernel parameters. (Of course, you must be careful ifyou do this.)

    / proc directory organization

    The /proc directory is organized in virtual directories and subdirectories, and it groupsfiles by similar topic. Working as root, the ls /proc command brings up something likethis:

    1 2432 3340 3715 3762 5441 815 devices modules

    129 2474 3358 3716 3764 5445 acpi diskstats mounts

    1290 248 3413 3717 3812 5459 asound dma mtrr

    133 2486 3435 3718 3813 5479 bus execdomains partitions

    1420 2489 3439 3728 3814 557 dri fb self

    165 276 3450 3731 39 5842 driver filesystems slabinfo

    166 280 36 3733 3973 5854 fs interrupts splash

    2 2812 3602 3734 4 6 ide iomem stat

    2267 3 3603 3735 40 6381 irq ioports swaps

    2268 326 3614 3737 4083 6558 net kallsyms sysrq-trigger

    2282 327 3696 3739 4868 6561 scsi kcore timer_list

    Training Blog Archive Discover the possibilities of the /proc di... http://tuxtraining.com/2008/04/15/discover-the-possibilities-of-the-proc...

    6 29/06/2009 20:50

  • 8/7/2019 Discover the possibilities of the or proc directory

    2/6

    2285 3284 3697 3742 4873 6961 sys keys timer_stats

    2295 329 3700 3744 4878 7206 sysvipc key-users uptime

    2335 3295 3701 3745 5 7207 tty kmsg version

    2400 330 3706 3747 5109 7222 buddyinfo loadavg vmcore

    2401 3318 3709 3749 5112 7225 cmdline locks vmstat

    2427 3329 3710 3751 541 7244 config.gz meminfo zoneinfo

    2428 3336 3714 3753 5440 752 cpuinfo misc

    The numbered directories (more on them later) correspond to each running process; aspecial self symlink points to the current process. Some virtual files provide hardwareinformation, such as /proc/cpuinfo, /proc/meminfo, and /proc/interrupts. Others givefile-related info, such as /proc/filesystems or /proc/partitions. The files under /proc/sysare related to kernel configuration parameters, as well see.

    The cat /proc/meminfo command might bring up something like this:

    # cat /proc/meminfo

    MemTotal: 483488 kB

    MemFree: 9348 kB

    Buffers: 6796 kB

    Cached: 168292 kB

    ...several lines snipped...

    If you try the top or free commands, you might recognize some of these numbers. Infact, several well-known utilities access the /proc directory to get their information. Forexample, if you want to know what kernel youre running, you might try uname -srv,or go to the source and type cat /proc/version. Some other interesting files include:

    / proc/ apm: Provides information on Advanced Power Management, if itsinstalled./ proc/ acpi: A similar directory that offers plenty of data on the more modernAdvanced Configuration and Power Interface. For example, to see if your laptop is

    connected to the AC power, you can use cat /proc/acpi/ac_adapter/AC/state toget either on line or off line./ proc/ cmdl ine: Shows the parameters that were passed to the kernel at boottime. In my case, it contains root=/dev/disk/by-id/scsi-SATA_FUJITSU_MHS2040_NLA5T3314DW3-part3 vga=0317 resume=/dev/sda2splash=silent PROFILE=QuintaWiFi, which tells me which partition is the root ofthe filesystem, which VGA mode to use, and more. The last parameter has to dowith openSUSEs System Configuration Profile Management.

    / proc/ cpuinfo: Provides data on the processor of your box. For example, in mylaptop, cat /proc/cpuinfo gets me a listing that starts with:

    processor : 0

    vendor_id : AuthenticAMD

    cpu family : 6

    Training Blog Archive Discover the possibilities of the /proc di... http://tuxtraining.com/2008/04/15/discover-the-possibilities-of-the-proc...

    6 29/06/2009 20:50

  • 8/7/2019 Discover the possibilities of the or proc directory

    3/6

    model : 8

    model name : Mobile AMD Athlon(tm) XP 2200+

    stepping : 1

    cpu MHz : 927.549

    cache size : 256 KB

    This shows that I have only one processor, numbered 0, of the 80686 family (the6 in cpu family goes as the middle digit): an AMD Athlon XP, running at less than1GHz.

    / proc/ loadavg: A related file that shows the average load on the processor; itsinformation includes CPU usage in the last minute, last five minutes, and last 10minutes, as well as the number of currently running processes./ proc/ stat: Also gives statistics, but goes back to the last boot./ proc/ uptime: A short file that has only two numbers: how many seconds yourbox has been up, and how many seconds it has been idle.

    / proc/ devices: Displays all currently configured and loaded character and blockdevices. /proc/ide and /proc/scsi provide data on IDE and SCSI devices./ proc/ ioports: Shows you information about the regions used for I/Ocommunication with those devices./ proc/ dma: Shows the Direct Memory Access channels in use./ proc/ filesystems: Shows which filesystem types are supported by your kernel.

    A portion of this file might look like this:

    nodev sysfs

    nodev rootfs

    nodev bdev

    nodev proc

    nodev cpuset

    ...some lines snipped...

    nodev ramfs

    nodev hugetlbfs

    nodev mqueue

    ext3

    nodev usbfs

    ext2

    nodev autofs

    The first column shows whether the filesystem is mounted on a block device. In

    my case, I have partitions configured with ext2 and ext3 mounted.

    / proc/ mounts: Shows all the mounts used by your machine (its output looks

    much like /etc/mtab). Similarly, /proc/partitions and /proc/swaps show allpartitions and swap space./ proc/ fs: If youre exporting filesystems with NFS, this directory has among itsmany subdirectories and files /proc/fs/nfsd/exports, which shows the file systemthat are being shared and their permissions.

    Training Blog Archive Discover the possibilities of the /proc di... http://tuxtraining.com/2008/04/15/discover-the-possibilities-of-the-proc...

    6 29/06/2009 20:50

  • 8/7/2019 Discover the possibilities of the or proc directory

    4/6

    / proc/ net: You cant beat this for network information. Describing each file inthis directory would require too much space, but it includes /dev (each networkdevice), several iptables (firewall) related files, net and socket statistics, wirelessinformation, and more.

    There are also several RAM-related files. Ive already mentioned /proc/meminfo, butyouve also got /proc/iomem, which shows you how RAM memory is used in your box,and /proc/kcore, which represents the physical RAM of your box. Unlike most other

    virtual files, /proc/kcore shows a size thats equal to your RAM plus a small overhead.(Dont try to cat this file, because its contents are binary and will mess up yourscreen.) Finally, there are many hardware-related files and directories, such as/proc/interrupts and /proc/irq, /proc/pci (all PCI devices), /proc/bus, and so on, but

    they include very specific information, which most users wont need.

    Whats in a process?

    As I said, the numerical named directories represent all running processes. When aprocess ends, its /proc directory disappears automatically. If you check any of these

    directories while they exist, you will find plenty of files, such as:

    attr cpuset fdinfo mountstats stat

    auxv cwd loginuid oom_adj statm

    clear_refs environ maps oom_score status

    cmdline exe mem root task

    coredump_filter fd mounts smaps wchan

    Lets take a look at the principal files:

    cmdline: Contains the command that started the process, with all its parameters.cw d: A symlink to the current working directory (CWD) for the process; exe linksto the process executable, and root links to its root directory.environ: Shows all environment variables for the process.fd: Contains all file descriptors for a process, showing which files or devices it isusing.maps, statm, and mem: Deal with the memory in use by the process.stat and status: Provide information about the status of the process, but the

    latter is far clearer than the former.

    These files provide several script programming challenges. For example, if you want tohunt for zombie processes, you could scan all numbered directories and check whether(Z) Zombie appears in the /status file. I once needed to check whether a certainprogram was running; I did a scan and looked at the /cmdline files instead, searchingfor the desired string. (You can also do this by working with the output of the pscommand, but thats not the point here.) And if you want to program a better-lookingtop, all the needed information is right at your fingertips.

    Tw eaking the system: / proc/ sys

    /proc/sys not only provides information about the system, it also allows you to changekernel parameters on the fly, and enable or disable features. (Of course, this couldprove harmful to your system consider yourself warned!)

    To determine whether you can configure a file or if its just read-only, use ls -ld; if a file

    Training Blog Archive Discover the possibilities of the /proc di... http://tuxtraining.com/2008/04/15/discover-the-possibilities-of-the-proc...

    6 29/06/2009 20:50

  • 8/7/2019 Discover the possibilities of the or proc directory

    5/6

    has the W attribute, it means you may use it to configure the kernel somehow. Forexample, ls -ld /proc/kernel/* starts like this:

    dr-xr-xr-x 0 root root 0 2008-01-26 00:49 pty

    dr-xr-xr-x 0 root root 0 2008-01-26 00:49 random

    -rw-r--r-- 1 root root 0 2008-01-26 00:49 acct

    -rw-r--r-- 1 root root 0 2008-01-26 00:49 acpi_video_flags

    -rw-r--r-- 1 root root 0 2008-01-26 00:49 audit_argv_kb

    -r--r--r-- 1 root root 0 2008-01-26 00:49 bootloader_type

    -rw------- 1 root root 0 2008-01-26 00:49 cad_pid

    -rw------- 1 root root 0 2008-01-26 00:49 cap-bound

    You can see that bootloader_type isnt meant to be changed, but other files are. To

    change a file, use something like echo 10 >/proc/sys/vm/swappiness. This particularexample would allow you to tune the virtual memory paging performance. By the way,

    these changes are only temporary, and their effects will disappear when you rebootyour system; use sysctl and the /etc/sysctl.conf file to effect more permanent changes.

    Lets take a high-level look at the /proc/sys directories:

    debug: Has (surprise!) debugging information. This is good if youre into kernel

    development.dev: Provides parameters for specific devices on your system; for example, checkthe /dev/cdrom directory.fs: Offers data on every possible aspect of the filesystem.kernel: Lets you affect the kernel configuration and operation directly.

    net: Lets you control network-related matters. Be careful, because messing withthis can make you lose connectivity!vm: Deals with the VM subsystem.

    Conclusion

    The /proc special directory provides full detailed information about the inner workingsof Linux and lets you fine-tune many aspects of its configuration. If you spend sometime learning all the possibilities of this directory, youll be able to get a more perfectLinux box. And isnt that something we all want?

    Source

    No P osts Found

    Print from XP to a Printer Attached to Ubuntu Gutsy or HardyCrack Password Protected zip, rar, 7z, and pdf files in Linux

    Getting started with awkSsh-copy-id and other ssh tipsHow To Add a Welcome Message for SSH Users

    HOWTO: Backup nightly via rsync

    Git CheatsheetZFS Tutorial Part 2Basic Installation of OpenBSDPerl Cheatsheet

    Training Blog Archive Discover the possibilities of the /proc di... http://tuxtraining.com/2008/04/15/discover-the-possibilities-of-the-proc...

    6 29/06/2009 20:50

  • 8/7/2019 Discover the possibilities of the or proc directory

    6/6

    Tags: /proc

    Category: Basics | Comment (RSS)

    | Trackback

    Training Blog Archive Discover the possibilities of the /proc di... http://tuxtraining.com/2008/04/15/discover-the-possibilities-of-the-proc...