Results 1 to 9 of 9
  1. #1
    Join Date
    Mar 2004
    Location
    India
    Posts
    41

    Unanswered: Problem in capturing process when CPU status reaches zero?

    Hi ,

    I have a problem with cpu utilisation.I want to capture all the process when the CPU status reaches zero.
    i used to manually capture this process by redirecting the o/p of the command ps -ef to some file.I want to automate this process.

    Is it possible to check the CPU status and trap the process for the given duration.

    pls help me to solve this problem.


    Thanks and Regds,
    Arun

  2. #2
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525
    I think the output from 'ps' is Unix flavour dependant. Can you post an example of the output that you would see and outline the specifics of the manual process you apply?

    Thanks.

  3. #3
    Join Date
    Mar 2004
    Location
    India
    Posts
    41
    This is the sample of the output which i used to get when i execute the ps -ef command at the unix prompt.Iam capturing this output at regular intervals when the CPU status(TOP cmnd gives the CPU status) shows zero by redirecting the o/p of ps -ef command to a file.

    ps -ef > <filename>

    I want to automate this process and execute the ps -ef command whenever the CPU status reaches zero.The problem is how to get cpu status by executing top command and run the ps -ef
    to capture the process running at that time.

    Pls give me any possible suggestion to solve this problem.


    UID PID PPID C STIME TTY TIME CMD
    root 0 0 0 Jan 30 ? 1:08 sched
    root 1 0 0 Jan 30 ? 578:19 /etc/init -
    root 2 0 0 Jan 30 ? 0:42 pageout
    root 3 0 1 Jan 30 ? 22326:38 fsflush
    root 2066 1 0 Jan 30 ? 0:00 /usr/lib/saf/sac -t 300
    root 643 1 0 Jan 30 ? 0:02 /usr/sbin/rpcbind
    root 702 1 0 Jan 30 ? 0:09 /usr/lib/inet/xntpd -c /etc/inet/ntp.conf
    daemon 681 1 0 Jan 30 ? 0:00 /usr/lib/nfs/statd
    root 13924 13898 0 Mar 12 ? 3:22 /apps/opt/Shareplex/prod/bin/sp_ocap -u2112
    root 1603 1 0 Jan 30 ? 0:15 /opt/VRTSvcs/bin/CmdServer
    root 854 793 0 Jan 30 ? 0:00 /opt/VRTSvmsa/vmsa/server/cmdserver
    root 816 1 0 Jan 30 ? 0:00 /usr/lib/efcode/sparcv9/efdaemon
    -noasyncgc -cp /opt/VRTSv
    root 866 793 0 Jan 30 ? 97:30 /opt/VRTSvmsa/jre/bin/../bin/sparc/native_threads/jre -noasyncgc -cp /opt/VRTSv
    oracle 25810 1 0 Mar 12 ? 73:52 ora_snp4_arpm1v
    oracle 12037 12032 1 01:13:22 ? 0:19 oraclearpm1v



    Thanks and Regds,
    Arun

  4. #4
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    Show us the output of the top command (not available on my system)
    Jean-Pierre.

  5. #5
    Join Date
    Mar 2004
    Location
    India
    Posts
    41
    This is the output of TOP Command.
    Here the CPU states is 56.1%.I have to capture the process when it reaches 0%


    CPU states: 56.1% idle, 20.4% user, 19.7% kernel, 3.9% iowait, 0.0% swap
    Memory: 40G real, 13G free, 1710M swap in use, 6920M swap free

    PID USERNAME THR PRI NICE SIZE RES STATE TIME CPU COMMAND
    4635 oracle 1 0 2 0K 0K cpu386 33.3H 4.10% oracle
    24533 oracle 11 0 0 0K 0K cpu354 25.2H 3.50% oracle
    21930 oracle 11 0 0 0K 0K cpu385 461:47 3.23% oracle
    21891 oracle 11 59 0 0K 0K cpu514 24.8H 1.97% oracle
    21910 oracle 11 59 0 0K 0K sleep 815:15 1.72% oracle
    24523 oracle 11 60 0 0K 0K sleep 40.1H 1.24% oracle
    1119 root 1 158 -20 6480K 3152K run 291.0H 1.10% seosd
    21953 oracle 11 59 0 0K 0K sleep 263:15 1.00% oracle
    12117 root 16 3 0 4464K 3168K cpu514 37.0H 0.89% OracleAgent.cur
    24528 oracle 12 59 0 0K 0K sleep 38.2H 0.62% oracle
    11250 oracle 1 60 2 0K 0K sleep 161:05 0.44% oracle
    27532 arbor 14 1 0 52M 35M sleep 13:58 0.35% gateway_server
    22544 oracle 1 0 2 0K 0K sleep 744:18 0.26% tnslsnr
    8571 arbor 1 59 0 3352K 2776K sleep 29:00 0.25% top
    28817 root 1 0 10 42M 38M sleep 181.7H 0.24% PatrolAgent

  6. #6
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    You can try something like this :
    Code:
    top | awk '
    /^CPU states:/ { 
       sub(/%/,"",$3);
       if ($3 == 0)
          exit 1;
       else
          exit 0}
    ' || ps -ef > output_file
    Jean-Pierre.

  7. #7
    Join Date
    Mar 2004
    Location
    India
    Posts
    41
    Hi Jean,

    Thanks for ur code.

    Could u please explain the code.
    Particularly wat's that sub is doing.


    Thanks and Regds,
    Arun

  8. #8
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    Code:
    top | awk '
    /^CPU states:/ { 
       sub(/%/,"",$3);
       if ($3 == 0)
          exit 1;
       else
          exit 0}
    ' || ps -ef > output_file
    top | awk ... || ps
    The output of the 'top' command is processed by 'awk'.
    If the status of awk is not succes (!= 0) the "ps' command is executed.
    The 'awk' script exit with status 1 when CPU state is zero.

    /^CPU states:/
    Select record from 'top' output stating with 'CPU states:'. In the output example you gived, the selected record is :
    CPU states: 56.1% idle, 20.4% user, 19.7% kernel, 3.9% iowait, 0.0% swap
    The CPU state 56.1% is field 3.

    sub(/%/,"",$3)
    Substitute the '%' character by nothing in field 3. Field 3 bocomes '56.1'
    The same thing can be done by 'substr($3,1,length($3)-1)' since '%' is the last character of the string.

    if ($3 == 0) exit 1;
    If the CPU state is equal to zero, exit from awk with status 1.
    When the status of awk is not succes (!= 0), the 'ps -ef' command will be executed because it is preceded by the || operator

    else exit 0
    The CPU state is non zero, exit awk with status 0.
    The 'ps -ef' command will not be executes
    Jean-Pierre.

  9. #9
    Join Date
    Apr 2004
    Location
    Boston, MA
    Posts
    325
    Or a bit shorter:

    Code:
    top | awk '
    /^CPU states:/ { 
          exit (($3 + 0) ? 0 : 1);
      }
    ' || ps -ef > output_file

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •