Entries Tagged 'System status commands' ↓

runlevel - report previous and current Unix OS runlevel

Hi! If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

runlevel is a basic Unix command aimed to do one simple task: report the runlevel of your Unix OS.

How runlevel command works

Your Unix system carefully logs information about every login session in special files. /var/run/utmp is the file containing information about everyone who's currently logged in, and since every record contains a runlevel information, it makes sense to use this file as a proof of the current OS runlevel.

runlevel command reads /var/run/utmp file and extracts the most recent login entry. It then uses this entry to extract the current and previous runlevel information from it.

Using runlevel command

Simply run the command without any parameters:

redhat$ runlevel
N 5

As it was said earlier, the two numbers shown are supposed to be previous and current Unix runlevels.

However, the previous runlevel information is not usually found in the most recent login entry simply because runlevel hasn't changed,

so the command prints "N" instead of it.

Looking at the output above, you can see that the current runlevel is 5.

See also:

time - system resource usage for running a Unix command

time command is a basic tool in Unix which allows you to keep track of the system resources when running a specified Unix command.

time command in Unix

Sometimes it is quite important to know exactly how much of your system resources are used for running a particular command. This is where the time command can be used. It's a really simple tool which takes any command line as a parameter, runs the command and then reports the system resources usage:

ubuntu# time du -sk /var
4228720 /var

real    0m17.747s
user    0m0.010s
sys     0m0.080s

In this example, a du command is run to gather the cumulative disk space taken up by the /var directory, and a report of used time is presented.

This is what each of these times mean:

  • real - real time, in other words a number of seconds, minutes and sometimes hours and even days it takes for the specified command to complete
  • user -user time, that is the time spent by your OS executing the user code of your command - every instruction of the specified command which was executed in user mode.
  • sys - system time, the amount of time spent by your OS running a system kernel code - instructions in response to the system calls initiated by your command

Real time vs user time vs system time

As you can see, the real time is not a sum of the other two - this is because only the system resources are reported, which is essentially just the CPU time.

Since we ran the I/O intensive command, most of the time it took for du to complete was spent waiting for the I/O operations to complete - as you can see from the example, the CPU time was minimal.

While the necessary file and directory attributes were being read from the disk, both the user time and the system time counters for the command were not clocking anything - your OS process scheduler was busy spending valuable CPU cycles to execute code for other processes.

See also:

top - display tasks and system status in Unix

top is a basic Unix command which is very useful for observing the current state of your Unix system, by default presenting you the list of top users of your system's resources - CPU shares and memory.

Basic usage of the top command

By default, you run top without any parameters, and it shows you a full screen (or full window of your terminal) with the current status of your system and a list of processes using most of its CPU:

ubuntu$ top
top - 13:29:09 up 2 days,  7:13,  4 users,  load average: 0.07, 0.02, 0.00
Tasks: 148 total,   1 running, 147 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.6%us,  0.5%sy,  0.0%ni, 97.3%id,  1.6%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   4051792k total,  4026104k used,    25688k free,   359168k buffers
Swap:  4096492k total,    24296k used,  4072196k free,  2806484k cached
  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 7629 greys     20   0  749m 291m  28m S    1  7.4  16:51.40 firefox
19935 greys     20   0  133m  14m  10m S    0  0.4   2:38.52 smplayer
    1 root      20   0  4020  880  592 S    0  0.0   0:00.96 init
    2 root      15  -5     0    0    0 S    0  0.0   0:00.00 kthreadd
    3 root      RT  -5     0    0    0 S    0  0.0   0:00.04 migration/0
    4 root      15  -5     0    0    0 S    0  0.0   0:00.90 ksoftirqd/0
    5 root      RT  -5     0    0    0 S    0  0.0   0:00.00 watchdog/0
    6 root      RT  -5     0    0    0 S    0  0.0   0:00.06 migration/1
    7 root      15  -5     0    0    0 S    0  0.0   0:01.32 ksoftirqd/1
    8 root      RT  -5     0    0    0 S    0  0.0   0:00.00 watchdog/1
    9 root      15  -5     0    0    0 S    0  0.0   0:02.14 events/0
   10 root      15  -5     0    0    0 S    0  0.0   0:01.44 events/1
   11 root      15  -5     0    0    0 S    0  0.0   0:00.00 khelper
   44 root      15  -5     0    0    0 S    0  0.0   0:01.26 kblockd/0
   45 root      15  -5     0    0    0 S    0  0.0   0:01.98 kblockd/1
   48 root      15  -5     0    0    0 S    0  0.0   0:00.00 kacpid
   49 root      15  -5     0    0    0 S    0  0.0   0:00.00 kacpi_notify
  153 root      15  -5     0    0    0 S    0  0.0   0:00.00 kseriod
  203 root      15  -5     0    0    0 S    0  0.0   0:03.56 kswapd0
  246 root      15  -5     0    0    0 S    0  0.0   0:00.00 aio/0
  247 root      15  -5     0    0    0 S    0  0.0   0:00.00 aio/1
 1595 root      15  -5     0    0    0 S    0  0.0   0:00.00 ksuspend_usbd
 1601 root      15  -5     0    0    0 S    0  0.0   0:00.02 khubd
 1612 root      15  -5     0    0    0 S    0  0.0   0:00.08 ata/0
 1615 root      15  -5     0    0    0 S    0  0.0   0:08.28 ata/1
 1616 root      15  -5     0    0    0 S    0  0.0   0:00.00 ata_aux

Output of the top command explained

These are the elements which default top output consists of:

Unix system uptime and average load

This is the line of top which confirms how many hours (or even days!) your system has been up, shows you the number of logged in users, and reports the average system load numbers for the last minute, 5 minutes and 15 minutes.

top - 13:29:09 up 2 days,  7:13,  4 users,  load average: 0.07, 0.02, 0.00

In this line:

  • 13:29:09 is the current time
  • 2 days, 7:13 is the uptime
  • 4 users shows how many users currently use your system
  • 0.07 - average load for the last minute
  • 0.02 - average load for the last 5 minutes
  • 0.00 - average load for the last 15 minutes

Unix tasks stats

Here you can see how many tasks are currently running on your system. Tasks here mean processes, and the main listing will show you the task names (in the COMMAND column) and the PIDs.

Tasks: 148 total,   1 running, 147 sleeping,   0 stopped,   0 zombie

CPU(s) status

Current CPU state, averaged for the number of CPUs installed in your system, is represented in this line:

Cpu(s):  0.6%us,  0.5%sy,  0.0%ni, 97.3%id,  1.6%wa,  0.0%hi,  0.0%si,  0.0%st

Here are the explanations for each parameter:

  • us - User CPU time. The time the CPU has spent running users’ processes with default priorities
  • sy - System CPU time. The time the CPU has spent running the kernel and its processes
  • ni - Nice CPU time. The time the CPU has spent running users’ proccess that have been prioritized up using nice command
  • wa - I/O wait. Amount of time the CPU has been waiting for I/O operations to complete
  • hi - Hardware IRQ. The amount of time the CPU has been servicing hardware interrupts
  • si - Software Interrupts. The amount of time the CPU has been servicing software interrupts
  • st - Steal Time. The amount of CPU ’stolen’ from this virtual machine by the hypervisor for other tasks (such as running another virtual machine) - a fairly recent addition to the top command, introduced with the increased virtualization focus in modern operating systems

Physical memory usage stats

Memory stats line gives you a summary of how much physical memory you have on your system, and how much of it is currently used or available for the use.

Modern Linux systems are buffering quite a lot for improved performance, which means you rarely get to see all your physical RAM free - the more your system stays up and running, the more of its recently used data ends up being buffered.

In this line, you can see how quite a bit is taken up by the buffers:

Mem:   4051792k total,  4026104k used,    25688k free,   359168k buffers

Swap usage stats

Swap statistics highlight how actively your system uses the swap space - most of it should not be used on a healthy system, although seeing substantial amount of swap memory cached is quite normal. Bear in mind that these are caches held in physical memory, so in this example these 2.8Gb of cached swap is responsible for most of the 4Gb physical RAM taken up and reported as used in the above stats for memory

Swap:  4096492k total,    24296k used,  4072196k free,  2806484k cached

List of the tasks (processes) running on your system

This is the main part of the top output, which looks like this (output is abridged):

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 7629 greys     20   0  749m 291m  28m S    1  7.4  16:51.40 firefox
19935 greys     20   0  133m  14m  10m S    0  0.4   2:38.52 smplayer
    1 root      20   0  4020  880  592 S    0  0.0   0:00.96 init
    2 root      15  -5     0    0    0 S    0  0.0   0:00.00 kthreadd
    3 root      RT  -5     0    0    0 S    0  0.0   0:00.04 migration/0
    4 root      15  -5     0    0    0 S    0  0.0   0:00.90 ksoftirqd/0
    5 root      RT  -5     0    0    0 S    0  0.0   0:00.00 watchdog/0
    6 root      RT  -5     0    0    0 S    0  0.0   0:00.06 migration/1

As you can see from this list, you're given all the vital information about each process running on your Unix system:

  • PID - process ID
  • USER - username for the owner of each process
  • PR - process priority (RT means a Real Time priority class - used for system processes)
  • NI - priority set by nice utility
  • VIRT - the amount of virtual memory used by a process: code, data and shared libraries plus pages that have been swapped out
  • RES - the resident part of a process - how much of it resides in the physical memory (non-swapped memory)
  • SHR - shows you the size of potentially shared memory segments for a process
  • S - the current state of each process
  • %CPU - percentage of the time shares CPU spends running a particular process
  • %MEM - percentage of the physical memory of your system which is used by each process
  • %TIME+ - total time CPUs spent running each process
  • COMMAND - a command used to initiate each process.

I'll be sure to revisit and expand this page at some later stage.

See also:

date - print or set the system date and time

date is a basic Unix command for getting or setting the current time and date on your system. Because it's the easiest way to get current time, this command is extensively used in Unix scripting.

Getting current time and date in your Unix system

The default usage of this command is simple and requires no additional command line parameters:

ubuntu$ date
Wed Jun 11 11:43:52 IST 2008

Continue reading →

who - find out who is on the system

who is one of basic Unix commands, which allows you to quickly see who else is logged in.

who - default behaviour

When you run who without any parameters, it returns you a list of users on your Unix system, along with terminals they're using, the time of the start for each session, and the hostnames where these users are logged in from.

Continue reading →

uptime - find out how long the Unix system has been up

uptime is one of the basic Unix commands which allows you to quickly confirm how long your Unix system has been up and running since it was last rebooted or powered on.

This is how you use it:

bash-2.05b$ uptime
 11:18:23  up 83 days, 18:29,  4 users,  load average: 0.16, 0.03, 0.01

Continue reading →

uname - print Unix system information

uname is one of the most useful commands when it comes to gathering basic information about your system. You can use it to find out the hostname of the system you're on, the hardware architectures supported by the currently used kernel and the exact release of your system.

Basic uname usage

uname -n

This command shows you the node (host) name of your system:

bash-3.00$ uname -n
samplehost

Continue reading →