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.
(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.
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
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 "$ORIGINAL $optargs".
Damn I opened my mouth again without looking at all the other suggestions properly...
YGOR's solution "wrapper" 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.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.