Help with ps command - Thanks


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with ps command - Thanks
# 1  
Old 07-11-2007
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  
Old 07-11-2007
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  
Old 03-29-2009
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  
Old 03-29-2009
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  
Old 03-30-2009
Quote:
Originally Posted by pgolvalkar
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!
This User Gave Thanks to Nick31 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Another one line command where I'd like to determine if Ubuntu or Red Hat when running command

Hello Forum, I'm making very good progress on my report thanks to the very helpful people on this forum. I've been able to successfully create my report for my Red Hat servers. But I do have a few ubuntu servers in the mix and I'd like to capture some data from them when an ssh connection is... (8 Replies)
Discussion started by: greavette
8 Replies

2. AIX

I'm facing problem with rpm command, when running the command and appears this error:

exec(): 0509-036 Cannot load program /usr/opt/freeware/bin/rpm because of the following errors: 0509-022 Cannot load module /opt/freeware/lib/libintl.a(libintl.so.1). 0509-150 Dependent module /opt/freeware/lib/libiconv.a(shr4.o) could not be loaded. 0509-152 Member... (4 Replies)
Discussion started by: Ohmkar
4 Replies

3. Shell Programming and Scripting

Multiple command execution inside awk command during xml parsing

below is the output xml string from some other command and i will be parsing it using awk cat /tmp/alerts.xml <Alert id="10102" name="APP-DS-ds_ha-140018-componentFailure-S" alertDefinitionId="13982" resourceId="11427" ctime="1359453507621" fixed="false" reason="If Event/Log Level(ANY) and... (2 Replies)
Discussion started by: vivek d r
2 Replies

4. Shell Programming and Scripting

SH script, variable built command fails, but works at command line

I am working with a sh script on a solaris 9 zone (sol 10 host) that grabs information to build the configuration command line. the variables Build64, SSLopt, CONFIGopt, and CC are populated in the script. the script includes CC=`which gcc` CONFIGopt=' --prefix=/ --exec-prefix=/usr... (8 Replies)
Discussion started by: oly_r
8 Replies

5. UNIX for Dummies Questions & Answers

passing command output from one command to the next command in cshell

HI Guys, I hope you are well. I am trying to write a script that gets executed every time i open a shell (cshell). I have two questions about that 1) I need to enter these commands $ echo $DISPLAY $ setenv $DISPLAY output_of_echo_$display_command How can i write a... (2 Replies)
Discussion started by: kaaliakahn
2 Replies

6. UNIX for Advanced & Expert Users

unix command : how to insert text at the cursor location via command line?

Hi, Well my title isn't very clear I think. So to understand my goal: I have a script "test1" #!/bin/bash xvkbd -text blabla with xbindkeys, I bind F5 key in order it runs my test1 script So when I press F5, test1 runs. I'm under Emacs/Vi and I press F5 in order to have "blabla" be... (0 Replies)
Discussion started by: xib.be
0 Replies

7. Shell Programming and Scripting

Need help! command working ok when executed in command line, but fails when run inside a script!

Hi everyone, when executing this command in unix: echo "WM7 Fatal Alerts:", $(cat query1.txt) > a.csvIt works fine, but running this command in a shell script gives an error saying that there's a syntax error. here is content of my script: tdbsrvr$ vi hc.sh "hc.sh" 22 lines, 509... (4 Replies)
Discussion started by: 4dirk1
4 Replies

8. AIX

AIX:Command to get netaddress/subnet address command in IPv4/IP6

AIX:Command to get netaddress/subnet address command in IPv4/IP6 Can anybody help us with a command to retrieve netaddress/subnet address command in IPv4/IP6 on aix machine. net/subnet address is in the format 172.16.212.0(signifies all 255 machines in an IPv4 network) (2 Replies)
Discussion started by: rookie8278
2 Replies

9. Shell Programming and Scripting

assign a command line argument and a unix command to awk variables

Hi , I have a piece of code ...wherein I need to assign the following ... 1) A command line argument to a variable e.g origCount=ARGV 2) A unix command to a variable e.g result=`wc -l testFile.txt` in my awk shell script When I do this : print "origCount" origCount --> I get the... (0 Replies)
Discussion started by: sweta_doshi
0 Replies

10. SuSE

inconsistent ls command display at the command prompt & running as a cron job

Sir, I using the following commands in a file (part of a bigger script): #!/bin/bash cd /opt/oracle/bin ls -lt | tail -1 | awk '{print $6}' >> /tmp/ramb.out If I run this from the command prompt the result is: 2007-05-16 if I run it as a cron job then... (5 Replies)
Discussion started by: rajranibl
5 Replies
Login or Register to Ask a Question