|P is for Practical|
Working With The Process Table (AIX)by Limbic~Region (Chancellor)
|on Aug 05, 2011 at 13:31 UTC||Need Help??|
Limbic~Region has asked for the
wisdom of the Perl Monks concerning the following question:
I am currently working on building a library of tools so that others at the office can more easily write robust scripts. One of the problems I am working on is "determining if a script is running" which is often poorly implemented as:
As you know, this will generate false results in a variety of situations. For instance, if $script is a substring of another process or someone happens to be editing the script. The real problem boils down to two variations: Is this specific script running /path/to/script and Is this specific script running /path/to/script "with these arguments".
At first I reached for Proc::ProcessTable but discovered it will not compile in my environment (AIX 6.1). I then found this example from IBM of "roll-your-own ps". While I looked forward to writing some Inline::C to convert to XS, I discovered the C struct you get only provides access to the executable file name and not the command line arguments. Next I started looking at /proc (AIX is trying to ease the transition from linux :-). There is a psinfo file that contains the argument list (thanks to bart, ambrus and others in the CB who pointed out the very cool Convert::Binary::C). Unfortunately, it is truncated.
At this point, I am beginning to think I need to just parse ps. Unfortunately, that is leading to another problem. While ps can get me access to the full argument list (at least in all the situations I care about), it seems to be impossible to tell if /path/to/script is running with certainty. Here is an example:
As you can see, I won't know if the some_script.pl that is running is /tmp/myscripts/some_script.pl or /some/other/dir/myscripts/some_script.pl. The /proc filesystem does provide a cwd of the process which combined with a rel2abs() could provide the information desired but since it changes if the script changes - it too is unreliable.
There is no lsof on the system. I need this to be usable by non-privileged users (though I could setgrpid if necessary). I can't really see a way to improve upon the crappy solution people have been getting by with just fine up till now. Am I missing something obvious?
Cheers - L~R