The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-11-2007
dpressm dpressm is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 1
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:
  #2 (permalink)  
Old 07-11-2007
Nick31 Nick31 is offline
Registered User
  
 

Join Date: May 2007
Posts: 8
Quote:
1. Why do I get ksh instead of Test1.sh ???
I'm guessing that the first line of Test1.sh is using the #! to specify the shell? Something like:
#!/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:
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)
I'm not sure about the cputime. I do know it can depend on what flavor of UNIX. In AIX one of the cpu measurements (pcpu) is the percentage of time the process has been using the cpu over the life of the process, not the percentage of the available cpu capacity like one might expect.

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:
3. Instead of the whole Path I really only want the current directory name DEV - how can I do that?
Are you referring to what pwd returns? (What you're putting in the $Environment variable?) That's a quick fix. Try calling your variable thusly: ${Environment##*/} . That will (since you do seem to be using korn shell) remove all the characters from the left of the variable's value up to the last forward slash.

Hope this helps.
  #3 (permalink)  
Old 03-29-2009
pgolvalkar pgolvalkar is offline
Registered User
  
 

Join Date: Mar 2009
Posts: 1
How to sort ps command

I want to sort ps command as per process start time...may be older process at the end of list...could some one help me to do that
?
  #4 (permalink)  
Old 03-29-2009
methyl methyl is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 1,184
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
  #5 (permalink)  
Old 03-30-2009
Nick31 Nick31 is offline
Registered User
  
 

Join Date: May 2007
Posts: 8
Quote:
Originally Posted by pgolvalkar View Post
I want to sort ps command as per process start time...may be older process at the end of list...could some one help me to do that?
That's a tricky one since ps formats the start time differently depending on how long it's been running, and the formats don't lend themselves to sorting. What I did is write a perl script to read the /proc/pid/psinfo files directly so I could get the start time displayed as the Epoch time (the number of seconds since 1/1/1970 00:00:00, give or take a few leap seconds). That you can sort on very easily.

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!
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:43 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0