uptime

You just logged on to the server and see some things that were supposed to be running are not. Perhaps the processes were killed or perhaps all processes were killed by a shutdown. Instead of guessing, find out if the server was indeed rebooted with the uptime command. The command shows the length of time the server has been up since the last reboot.

# uptime
 16:43:43 up 672 days, 17:46,   45 users,  load average: 4.45,  5.18, 5.38
The output shows much useful information. The first column shows the current time when the command was executed. The second portion – up 672 days, 17:46 – shows the amount of time the server has been up. The numbers 17:46 depict the hour and minutes. So this server has been up for 672 days, 17 hours, and 46 minutes as of now.

The next item – 45 users – shows how many users are logged in to the server right now.

The last bits of the output show how much has been the load average of the server in the last 1, 5, and 15 minutes respectively. The term “load average” is a composite score that determines the load on the system based on CPU and I/O metrics. The higher the load average, the more the load on the system. It’s not based on a scale; unlike percentages it does not end at a fixed number such as 100. In addition, load averages of two systems can’t be compared. It is a number to quantify load on a system and relevant in that system alone. This output shows that the load average was 4.45 in the last 1 min, 5.18 in the 5 last mins, and so on.

The command does not have any options or accept any parameter other than -V, which shows the version of the command.

# uptime -V
procps version 3.2.3
Usage for Oracle Users
There is no clear Oracle-specific use of this command, except that you can find out the load on the system to explain some performance issues. If you see some performance issues on the database, and you trace it to high CPU or I/O load, you should immediately check the load averages using the uptime command. If you see a high load average, your next course of action is to dive down deep below the surface to find the root cause. To perform that deep dive, you have in your arsenal tools like mpstat, iostat, and sar (covered in this installment of this series).

Consider an output as shown below:

# uptime
 21:31:04 up 330 days,   7:16,  4 users,  load average: 12.90, 1.03, 1.00
It’s interesting as the load average was very high (12.90) in the last 1 minute but has been pretty low, even irrelevant, at 1.03 and 1.00 for 5 minutes and 15 minutes respectively. What does it mean? It proves that in less than 5 minutes, some process started that caused the load average to jump up for the last minute. This process was not present earlier because the previous load averages were so small. This analysis leads us to focus on the processes that kicked off during the last few minutes – speeding up the resolution process.

Of course, since it shows how long the server has been up, it also explains why the instance has been up since then.


who

Who is logged in the system right now? That’s a common question you might want to ask, especially when you are tracking down an errant user running some resource consuming commands.

The who command answers that question. Here is the simplest usage without any arguments or parameters.

# who
oracle   pts/2        Jan  8 15:57  (10.14.105.139)
oracle   pts/3        Jan  8 15:57  (10.14.105.139)
root     pts/1        Dec 26 13:42  (:0.0)
root     :0           Oct 23 15:32
The command can take several options. The -s option is the default; it produces the same output as the above.

Looking at the output, you might be straining your memory to remember what the columns are meant to be. Well, relax. You can use the -H option to display the header:

# who -H
NAME     LINE         TIME         COMMENT
oracle   pts/2        Jan  8 15:57  (10.14.105.139)
oracle   pts/3        Jan  8 15:57  (10.14.105.139)
root     pts/1        Dec 26  13:42 (:0.0)
root     :0           Oct 23  15:32
Now the meanings of the columns are clear. The column NAME shows the username of the logged in user. LINE shows the terminal name. In Linux each connection is labeled as a terminal with the naming convention pts/ where is a number starting with 1. The :0 terminal is a label for X terminal. TIME shows when they first logged in. COMMENTS shows the IP address where they logged in from.

What if you just want a list of names of users instead of all those extraneous details? The -q option accomplishes that. It displays the names of users on one line, sorted alphabetically. It also displays a count of total number of users at the end (45 in this case):

# who -q
ananda ananda jsmith klome  oracle oracle root root … and so on for  45 names
# users=45
Some users could be just logged on but actually doing nothing. You can check how long they have been idle, a command especially useful if you are the boss, by using the -u option.

# who -uH
NAME     LINE         TIME          IDLE          PID COMMENT
oracle   pts/2        Jan  8 15:57   .          18127 (10.14.105.139)
oracle   pts/3        Jan  8 15:57  00:26       18127 (10.14.105.139)
root     pts/1        Dec 26 13:42   old         6451 (:0.0)
root     :0           Oct 23 15:32    ?         24215
The new column IDLE shows how long they have been idle in hh:mm format. Note the value “old” in that column? It means that the user has been idle for more than 1 day. The PID column shows the process ID of their shell connection.

Another useful option is -b that shows when the system was rebooted.

# who -b
         system boot  Feb 15  13:31
It shows the system was booted on Feb 15th at 1:31 PM. Remember the uptime command? It also shows you how long this system has been up. You can subtract the days shown in uptime to know the day of the boot. The who -b command makes it much simpler; it directly shows you the time of the boot.

Very Important Caveat: The who -b command shows the month and date only, not the year. So if the system has been up longer than a year, the output will not reflect the correct value. Therefore uptime is always a preferred approach, even if you have to do a little calculation. Here is an example:

# uptime
 21:37:49 up 675 days, 22:40,   1 user,  load average: 3.35,  3.08, 2.86
# who -b
         system boot   Mar  7 22:58
Note the boot time shows as March 7. That’s in 2007, not 2008! The uptime shows the correct time – it has been up for 675 days. If subtractions are not your forte you can use a simple SQL to get that date 675 days ago:

SQL> select sysdate – 675  from dual;
SYSDATE-6
———
07-MAR-07
The -l option shows the logons to the system:

# who -lH
NAME     LINE         TIME         IDLE          PID COMMENT
LOGIN    tty1         Feb 15  13:32              4081 id=1
LOGIN    tty6         Feb 15  13:32              4254 id=6
To find out the user terminals that have been dead, use the -d option:

# who -dH
NAME     LINE         TIME         IDLE          PID COMMENT  EXIT
                      Feb 15  13:31               489 id=si     term=0 exit=0
                      Feb 15  13:32              2870 id=l5     term=0 exit=0
         pts/1        Oct 10  14:53             31869 id=ts/1  term=0 exit=0
         pts/4        Jan 11  00:20             22155 id=ts/4  term=0 exit=0
         pts/3        Jun 29  16:01                 0 id=/3    term=0 exit=0
         pts/2         Oct 4  22:35              8371 id=/2    term=0 exit=0
         pts/5        Dec 30  03:15              5026 id=ts/5  term=0 exit=0
         pts/4        Dec 30  22:35                 0 id=/4    term=0 exit=0
Sometimes the init process (the process that starts first when the system is booted) kicks off other processes. The -p option shows all those logins that are active.

# who -pH
NAME     LINE         TIME                PID COMMENT
                      Feb 15 13:32       4083 id=2
                      Feb 15 13:32       4090 id=3
                      Feb 15 13:32       4166 id=4
                      Feb 15 13:32       4174 id=5
                      Feb 15 13:32       4255 id=x
                      Oct  4 23:14      13754 id=h1
Later in this installment, you will learn about a command – write – that enables real time messaging. You will also learn how to disable others’ ability to write to your terminal (the mesg command). If you want to know which users do and do not allow others to write to their terminals, use the -T option:

# who -TH
NAME       LINE          TIME         COMMENT
oracle   + pts/2        Jan 11 12:08  (10.23.32.10)
oracle   + pts/3        Jan 11 12:08  (10.23.32.10)
oracle   – pts/4        Jan 11 12:08  (10.23.32.10)
root     + pts/1        Dec 26 13:42  (:0.0)
root     ? :0           Oct 23 15:32
The + sign before the terminal name means the terminal accepts write commands from others; the “-“ sign means that the terminal does not allow. The “?” in this field means the terminal does not support writing to it, e.g. an X-window session.

The current run level of the system can be obtained by the -r option:

# who -rH
NAME     LINE         TIME         IDLE          PID COMMENT
         run-level 5  Feb 15  13:31                   last=S
A more descriptive listing can be obtained by the -a (all) option. This option combines the -b -d -l -p -r -t -T -u options. So these two commands produce the same result:

# who  -bdlprtTu
# who -a
Here is a sample output (with the header, so that you can understand the columns better):

# who -aH
NAME       LINE          TIME         IDLE          PID COMMENT  EXIT
                        Feb 15 13:31               489 id=si     term=0 exit=0
           system boot  Feb 15 13:31
           run-level 5  Feb 15 13:31                   last=S
                        Feb 15 13:32              2870 id=l5     term=0 exit=0
LOGIN      tty1         Feb 15 13:32              4081 id=1
                        Feb 15 13:32              4083 id=2
                        Feb 15 13:32              4090 id=3
                        Feb 15 13:32              4166 id=4
                        Feb 15 13:32              4174 id=5
LOGIN      tty6         Feb 15 13:32              4254 id=6
                        Feb 15 13:32              4255 id=x
                        Oct  4 23:14             13754 id=h1
           pts/1        Oct 10 14:53             31869 id=ts/1  term=0 exit=0
oracle   + pts/2        Jan  8 15:57   .         18127 (10.14.105.139)
oracle   + pts/3        Jan  8 15:57  00:18      18127 (10.14.105.139)
           pts/4        Dec 30 03:15              5026 id=ts/4  term=0 exit=0
           pts/3        Jun 29 16:01                 0 id=/3    term=0 exit=0
root     + pts/1        Dec 26 13:42  old         6451 (:0.0)
           pts/2        Oct  4 22:35              8371 id=/2     term=0 exit=0
root     ? :0           Oct 23 15:32   ?         24215
           pts/5        Dec 30 03:15              5026 id=ts/5  term=0 exit=0
           pts/4        Dec 30 22:35                 0 id=/4    term=0 exit=0
To find out your own login, use the -m option:

# who -m
oracle   pts/2        Jan  8 15:57  (10.14.105.139)
Note the pts/2 value? That’s the terminal number. You can find your own terminal via the tty command:

# tty
/dev/pts/2
There is a special command structure in Linux to show your own login – who am i. It produces the same output as the -m option.

# who am i
oracle   pts/2        Jan  8 15:57  (10.14.105.139)

The only arguments allowed are “am i” and “mom likes” (yes, believe it or not!). Both produce the same output,

 
(Extracted from oracle technet notes author Arup Nanda)