ps command : NIX Classes

The ps (process status) command is used to provide information about the currently running processes, including their process identification numbers (PIDs).
A process, also referred to as a task, is an executing (i.e., running) instance of a program. Every process is assigned a unique PID by the system.

The basic syntax of ps is

ps [options]

Options

-a     Displays all processes on a terminal, with the exception of group leaders.
-c     Displays scheduler data.
-d     Displays all processes with the exception of session leaders.
-e     Displays all processes.
-f     Displays a full listing.
-glist     Displays data for the list of group leader IDs.
-j     Displays the process group ID and session ID.
-l     Displays a long listing
-plist     Displays data for the list of process IDs.
-slist     Displays data for the list of session leader IDs.
-tlist     Displays data for the list of terminals.
-ulist     Displays data for the list of usernames.

The Default Output

linux@atul:/home/atul $ ps
PID    TTY  TIME CMD
3567696  pts/2  0:00 ps
4391144  pts/2  0:00 -ksh

The Process list output for user smaadmp7

linux@atul:/home/atul $ ps -u smaadmp7
UID     PID    TTY  TIME CMD
1930  299258      -  0:01 dsapi_server
1930  671930      -  0:01 dsapi_slave
1930  696452      -  0:17 dsapi_slave
1930  790608      -  0:00 dsapi_server
1930 1200174      -  0:01 dsapi_server
1930 1634556      -  0:15 dsapi_slave
1930 1687662      -  0:00 dsapi_server
1930 1757284      -  0:00 dsapi_slave
1930 1790152      -  0:00 dsapi_server
1930 2081022      -  0:00 dsapi_server
1930 2158782      -  0:47 dsapi_slave
1930 2289812      -  0:00 dsapi_server

The -a option tells ps to list the processes of all users on the system rather than just those of the current user

linux@atul:/home/atul $ ps -a
PID    TTY  TIME CMD
1294566  pts/8  0:00 sleep
1884294  pts/5  0:00 ksh
2240748  pts/3  0:19 topas_nmon
3813422         0:00 <defunct>
4661352 pts/12  0:00 tail
4710552  pts/8  0:00 ksh
5263386  pts/3  0:00 ksh
5558486 pts/11  0:01 topas_nmon
6266958  pts/2  0:00 ps
6885508 pts/13  0:00 ksh
7254052  pts/4  0:00 ksh
8024154 pts/11  0:00 ksh

Own output format
If you are bored by the regular output, you could simply change the format. To do so use the formatting characters which are supported by the ps command.
If you execute the ps command with the ‘o’ parameter you can tell the ps command what you want to see:

linux@atul:/home/atul $ ps -o "%u : %U : %p : %a"
RUSER       USER       PID   COMMAND
atul : atul : 2256934 : ps -o %u : %U : %p : %a
atul : atul : 4391144 : -ksh

Log listing
The -l option generates a long listing
The additional columns of most interest are NI and SZ. The former shows the nice value of the process, which determines the priority of the process. The higher the value, the lower the priority. The default nice value is 0 on NIX systems.
The latter displays the size of the process in memory. The value of the field is the number of pages the process is occupying. On Linux systems a page is 4,096 bytes.

linux@atul:/home/atul $ ps -l
F S  UID     PID    PPID   C PRI NI ADDR    SZ    WCHAN    TTY  TIME CMD
200001 A 2291 3305488 4391144   3  61 20 432309400  1788           pts/2  0:00 ps
240001 A 2291 4391144 5161086   0  60 20 3430d7400   792           pts/2  0:00 ksh

The -e option generates a list of information about every process currently running. The -f option generates a listing that contains fewer items of information for each process than the -l option.

linux@atul:/home/atul > ps -ef | more
UID     PID    PPID   C    STIME    TTY  TIME CMD
root       1       0   0   Apr 28      -  6:21 /etc/init
root  102518       1   0   Apr 28      - 23:31 /usr/sbin/syncd 60
root  106530       1   0   Apr 28      -  0:00 /usr/sbin/uprintfd
root  127116       1   0   Apr 28      -  0:10 /usr/ccs/bin/shlap64
root  135190  204998   0   Apr 28      -  4:39 /opt/IBM/ITM/aix526/ux/bin/stat_daemon 12
root  147644  323618   0   Apr 28      -  0:02 /usr/sbin/portmap
root  163990       1   0   Apr 28      -  0:00 /usr/lib/errdemon
root  192710  323618   0   Apr 28      -  0:00 haemd HACMP 1 CCMP0 SECNOSUPPORT
root  204998       1   0   Apr 28      - 2352:55 /opt/IBM/ITM/aix526/ux/bin/kuxagent
root  209066  135190   0   Apr 28      -  0:06 /opt/IBM/ITM/aix526/ux/bin/ifstat 30 7
root  217206  286754   0 08:18:21      -  0:00 sshd: mahendra [priv]
root  229520  323618   0   Apr 28      -  0:00 /usr/sbin/rsct/bin/IBM.ServiceRMd
root  233604  323618   0   Apr 28      -  5:59 /usr/sbin/snmpd -c /etc/snmpd.conf
root  241798  135190   0   Apr 28      -  0:59 /opt/IBM/ITM/aix526/ux/bin/nfs_stat AIX 30 9
root  249928       1   0   Apr 28      -  0:11 /tivoli_ep_B/opt/Tivoli/lcf/bin/aix4-r1/mrt/lcfd
root  253990       1   0   Apr 28      -  3:53 /opt/IBM/ITM/aix526/ul/bin/kulagent
root  258098  135190   0   Apr 28      -  2:00 /opt/IBM/ITM/aix526/ux/bin/kux_vmstat 30 6

 
 

keep scripting…….
©Victimizeit

date command : NIX classes

 

A useful command line tool is date, which is typically used for displaying the current system date, or setting it. The default format of the date and time displayed will be the system default, eg “date : Wed May  9 19:42:23 GMT 2012
“, but it is possible to apply your own formatting, and also to specify a different date to use, without adjusting the system clock.

The date command writes the current date and time to standard output if called with no flags or with a flag list that begins with a + (plus sign). Otherwise, it sets the current date. Only a root user can change the date and time.
If, for example, you wanted to display the current day’s day of the week only.
If you follow the date command with a + (plus sign) and a field descriptor, you can control the output of the command. You must precede each field descriptor with a % (percent sign). The system replaces the field descriptor with the specified value. Enter a literal % as %% (two percent signs). The date command copies any other characters to the output without change. The date command always ends the string with a new-line character.

date +%A

The + switch tells the date command to apply the following format to the current date. %A tells date that the format to use is the locale’s full weekday name. A full list of the formatting modifiers is at the end of this article. It’s Friday today, so entering the above command at the command prompt would display this:

$ date +%A
Wednesday

If you wanted to display the date in the format YYYY-MM-DD, with a 4 digit year and 2 digit months with leading zeros, you would do this:

$ date +%Y-%m-%d
2012-05-09

Specifying different dates

That was pretty easy, but the above examples only show the current system date. What if you wanted to show yesterday’s date? There’s another switch for date which allows you to specify a date other than the current one, the -d switch. The great thing with -d is you can use words to specify previous or future dates, as per the examples below.

Using date in other commands

Within the bash/ksh shell you can embed commands within other commands using backticks. As a very simple example, we’ll use the echo command. The first example is without backticks so will just echo the word “date” the second example uses backticks and does echo the date. You wouldn’t normally do this because date echoes the output anyway.

$ echo date
date

$ echo `date`
Wed May  9 19:42:23 GMT 2012

Date format specifiers

The following are the available date format specifiers: (Some are supported by only specific shells, please check before using these)

%%     a literal %
%a     locale's abbreviated weekday name (e.g., Sun)
%A     locale's full weekday name (e.g., Sunday)
%b     locale's abbreviated month name (e.g., Jan)
%B     locale's full month name (e.g., January)
%c     locale's date and time (e.g., Thu Mar  3 23:05:25 2005)
%C     century; like %Y, except omit last two digits (e.g., 21)
%d     day of month (e.g, 01)
%D     date; same as %m/%d/%y
%e     day of month, space padded; same as %_d
%F     full date; same as %Y-%m-%d
%g     last two digits of year of ISO week number (see %G)
%G     year of ISO week number (see %V); normally useful only with %V
%h     same as %b
%H     hour (00..23)
%I     hour (01..12)
%j     day of year (001..366)
%k     hour ( 0..23)
%l     hour ( 1..12)
%m     month (01..12)
%M     minute (00..59)
%n     a newline
%N     nanoseconds (000000000..999999999)
%p     locale's equivalent of either AM or PM; blank if not known
%P     like %p, but lower case
%r     locale's 12-hour clock time (e.g., 11:11:04 PM)
%R     24-hour hour and minute; same as %H:%M
%s     seconds since 1970-01-01 00:00:00 UTC
%S     second (00..60)
%t     a tab
%T     time; same as %H:%M:%S
%u     day of week (1..7); 1 is Monday
%U     week number of year, with Sunday as first day of week (00..53)
%V     ISO week number, with Monday as first day of week (01..53)
%w     day of week (0..6); 0 is Sunday
%W     week number of year, with Monday as first day of week (00..53)
%x     locale's date representation (e.g., 12/31/99)
%X     locale's time representation (e.g., 23:13:48)
%y     last two digits of year (00..99)
%Y     year
%z     +hhmm numeric timezone (e.g., -0400)
%Z     alphabetic time zone abbreviation (e.g., EDT)

 
 

keep scripting…….
©Victimizeit

du command & tips

Hi folks, Here I am again with some nice nix commands tips/tricks
For this session, I have “du” command.

Syntax
The basic syntax for du is:
du [options] [directories and/or files]

The du command displays the number of blocks used for files. If the File parameter specified is actually a directory, all files within the directory are reported on. If no File parameter is provided, the du command uses the files in the current directory……

$ ls -ls
total 6
2 -rw-r--r--   1 mjb     group        3 Feb 04 23:31 minutes.txt
4 -rw-r--r--   1 mjb     group     1201 Feb 04 23:25 note.txt
52 -rwxrwxrwx   1 adm     mqm      50329 Dec 16 08:25 addGLC  

The first column contains the size of the file in 512-byte blocks, and the sixth column gives the size of the file in bytes.
Earlier Unix systems used an allocation unit of 512 bytes. These 512 bytes came to be known as a block. As disk sizes grew, the basic allocation unit was increased to 1024 bytes on most systems (larger on some), but many utilities, such as ls above, still report file sizes or disk use in 512 byte blocks. So, the 3-byte file uses 2 blocks.

$du -a
8       ./.profile
8       ./.vi_history
8       ./.kshrc
8       ./dir1/ccms
The -a flag reports the number of blocks in individual files.

$du -sThis displays a summary of the directory size. It is the simplest way to know the total size of the current directory.

Shows the size in default block size (512 byte)
$du ./AUTOTEST/library/init.tcl
56      ./AUTOTEST/library/init.tcl

Shows the size in 1KB block size
$du -k ./AUTOTEST/library/init.tcl
28      ./AUTOTEST/library/init.tcl

Shows the size in 1MB block size
$du -m ./AUTOTEST/library/init.tcl
0.03    ./AUTOTEST/library/init.tcl

Shows the size in 1GB block size
$du -g ./AUTOTEST/library/init.tcl
0.00    ./AUTOTEST/library/init.tcl

Tricks/Tips

a) display the disk usage of all files in KB
$du -ak | sort +nr

b) display the disk usage of largest 10 files in KB
du -ak | sort +nr | head

c)display everything sorted by filesize:
$ du -sk .[A-z]* *| sort -n

d)find top 5 directories, enter :
$ cd /home/user1
$ du -sk * | sort -nr | head -5