Showing posts with label Solaris_Linux. Show all posts
Showing posts with label Solaris_Linux. Show all posts

Monday, October 10, 2011

Shell script to notify Hard disk space utilization

So many people facing issue high CPU utilization in production environment.I am posting one script which are use full for notify in your application environment




Shell script to notify Hard disk space utilization


The below script will check the hard disk space usage, using the command du and send an email alert if any of the partition is using more than 85% of space.

vim disk-usage.sh

#!/bin/sh
df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }' | while read output;
do
  echo $output
  usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1  )
  partition=$(echo $output | awk '{ print $2 }' )
  if [ $usep -ge 85 ]; then
    echo "Running out of space \"$partition ($usep%)\" on $(hostname) as on $(date)" |
     mail -s "Alert: Almost out of disk space $usep%" example@example.com
  fi
done




Set a cronjob to run this script.
*/30 * * * * /bin/sh /path-to-file/disk-usage.sh


                                           ***your comment would be appreciable***

Tuesday, September 20, 2011

Some Solaris Command's

  alias    Create an alias
  apropos  Search Help manual pages (man -k)
  apt-get  Search for and install software packages (Debian)
  aspell   Spell Checker
  awk      Find and Replace text, database sort/validate/index
b
  bash     GNU Bourne-Again SHell 
  bc       Arbitrary precision calculator language 
  bg       Send to background
  break    Exit from a loop
  builtin  Run a shell builtin
  bzip2    Compress or decompress named file(s)
c
  cal      Display a calendar
  case     Conditionally perform a command
  cat      Display the contents of a file
  cd       Change Directory
  cfdisk   Partition table manipulator for Linux
  chgrp    Change group ownership
  chmod    Change access permissions
  chown    Change file owner and group
  chroot   Run a command with a different root directory
  chkconfig System services (runlevel)
  cksum    Print CRC checksum and byte counts
  clear    Clear terminal screen
  cmp      Compare two files
  comm     Compare two sorted files line by line
  command  Run a command - ignoring shell functions
  continue Resume the next iteration of a loop
  cp       Copy one or more files to another location
  cron     Daemon to execute scheduled commands
  crontab  Schedule a command to run at a later time
  csplit   Split a file into context-determined pieces
  cut      Divide a file into several parts
d
  date     Display or change the date & time
  dc       Desk Calculator
  dd       Convert and copy a file, write disk headers, boot records
  ddrescue Data recovery tool
  declare  Declare variables and give them attributes
  df       Display free disk space
  diff     Display the differences between two files
  diff3    Show differences among three files
  dig      DNS lookup
  dir      Briefly list directory contents
  dircolors Colour setup for `ls'
  dirname  Convert a full pathname to just a path
  dirs     Display list of remembered directories
  dmesg    Print kernel & driver messages 
  du       Estimate file space usage
e
  echo     Display message on screen
  egrep    Search file(s) for lines that match an extended expression
  eject    Eject removable media
  enable   Enable and disable builtin shell commands
  env      Environment variables
  ethtool  Ethernet card settings
  eval     Evaluate several commands/arguments
  exec     Execute a command
  exit     Exit the shell
  expect   Automate arbitrary applications accessed over a terminal
  expand   Convert tabs to spaces
  export   Set an environment variable
  expr     Evaluate expressions
f
  false    Do nothing, unsuccessfully
  fdformat Low-level format a floppy disk
  fdisk    Partition table manipulator for Linux
  fg       Send job to foreground 
  fgrep    Search file(s) for lines that match a fixed string
  file     Determine file type
  find     Search for files that meet a desired criteria
  fmt      Reformat paragraph text
  fold     Wrap text to fit a specified width.
  for      Expand words, and execute commands
  format   Format disks or tapes
  free     Display memory usage
  fsck     File system consistency check and repair
  ftp      File Transfer Protocol
  function Define Function Macros
  fuser    Identify/kill the process that is accessing a file
g
  gawk     Find and Replace text within file(s)
  getopts  Parse positional parameters
  grep     Search file(s) for lines that match a given pattern
  groups   Print group names a user is in
  gzip     Compress or decompress named file(s)
h
  hash     Remember the full pathname of a name argument
  head     Output the first part of file(s)
  history  Command History
  hostname Print or set system name
i
  id       Print user and group id's
  if       Conditionally perform a command
  ifconfig Configure a network interface
  ifdown   Stop a network interface 
  ifup     Start a network interface up
  import   Capture an X server screen and save the image to file
  install  Copy files and set attributes
j
  join     Join lines on a common field
k
  kill     Stop a process from running
  killall  Kill processes by name
l
  less     Display output one screen at a time
  let      Perform arithmetic on shell variables
  ln       Make links between files
  local    Create variables
  locate   Find files
  logname  Print current login name
  logout   Exit a login shell
  look     Display lines beginning with a given string
  lpc      Line printer control program
  lpr      Off line print
  lprint   Print a file
  lprintd  Abort a print job
  lprintq  List the print queue
  lprm     Remove jobs from the print queue
  ls       List information about file(s)
  lsof     List open files
m
  make     Recompile a group of programs
  man      Help manual
  mkdir    Create new folder(s)
  mkfifo   Make FIFOs (named pipes)
  mkisofs  Create an hybrid ISO9660/JOLIET/HFS filesystem
  mknod    Make block or character special files
  more     Display output one screen at a time
  mount    Mount a file system
  mtools   Manipulate MS-DOS files
  mv       Move or rename files or directories
  mmv      Mass Move and rename (files)
n
  netstat  Networking information
  nice     Set the priority of a command or job
  nl       Number lines and write files
  nohup    Run a command immune to hangups
  nslookup Query Internet name servers interactively
o
  open     Open a file in its default application
  op       Operator access 
p
  passwd   Modify a user password
  paste    Merge lines of files
  pathchk  Check file name portability
  ping     Test a network connection
  popd     Restore the previous value of the current directory
  pr       Prepare files for printing
  printcap Printer capability database
  printenv Print environment variables
  printf   Format and print data
  ps       Process status
  pushd    Save and then change the current directory
  pwd      Print Working Directory
q
  quota    Display disk usage and limits
  quotacheck Scan a file system for disk usage
  quotactl Set disk quotas
r
  ram      ram disk device
  rcp      Copy files between two machines
  read     read a line from standard input
  readonly Mark variables/functions as readonly
  reboot   Reboot the system
  renice   Alter priority of running processes 
  remsync  Synchronize remote files via email
  return   Exit a shell function
  rev      Reverse lines of a file
  rm       Remove files
  rmdir    Remove folder(s)
  rsync    Remote file copy (Synchronize file trees)
s
  screen   Multiplex terminal, run remote shells via ssh
  scp      Secure copy (remote file copy)
  sdiff    Merge two files interactively
  sed      Stream Editor
  select   Accept keyboard input
  seq      Print numeric sequences
  set      Manipulate shell variables and functions
  sftp     Secure File Transfer Program
  shift    Shift positional parameters
  shopt    Shell Options
  shutdown Shutdown or restart linux
  sleep    Delay for a specified time
  slocate  Find files
  sort     Sort text files
  source   Run commands from a file `.'
  split    Split a file into fixed-size pieces
  ssh      Secure Shell client (remote login program)
  strace   Trace system calls and signals
  su       Substitute user identity
  sudo     Execute a command as another user
  sum      Print a checksum for a file
  symlink  Make a new name for a file
  sync     Synchronize data on disk with memory
t
  tail     Output the last part of files
  tar      Tape ARchiver
  tee      Redirect output to multiple files
  test     Evaluate a conditional expression
  time     Measure Program running time
  times    User and system times
  touch    Change file timestamps
  top      List processes running on the system
  traceroute 
Trace Route
to Host
  trap     Run a command when a signal is set(bourne)
  tr       Translate, squeeze, and/or delete characters
  true     Do nothing, successfully
  tsort    Topological sort
  tty      Print filename of terminal on stdin
  type     Describe a command
u
  ulimit   Limit user resources
  umask    Users file creation mask
  umount   Unmount a device
  unalias  Remove an alias
  uname    Print system information
  unexpand Convert spaces to tabs
  uniq     Uniquify files
  units    Convert units from one scale to another
  unset    Remove variable or function names
  unshar   Unpack shell archive scripts
  until    Execute commands (until error)
  useradd  Create new user account
  usermod  Modify user account
  users    List users currently logged in
  uuencode Encode a binary file 
  uudecode Decode a file created by uuencode
v
  v        Verbosely list directory contents (`ls -l -b')
  vdir     Verbosely list directory contents (`ls -l -b')
  vi       Text Editor
  vmstat   Report virtual memory statistics
w
  watch    Execute/display a program periodically
  wc       Print byte, word, and line counts
  whereis  Report all known instances of a command    
  which    Locate a program file in the user's path. 
  while    Execute commands
  who      Print all usernames currently logged in
  whoami   Print the current user id and name (`id -un')
  Wget     Retrieve web pages or files via HTTP, HTTPS or FTP
  write    Send a message to another user 
x
  xargs    Execute utility, passing constructed argument list(s)
  yes      Print a string until interrupted
  .        Run a command script in the current shell
  ###      Comment / Remark

Sunday, September 18, 2011

Open SSH

Open SSH

SSH relies upon the proper installation of SSL.

[root@client ~]# which ssh
/usr/bin/ssh
[root@client ~]# ls -l /usr/bin/ssh
-rwxr-xr-x  1 root root 240488 Nov 30  2004 /usr/bin/ssh
[root@client ~]# rpm -qf /usr/bin/ssh
openssh-clients-3.9p1-8.RHEL4.1
[root@server ssh]# rpm -qa | grep -i ssh
openssh-3.9p1-8.RHEL4.1
openssh-askpass-3.9p1-8.RHEL4.1
openssh-clients-3.9p1-8.RHEL4.1
openssh-server-3.9p1-8.RHEL4.1
openssh-askpass-gnome-3.9p1-8.RHEL4.1
[root@client ~]# ssh
usage: ssh [-1246AaCfghkMNnqsTtVvXxY] [-b bind_address] [-c cipher_spec] [-D port] [-e escape_char] [-F configfile] [-i identity_file] [-L port:host:hostport] [-l login_name] [-m mac_spec] [-o option] [-p port] [-R port:host:hostport] [-S ctl] [user@]hostname [command]
[root@client ~]# netstat -ntlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q     Local Address     Foreign Address State    PID/Program name
Tcp         0      0          0.0.0.0:32769     0.0.0.0:*       LISTEN   1890/rpc.statd
Tcp         0      0          0.0.0.0:111 0.0.0.0:*        LISTEN   1870/portmap
Tcp         0           0           127.0.0.1:631     0.0.0.0:*           LISTEN   2012/cupsd
Tcp         0           0           127.0.0.1:25      0.0.0.0:*           LISTEN   2082/sendmail: acce
Tcp         0           0           :::22             :::*            LISTEN   2048/sshd
-n – shows numerical address
-t – shows only tcp related connection
-l – shows only listening sockets
-p - Show the PID and name of the program to which each socket belongs.
[root@client ~]# ssh server.linux.com
The authenticity of host 'server.linux.com (192.168.226.129)' can't be established.
RSA key fingerprint is 4c:c0:9b:19:26:70:54:59::a9:7e:.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'server.linux.com,192.168.226.129' (RSA) to the list of known hosts.
root@server.linux.com's password:
Last login: Tue Dec 19  2006
-bash: /usr/lib/vmware-tools/bin32: is a directory
[root@server ~]#
The above ssh command connects to the system server.linux.com and prompts you to enter the password. Note that the ssh actually prompts for the password and not the login, as it automatically assumes you are trying to connect to the remote system as the current logged in user of your system.

[root@client ~]# ls -l .ssh/
total 8
-rw-r--r--  1 root root 242 Dec 19  known_hosts

The hidden directory in the home directory of the user in the local system who has used ssh to connect to a remote server has all the files and public keys stored in a file known_hosts.

Ssh relies on PKI (Public Key Infrastructure). PKI relies upon asymmetric cryptography. What it means is that there are 2 keys that are used for communications. One is considered as the Public Key and the other a Private Key. Each ssh daemon creates its own secret private key. That private key also has a corresponding public key. The public key is what we store as ssh clients in the local systems known_hosts file. We can then encrypt information using the stored public key, which only the unique owner of the public key can decrypt using the servers private key.

[root@client ~]# cat .ssh/known_hosts
server.linux.com,192.168.226.129 ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAIEAyJo1z2nO0NRMN4zAFuwTfzMihwwPVuQxbH/XnSMbCwfvxMBAdUEcmbZbn6q0s6EAhAHS3nI+iS/brMOHurttd+pHoVrcLL//UfEzsZ5krb+YrPbrLVKP5uq5iJG1Tds05xGixG6V8e2JqorrTU0k0gBuu8/29rHXeQxfdsEOxY8=
[root@client ~]#

Above we see the public key that was generated earlier when we logged into the server server.linux.com which has the IP Address 192.168.226.129. There are 2 protocols which ssh use for encryption. RSA and DSA. Here the protocol that is being used is RSA which is the default protocol. The rest is the public key portion of the public/private key pair.

Note: Protect your Private Keys from the server to any media.

To login as a different user on the server run the command

[root@client ~]# ssh remoteuser@server.linux.com
remoteuser@server.linux.com's password:
[remoteuser@server ~]$ whoami
remoteuser

or

[root@client ~]# ssh -l remoteuser server.linux.com
remoteuser@server.linux.com's password:
Last login: Tue Dec 19 2006 from client.linux.com
[remoteuser@server ~]$ whoami
remoteuser
[remoteuser@server ~]$

Check the Gateway of your network if it is up and running by executing the following command, in order to connect to a remote system.

[remoteuser@server ~]$ netstat -rn
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
192.168.226.0   0.0.0.0         255.255.255.0   U         0 0          0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U         0 0          0 eth0
0.0.0.0         192.168.226.2   0.0.0.0         UG        0 0          0 eth0

-r – shows routing table
-n – shows numerical address

Secure File Transfer

[root@server ssh]# which sftp
/usr/bin/sftp
[root@server ssh]# rpm -qf /usr/bin/sftp
openssh-clients-3.9p1-8.RHEL4.1

Secure Copy

[remoteuser@client ~]$ seq 25 > hello.txt
[remoteuser@client ~]$ scp hello.txt remoteuser@server.linux.com:/home/remoteuser
remoteuser@server.linux.com's password:
hello.txt                                    100%   66     0.1KB/s  
[remoteuser@client ~]$

To check whether the file has been transferred execute

[remoteuser@client ~]$ ssh remoteuser@server.linux.com "ls -l"
remoteuser@server.linux.com's password:
total 8
-rw-rw-r--  1 remoteuser remoteuser 66 Dec 19 hello.txt
[root@server ~]# cd /etc/ssh
[root@server ssh]# ls -l
total 184
-rw-------  1 root root 111892 Nov 30  2004 moduli
-rw-r--r--  1 root root   1192 Nov 30  2004 ssh_config
-rw-------  1 root root   3025 Nov 30  2004 sshd_config
-rw-------  1 root root    668 Nov 29 ssh_host_dsa_key
-rw-r--r--  1 root root    590 Nov 29 ssh_host_dsa_key.pub
-rw-------  1 root root    515 Nov 29 ssh_host_key
-rw-r--r--  1 root root    319 Nov 29 ssh_host_key.pub
-rw-------  1 root root    887 Nov 29 ssh_host_rsa_key
-rw-r--r--  1 root root    210 Nov 29 ssh_host_rsa_key.pub
[root@server ssh]#


Generating Keys Manually

[remoteuser@client ~]$ which ssh-keygen
/usr/bin/ssh-keygen
[remoteuser@client ~]$ ls -l /usr/bin/ssh-keygen
-rwxr-xr-x  1 root root 81264 Nov 30  2004 /usr/bin/ssh-keygen
[remoteuser@client ~]$ ssh-keygen
You must specify a key type (-t).
Usage: ssh-keygen [options]
Options:
  -b bits     Number of bits in the key to create.
  -c          Change comment in private and public key files.
  -e          Convert OpenSSH to IETF SECSH key file.
  -f filename Filename of the key file.
  -g          Use generic DNS resource record format.
  -i          Convert IETF SECSH to OpenSSH key file.
  -l          Show fingerprint of key file.
  -p          Change passphrase of private key file.
  -q          Quiet.
  -y          Read private key file and print public key.
  -t type     Specify type of key to create.
  -B          Show bubblebabble digest of key file.
  -C comment  Provide new comment.
  -N phrase   Provide new passphrase.
  -P phrase   Provide old passphrase.
  -r hostname Print DNS resource record.
  -G file     Generate candidates for DH-GEX moduli
  -T file     Screen candidates for DH-GEX moduli


[root@server remoteuser]# su - remoteuser
[remoteuser@server ~]$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/remoteuser/.ssh/id_rsa):
Created directory '/home/remoteuser/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/remoteuser/.ssh/id_rsa.
Your public key has been saved in /home/remoteuser/.ssh/id_rsa.pub.
The key fingerprint is:
54:65:4b:08:69:a1:7b:3d:f1:be:50:37:b3::ff remoteuser@server.linux.com
[remoteuser@server ~]$ ls -l .ssh/
total 16
-rw-------  1 remoteuser remoteuser 887 Dec 19 id_rsa
-rw-r--r--  1 remoteuser remoteuser 237 Dec 19 id_rsa.pub

To generate DSA Private/Public Keys

[remoteuser@server ~]$ ssh-keygen -t dsa
Generating public/private dsa key pair.
Enter file in which to save the key (/home/remoteuser/.ssh/id_dsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/remoteuser/.ssh/id_dsa.
Your public key has been saved in /home/remoteuser/.ssh/id_dsa.pub.
The key fingerprint is:
69:64:4c:dc:d9:42:02:1c:c7:f3:cf:0d:ac:24:89:74 remoteuser@server.linux.com
[remoteuser@server ~]$ ls -l .ssh/
total 32
-rw-------  1 remoteuser remoteuser 668 Dec 19 id_dsa
-rw-r--r--  1 remoteuser remoteuser 617 Dec 19 id_dsa.pub
-rw-------  1 remoteuser remoteuser 887 Dec 19 id_rsa
-rw-r--r--  1 remoteuser remoteuser 237 Dec 19 id_rsa.pub


Finally copy the file id_rsa.pub to the users systems home directory, ie the client from where the user remoteuser in our example will connect to the server inside /home/username/.ssh directory and rename it to authorized_keys. This way a user will not be prompted for a password from that remote system.

Process Management in Unix Machine

Process Management

The ps command enumerates the processes running on your system. The ps command is similar to the Task Manager in Windows OS, in that it lists the running processes (programs).

[root@server ~]# ps
  PID TTY          TIME CMD
 2980 pts/1     bash
 2997 pts/1     ps
[root@server ~]# which ps
/bin/ps

As per existing standards required binaries are to exist in /bin directory and ps is one such binary.

[root@server ~]# rpm -ql procps-3.2.3-7EL
/bin/ps
/lib/libproc-3.2.3.so
/sbin/sysctl
/usr/bin/free
/usr/bin/pgrep
/usr/bin/pkill
/usr/bin/pmap
/usr/bin/skill
/usr/bin/slabtop
/usr/bin/snice
/usr/bin/tload
/usr/bin/top
/usr/bin/uptime
/usr/bin/vmstat
/usr/bin/w
/usr/bin/watch
/usr/share/doc/procps-3.2.3
(Output filtered)

The ps with the –e option will list all the processing running on the system

[root@server ~]# ps -e
  PID TTY          TIME CMD
    1 ?         init
    2 ?         ksoftirqd/0
    3 ?         events/0
    4 ?         khelper
    5 ?         kacpid
   18 ?         kblockd/0
   28 ?         pdflush
   29 ?         pdflush
   31 ?         aio/0
   19 ?         khubd
   30 ?         kswapd0
  105 ?         kseriod
  176 ?         scsi_eh_0
  188 ?         kmirrord/0
  197 ?         kjournald
 1042 ?         udevd
The command ps –aux or ps aux (BSD Style) will list all process and their ids and will provide detailed information about the processes running on the system.

[root@server ~]# ps -aux
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.3/FAQ
USER       PID %CPU %MEM   VSZ  RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.2  1664  556 ?        S       0:00 init [5]
root         2  0.0  0.0     0    0 ?        SN      0:00 [ksoftirqd/0]
root         3  0.0  0.0     0    0 ?        S<   07:36   0:00 [events/0]
root         4  0.0  0.0     0    0 ?        S<   07:36   0:00 [khelper]
root         5  0.0  0.0     0    0 ?        S<   07:36   0:00 [kacpid]
root        18  0.0  0.0     0    0 ?        S<   07:36   0:00 [kblockd/0]
root        28  0.0  0.0     0    0 ?        S       0:00 [pdflush]
root        29  0.0  0.0     0    0 ?        S       0:00 [pdflush]
root        31  0.0  0.0     0    0 ?        S<   07:36   0:00 [aio/0]
root        19  0.0  0.0     0    0 ?        S       0:00 [khubd]
root        30  0.0  0.0     0    0 ?        S       0:00 [kswapd0]
root       105  0.0  0.0     0    0 ?        S       0:00 [kseriod]
root       176  0.0  0.0     0    0 ?        S       0:00 [scsi_eh_0]
root       188  0.0  0.0     0    0 ?        S<   07:36   0:00 [kmirrord/0]
root       197  0.0  0.0     0    0 ?        S       0:00 [kjournald]
root      1042  0.0  0.1  2452  448 ?        S<s     0:00 udevd
root      3022  0.8  1.1  5484 3040 pts/2    S       0:07 /usr/lib/vmware-tools/bin32/vmware
root      3087  0.0  0.3  2808  772 pts/1    R+      0:00 ps -aux

-a  all w/ tty, including other users  -t by tty
-u  by effective user ID (supports names)
-x  processes w/o controlling ttys

PROCESS STATE CODES

Here are the different values that the s, stat and state output specifiers (header "STAT" or "S") will display to describe the state of a process.
D    Uninterruptible sleep (usually IO)
R    Running or runnable (on run queue)
S    Interruptible sleep (waiting for an event to complete)
T    Stopped, either by a job control signal or because it is being traced.
W    paging (not valid since the 2.6.xx kernel)
X    dead (should never be seen)
Z    Defunct ("zombie") process, terminated but not reaped by its parent.

For BSD formats and when the stat keyword is used, additional characters may be displayed:
<    high-priority (not nice to other users)
N    low-priority (nice to other users)
L    has pages locked into memory (for real-time and custom IO)
s    is a session leader
l    is multi-threaded (using CLONE_THREAD, like NPTL pthreads do)
+    is in the foreground process group

Note: Use ps –aux | less command to display output one page at a time.

[root@server ~]# ps -aux | grep sshd
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.3/FAQ
root      2037  0.0  0.6  5892 1652 ?        Ss      0:00 /usr/sbin/sshd
root      3152  0.0  0.1  2444  352 pts/1    R+      0:00 grep sshd

The above command filters the ps –aux and lists only the sshd process.

[root@server ~]# ps -aux | grep tty
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.3/FAQ
root      2199  0.0  0.1  3112  408 tty1     Ss+     0:00 /sbin/mingetty tty1
root      2201  0.0  0.1  2972  404 tty2     Ss+     0:00 /sbin/mingetty tty2
root      2202  0.0  0.1  2192  408 tty3     Ss+     0:00 /sbin/mingetty tty3
root      2203  0.0  0.1  2476  408 tty4     Ss+     0:00 /sbin/mingetty tty4
root      2452  0.0  0.1  1476  408 tty5     Ss+     0:00 /sbin/mingetty tty5
root      2525  0.0  0.1  2464  408 tty6     Ss+    0:00 /sbin/mingetty tty6
root      3154  0.0  0.2  5028  672 pts/1    S+      0:00 grep tty

The above command filters all processes and lists only tty (Tele Type Terminals) processes.

[root@server ~]# ps -t tty1
  PID TTY          TIME CMD
 2199 tty1     mingetty
[root@server ~]# ps -t tty2
  PID TTY          TIME CMD
 2201 tty2     mingetty

The above command lists only -tty processes, -t option.

The who command also displays information about tty/pts terminals.

[root@server ~]# who
root     :0           Dec 20
root     pts/1        Dec 20 (:0.0)
root     pts/2        Dec 20 (:0.0)
[root@server ~]# who -a
                        Dec 20                216 id=si    term=0 exit=0
           system boot  Dec 20
           run-level 5  Dec 20                    last=S
                        Dec 20               1497 id=l5    term=0 exit=0
                        Dec 20               2199 id=1
                        Dec 20               2201 id=2
LOGIN      tty3         Dec 20               2202 id=3
                        Dec 20               2203 id=4
                        Dec 20               2452 id=5
                        Dec 20               2525 id=6
                        Dec 20               2571 id=x
root     ? :0           Dec 20    ?          2829
root     + pts/1        Dec 20    .          2978 (:0.0)
root     + pts/2        Dec 20         2978 (:0.0)


The pstree utility

[root@server ~]# which pstree
/usr/bin/pstree
[root@server ~]# rpm -qf /usr/bin/pstree
psmisc-21.4-4
[root@server ~]# rpm -ql psmisc-21.4-4
/sbin/fuser
/usr/bin/killall
/usr/bin/pstree
/usr/bin/pstree.x11
/usr/share/locale/de/LC_MESSAGES/psmisc.mo
/usr/share/locale/en/LC_MESSAGES/psmisc.mo
/usr/share/locale/fr/LC_MESSAGES/psmisc.mo
/usr/share/locale/it/LC_MESSAGES/psmisc.mo
/usr/share/locale/pt/LC_MESSAGES/psmisc.mo
/usr/share/locale/sv/LC_MESSAGES/psmisc.mo
/usr/share/man/man1/fuser.1.gz
/usr/share/man/man1/killall.1.gz
/usr/share/man/man1/pstree.1.gz

The pstree command displays the processes in a tree like view.



Process Selections

The free command lists the amount of total memory, used, free, shared, buffers, cached and the swap usage.

[root@server ~]# free
             total       used       free     shared    buffers     cached
Mem:        256044     247204       8840          0      15440     140916
-/+ buffers/cache:      90848     165196
Swap:       524280          0     524280
[root@server ~]#
                              
The free command with the –m option returns the memory and swap in megabytes.

[root@server ~]# free -m
             total       used       free     shared    buffers     cached
Mem:           250        241          8          0         15        137
-/+ buffers/cache:         88        161
Swap:          511          0        511

Use the free command with the –m and –s (seconds) to update the memory usage every 3 seconds.

[root@server ~]# free -m -s 3
             total       used       free     shared    buffers     cached
Mem:           250        241          8          0         15        137
-/+ buffers/cache:         89        160
Swap:          511          0        511

             total       used       free     shared    buffers     cached
Mem:           250        241          8          0         15        137
-/+ buffers/cache:         89        161
Swap:          511          0        511

Use the top command to display information about the processes running on the system. The top command updates the information displayed every 3 seconds.

[root@server ~]# top

top - up  ,  3 users,  load average: 0.05, 0.11, 0.15
Tasks:  76 total,   1 running,  73 sleeping,   2 stopped,   0 zombie
Cpu(s):  6.0% us,  5.6% sy,  0.0% ni, 88.4% id,  0.0% wa,  0.0% hi,  0.0% si
Mem:    256044k total,   247976k used,     8068k free,    15540k buffers
Swap:   524280k total,        0k used,   524280k free,   140992k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 2675 root      15   0 38508  15m 5096 S  6.8  6.0   1:09.98 X
 2978 root      15   0 38348  14m 8680 S  2.9  5.6   0:17.09 gnome-terminal
24855 root      16   0  2612  940  756 R  0.7  0.4   0:00.12 top
 3022 root      15   0  5484 3056 2496 S  0.3  1.2   0:29.69 vmware-toolbox-
    1 root      16   0  1664  556  480 S  0.0  0.2   0:00.86 init
    2 root      34  19     0    0    0 S  0.0  0.0   0:00.12 ksoftirqd/0
    3 root       5 -10     0    0    0 S  0.0  0.0   0:00.22 events/0
    4 root       5 -10     0    0    0 S  0.0  0.0   0:00.02 khelper
    5 root      15 -10     0    0    0 S  0.0  0.0   0:00.00 kacpid
   18 root       5 -10     0    0    0 S  0.0  0.0   0:00.14 kblockd/0
   28 root      20   0     0    0    0 S  0.0  0.0   0:00.00 pdflush
   29 root      15   0     0    0    0 S  0.0  0.0   0:00.44 pdflush
   31 root       8 -10     0    0    0 S  0.0  0.0   0:00.00 aio/0
   19 root      15   0     0    0    0 S  0.0  0.0   0:00.00 khubd
   30 root      15   0     0    0    0 S  0.0  0.0   0:00.12 kswapd0
  105 root      25   0     0    0    0 S  0.0  0.0   0:00.00 kseriod
  176 root      22   0     0    0    0 S  0.0  0.0   0:00.00 scsi_eh_0
  188 root       6 -10     0    0    0 S  0.0  0.0   0:00.00 kmirrord/0
  197 root      16   0     0    0    0 S  0.0  0.0   0:00.87 kjournald

Pressing SHIFT+f will bring up a menu to select more options in the top command.

Note: zombie processes tend to be in deep sleep.

[root@server ~]# top --help
top: procps version 3.2.3
usage:  top -hv | -bcisS -d delay -n iterations [-u user | -U user] -p pid [,pid ...]

The top command with the –u username option will display usage of a particular user.

[root@server ~]# top -u root

top - up  ,  3 users,  load average: 0.07, 0.05, 0.06
Tasks:  77 total,   1 running,  73 sleeping,   3 stopped,   0 zombie
Cpu(s):  2.8% us,  3.8% sy,  0.0% ni, 93.4% id,  0.0% wa,  0.0% hi,  0.0% si
Mem:    256044k total,   248564k used,     7480k free,    15648k buffers
Swap:   524280k total,        0k used,   524280k free,   141020k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 2675 root      15   0 38508  15m 5112 S  3.3  6.0   1:21.08 X
 2978 root      15   0 38216  14m 8680 S  1.6  5.6   0:21.18 gnome-terminal
 3022 root      15   0  5484 3056 2496 S  1.0  1.2   0:38.82 vmware-toolbox-
24886 root      17   0  3612  944  760 R  0.7  0.4   0:00.04 top
 2189 root      16   0  6524 4004 1612 S  0.3  1.6   0:10.54 hald
 2956 root      25  10 32156  18m  10m S  0.3  7.5   0:06.61 rhn-applet-gui
 2972 root      15   0 21324 9224 6936 S  0.3  3.6   0:03.83 mixer_applet2
    1 root      16   0  1664  556  480 S  0.0  0.2   0:00.93 init
    2 root      34  19     0    0    0 S  0.0  0.0   0:00.12 ksoftirqd/0
    3 root       5 -10     0    0    0 S  0.0  0.0   0:00.25 events/0
    4 root       5 -10     0    0    0 S  0.0  0.0   0:00.02 khelper
    5 root      15 -10     0    0    0 S  0.0  0.0   0:00.00 kacpid
   18 root       5 -10     0    0    0 S  0.0  0.0   0:00.15 kblockd/0
   28 root      20   0     0    0    0 S  0.0  0.0   0:00.00 pdflush
   29 root      15   0     0    0    0 S  0.0  0.0   0:00.47 pdflush
   31 root       8 -10     0    0    0 S  0.0  0.0   0:00.00 aio/0
   19 root      15   0     0    0    0 S  0.0  0.0   0:00.00 khubd
   30 root      15   0     0    0    0 S  0.0  0.0   0:00.12 kswapd0
  105 root      25   0     0    0    0 S  0.0  0.0   0:00.00 kseriod
  176 root      22   0     0    0    0 S  0.0  0.0   0:00.00 scsi_eh_0
  188 root       6 -10     0    0    0 S  0.0  0.0   0:00.00 kmirrord/0
  197 root      15   0     0    0    0 S  0.0  0.0   0:00.92 kjournald

Changing Priority (renice)

  1. Open a new terminal and run the top command.
  2. Open a new terminal and run the sequence command seq 1000000 > seqmilrecord.txt
  3. Go back to terminal one where you ran top and sort the process in order of high cpu usage.
  4. Press n to change priority of a process.
  5. Enter the PID of the process in our case it was seq.
  6. Enter new renice value to increase or decrease priority of the seq command. Where -19 is the highest value where process gets a highest priority and 20 is the lowest value, meaning the lowest priority.

Killing a Process

Press k in the top menu to kill a process and type in the PID of the process you want to kill (shut).

The kill command.

The kill command is used to kill a process by sending signal to the process using the Process ID(PID).

kill –l

Will list all signals supported by the kill command.

kill processid

Will kill the process whose id was given to kill.

Killall command

The killall command kills all the processes of the given process name.

#killall seq

Will kill all seq processes running in other shells (in background &).

Note: The information listed by ps, top etc is pulled from a filesystem /proc which is mounted in memory. Below is a example of cpuinfo kept in the /proc filesystem.

[root@server ~]# cat /proc/cpuinfo
processor       : 0
vendor_id       : GenuineIntel
cpu family      : 15
model           : 3
model name      : Intel(R) Pentium(R) 4 CPU 2.40GHz
stepping        : 8
cpu MHz         : 2389.200
cache size      : 1024 KB
fdiv_bug        : no
hlt_bug         : no
f00f_bug        : no
coma_bug        : no
fpu             : yes
fpu_exception   : yes
cpuid level     : 5
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss pni ds_cpl
bogomips        : 4702.20

The uptime command displays information about the time since the system has been up and running.

[root@server ~]# uptime
  up  ,  3 users,  load average: 0.02, 0.08, 0.14


The jobs command is used to display the jobs running in the background. The fg (Foreground) commands is used to display foreground processes running in the shell. Both are inbuilt inside the shell and are not separate binary files.

[root@server ~]#pstree