Find the answer to your Linux question:
Results 1 to 9 of 9
Code: #!/bin/bash echo "Enter the process id" read pid ps aux | awk '/$pid/ && !/awk/ {print "%CPU usage is:", $3}' ps aux | awk '/$pid/ && !/awk/ {print "%Mem ...
  1. #1
    Just Joined!
    Join Date
    Jul 2007
    Posts
    12

    why is cpu usage code not working?

    Code:
    #!/bin/bash
    
    echo "Enter the process id"
    read pid
    ps aux | awk '/$pid/ && !/awk/ {print "%CPU usage is:", $3}'
    ps aux | awk '/$pid/ && !/awk/ {print "%Mem usage is:", $4}'
    i'm a linux newbie and also new to shell scripting.
    the thing i'm working on:find cpu usage and mem usage when pid of a process is given
    why is this not working?
    when i give the pid, it returns to command prompt.

    could yu pelase tell me whats wrong with this thing?
    and also correct it if possible

    if i want to know the cpu usage and mem usage for every 5 secs...
    how do i loop it?

    thanks in advance

  2. #2
    Just Joined!
    Join Date
    Jul 2007
    Posts
    12

    this thing works

    #!/bin/bash

    echo "Enter the process id"
    read pid
    ps aux |grep $pid |awk '{print "%CPU usage is:", $3}'
    ps aux |grep $pid| awk '{print "%Mem usage is:", $4}'

    but it is showing the cpu usage of grep also....
    what should i do to get only for the pid?

  3. #3
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    Do a grep a 2nd time to exclude grep (-v option)

    echo "Enter the process id"
    read pid
    ps aux |grep $pid |grep -v grep| awk '{print "%CPU usage is:", $3}'
    ps aux |grep $pid |grep -v grep| awk '{print "%Mem usage is:", $4}'

  4. #4
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    Or, rather than grepping, you can select by ps's built-in PID selecting:
    Code:
    echo "Enter the process id"
    read pid
    ps up $pid | awk '{print "%CPU usage is:", $3}'
    ps up $pid | awk '{print "%Mem usage is:", $4}'
    In ps, the 'u' means "user-oriented format", and the 'p' means "Only those PIDs that I specify".
    DISTRO=Arch
    Registered Linux User #388732

  5. #5
    Just Joined!
    Join Date
    Jul 2007
    Posts
    12

    thanks

    hey thanks cabhan and vsemaska

    actually this thing also works:same thing as vsemsaka's

    Code:
    ps aux | grep $pid | awk '{print "% CPU usage of", $2 " is: ", $3}' | grep $pid
    ps aux | grep $pid | awk '{print "% MEM usage of", $2 " is: ", $4}' | grep $pid
    i have another doubt:
    i tried to use these in c program and used system() function and included stdlib.h
    then i wrote a simple program:
    from:Process Control: <stdlib.h>,<unistd.h>

    Code:
    printf("the files are");
    system("ls -l");
    when i tried executing it ..it says 'permission denied'

    so i gave this command: chmod a+x check.c

    now it is showing syntax errors(though there arent any)

    ./check.c: line 7: syntax error near unexpected token `"the files are"'
    ./check.c: line 7: `printf("the files are");'




    why is that so??what can i do to execute it??

  6. #6
    Linux Guru Juan Pablo's Avatar
    Join Date
    Mar 2006
    Location
    /home/south_america/ecuador/quito
    Posts
    2,064
    You need to compile it first,

    gcc -o check check.c

    Then run ./check
    Put your hand in an oven for a minute and it will be like an hour, sit beside a beautiful woman for an hour and it will be like a minute, that is relativity. --Albert Einstein
    Linux User #425940

    Don't PM me with questions, instead post in the forums

  7. #7
    Just Joined!
    Join Date
    Jul 2007
    Posts
    12

    ok..but another doubt

    ok thanks juan pablo ...
    >its running but then it is showing the files first and then it is printing
    "the files are:"

    i mean it is executing system() thing first and then printf...??!!

    >how can i include the cpu usage code mentioned before in a c program..
    system() wont work...

    it says "segmentation fault"

  8. #8
    Just Joined!
    Join Date
    Jul 2007
    Posts
    12

    Unhappy

    ok i cant use windows APIs.i actually got that with windows APIs.
    but...
    i've to wap in c in unix/linux. and all i could find regarding pids is
    getpid() which returns only the pid of the process calling it.

    so how can i get the cpu usage of a single process (when pid is given as input) ??

  9. #9
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    In regards to the output question: output is generally buffered, as this results in fewer writes from the program. This means that the program will store up a bit of output and then write it all at once. Generally, once you write a '\n', the output will flush. Alternatively, you can use fflush(FILE *) to flush a particular file. Try that with stdout.

    As for finding CPU usage, check out the /proc directory. This contains a great deal of information on every running process.
    DISTRO=Arch
    Registered Linux User #388732

Posting Permissions

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