Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

script that monitors processes???

Status
Not open for further replies.

SDCSA

Programmer
May 8, 2003
22
0
0
US
Hello all!!

Can we write a shell script that monitors an executable and counts how many times it is invoked? The script should output the current number of invocations and also how many times it has been invoked so far.


Thanks!
 
No.

(haha). But seriously, this would be a script intensive thing to do. A script would have to parse a 'ps -ef' (or equiv) very quickly, looking for your executable. It would have to record the results somewhere (so as not to lose your totals if someone stops the script). Then it would have to do it all over again (and again, and again) - busy, busy.....
The risk is that between iterations, one invocation could end and another could start -net change of 0. The chance if this happening is >0.

Personally, I'd look into a 'process accounting' mechanism for your specific Unix.

-cheers
 
There is the option to write a "wrapper" script for your executable which would increment a counter before launching the executable.
e.g....
#!/usr/bin/ksh
[ ! -f cntfile ] && echo 0 >cntfile
echo $(( $(<cntfile) + 1 )) >cntfile
your_executable $*

So the report script is....
#!/usr/bin/ksh
echo your_executable has been invoked $(<cntfile) times so far
echo current number of invocations is : \\c
ps -ef|grep your_executable |grep -v grep |wc -l
 
The answer is much more simple than that guys!

Try this:

Move your executable/shell to another location and create a dummy shell in it's place, then have the dummy make a call to the original exe/shell with whatever logging you need.

I use a similar method by which I have a dummy lp which acts as a filter before finally calling the original lp and this works just fine. Of course you will need to grab all the options and args passed to the dummy (you can do this by assigning optargs=$*) and then call your original with &quot;$ORIGINAL $optargs&quot;.

hope that helps!

ART
 
Damn I opened my mouth again without looking at all the other suggestions properly...

YGOR's solution &quot;wrapper&quot; is the same as mine, except I wouldn't call my script a wrapper because I would use it simply to log and launch the target.

ART
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top