![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Help Required: Command to find IP address and command executed of a user | loggedout | Security | 2 | 08-06-2008 09:12 PM |
| how to? launch command with string of command line options | TinCanFury | Shell Programming and Scripting | 5 | 04-28-2008 07:06 PM |
| inconsistent ls command display at the command prompt & running as a cron job | rajranibl | SuSE | 5 | 07-30-2007 09:26 AM |
| How to use more than one MPE command STREAM with Unix command in a single shell? | bosskr | HP-UX | 1 | 10-16-2006 05:16 PM |
| How to use more than one MPE command STREAM with Unix command in a single shell? | bosskr | Shell Programming and Scripting | 0 | 09-19-2006 10:44 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
Help with ps command - Thanks
I am trying to put some ps commands into a long running script to see where the bottlenecks are.
Typically I am running a single ps command directing the output to an environment variable and then printing them to a file for later analysis. I am having trouble retrieving the name of the shell script in which these commands are running (I want to put this into a number of files with output tab delimited so I can sort and analyse later). Explicitly here are my 3 questions: 1. Why do I get ksh instead of Test1.sh ??? 2. I do not seem to be getting any time under CPU (or is it so little it rounds ot 0 - how do I change the display) 3. Instead of the whole Path I really only want the current directory name DEV - how can I do that? Thanks a lot in advance - I will not be offended if you point me to where I can figure out the answers (I have read man pages for ps) but I am not sure what to do next??? SCRIPT: Environment=`pwd ..` StartingTS=`date +"%D%t%T"` Command4sh=`ps -p $$ -o comm=` CurrentTS=`date +"%D%t%T"` etime=`ps -p $$ -o etime=` cputime=`ps -p $$ -o cputime=` print "$Environment\t$Command4sh\t$StartingTS\t$PPID\t$CurrentTS\t$etime\t$cputime\tStart Nightly" >>TimeLog.txt print "$Environment\t$Command4sh\t$StartingTS\t$PPID\t$CurrentTS\t$etime\t$cputime\tStart Nightly" some other .sh scripts are run here I took them out for simplicity CurrentTS=`date +"%D%t%T"` etime=`ps -p $$ -o etime=` cputime=`ps -p $$ -o cputime=` print "$Environment\t$Command4sh\t$StartingTS\t$PPID\t$CurrentTS\t$etime\t$cputime\tMiddle Nightly" >>TimeLog.txt print "$Environment\t$Command4sh\t$StartingTS\t$PPID\t$CurrentTS\t$etime\t$cputime\tMiddle Nightly" some more other .sh scripts are run here I took them out for simplicity CurrentTS=`date +"%D%t%T"` etime=`ps -p $$ -o etime=` cputime=`ps -p $$ -o cputime=` print "$Environment\t$Command4sh\t$StartingTS\t$PPID\t$CurrentTS\t$etime\t$cputime\tEnd Nightly" >>TimeLog.txt print "$Environment\t$Command4sh\t$StartingTS\t$PPID\t$CurrentTS\t$etime\t$cputime\tEnd Nightly" OUTPUT: user@crndas051:/opt/tools/AllScripts/DEV => test1.sh /opt/tools/AllScripts/DEV ksh 07/11/07 20:00:08 884900 07/11/07 20:00:08 00:00 00:00:00 Start Nightly /opt/tools/AllScripts/DEV ksh 07/11/07 20:00:08 884900 07/11/07 20:00:16 00:08 00:00:00 Middle Nightly /opt/tools/AllScripts/DEV ksh 07/11/07 20:00:08 884900 07/11/07 20:00:24 00:16 00:00:00 End Nightly user@crndas051:/opt/tools/AllScripts/DEV => Here is my script: |
|
||||
|
Quote:
#!/usr/bin/ksh Environment=... That means the process that you're checking IS the shell. Now the shell happens to have a child process, in this case Test1.sh. If you were to use the args option in ps instead of comm, you would see that Test1.sh is the first argument to the ksh command. Quote:
Now that I think about it a bit more, it's occurring to me that this might be the same problem as question #1. The cpu time for the shell process isn't going to be that much compared to the cpu time of the script. Quote:
Hope this helps. |
|
||||
|
There are many variants on the "ps" command. Please state your Operating System and version. We can safely assume that you prefer ksh.
Question 1). To find out the name of the script you are running. SCRIPTNAME="`basename $0`" # Please do substitute POSIX compliant versions of the above command as you see fit. "ps -p $$" will return details of the current shell process, not what it is doing. Question 2). You appear to be monitoring the calling shell not what it is running. More about $$. $$ is the Process ID of the current shell. Statistics from "ps" will not include any programs run by that shell. Reading between the lines the process hierarchy (in some versions of "ps" it's "ps -H") might be what you are looking for. Perhaps one or more of the processes called by you script are heavy CPU users? Question 3). basename "/opt/tools/AllScripts/DEV" DEV Last edited by methyl; 03-29-2009 at 08:53 PM.. Reason: Correct ambiguity |
|
||||
|
Quote:
First, and foremost, this only works if the Unix flavor you're working on uses a psinfo file. It's not going to help you if you're wanting to do this on your Linux system. The other catch here is that every flavor of Unix that does have a psinfo file will have it's own structure for the file. I can tell you what works for AIX 5.3, but odds are that you'll have to figure out what it is on your system. Secondly, this is not a practical and efficient solution if you are on a system with thousands of processes and you want to sort them all, since it has to build the list of process id's and then read the psinfo file for each process. It's basically writing a script to do what ps does (but to do it differently). If you are only looking for a subset of the active processes it's feasible. With those caveats, here's a rough draft of a script for an AIX system that will just list the processes and start times. There's a lot more that you could do with it obviously. Code:
#!/usr/bin/perl
my ( $Pid, $Proc, $Psinfo, @Struct, $TimeNow, $PIndex, %Procs );
foreach $Pid ( qx(ps -e -o "pid") ) {
chomp $Pid;
$Pid =~ s/^\s*//;
$Pid =~ s/\s*$//;
$PIndex = "p".$Pid;
next if $Pid =~ /PID/;
open (PSINFO, "</proc/$Pid/psinfo") or warn "Could not open psinfo for $Pid!\n";
$TimeNow = time;
read (PSINFO, $Psinfo, 448);
@Struct = unpack ("L42a16a80L16L7C4ILA8I2L16", $Psinfo);
close PSINFO;
$Procs{$PIndex}{"PID"} = $Pid;
$Procs{$PIndex}{"STime"} = $Struct[29];
$Procs{$PIndex}{"ETime"} = $TimeNow - $Procs{$PIndex}{"STime"};
$Procs{$PIndex}{"PName"} = $Struct[43];
$Procs{$PIndex}{"PName"} =~ s/\x00*$//;
}
print STDOUT "PID\tSTART \tELAPSED\tCMD\n";
foreach $Proc ( sort { $Procs{$b}{"STime"} <=> $Procs{$a}{"STime"} } keys %Procs ) {
printf STDOUT "%d\t%s\t%s\t%s\n", $Procs{$Proc}{"PID"}, $Procs{$Proc}{"STime"}, $Procs{$Proc}{"ETime"}, $Procs{$Proc}{"PName"};
}
exit 0;
It's not the most straightforward or efficient solution, but it's worked for what I'm doing. Your mileage may vary. Good luck! |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|