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 ...
- 07-24-2007 #1Just Joined!
- Join Date
- Jul 2007
- Posts
- 12
why is cpu usage code not working?
i'm a linux newbie and also new to shell scripting.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}'
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
- 07-24-2007 #2Just 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?
- 07-24-2007 #3Linux 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}'
- 07-24-2007 #4
Or, rather than grepping, you can select by ps's built-in PID selecting:
In ps, the 'u' means "user-oriented format", and the 'p' means "Only those PIDs that I specify".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}'DISTRO=Arch
Registered Linux User #388732
- 07-25-2007 #5Just Joined!
- Join Date
- Jul 2007
- Posts
- 12
thanks
hey thanks cabhan and vsemaska
actually this thing also works:same thing as vsemsaka's
i have another doubt: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 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>
when i tried executing it ..it says 'permission denied'Code:printf("the files are"); system("ls -l");
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??
- 07-25-2007 #6
You need to compile it first,
gcc -o check check.c
Then run ./checkPut 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
- 07-25-2007 #7Just 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"
- 07-25-2007 #8Just Joined!
- Join Date
- Jul 2007
- Posts
- 12
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) ??
- 07-25-2007 #9
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


Reply With Quote