All,
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:
ps -ef | grep $script | grep -v grep

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:

/tmp/myscripts/some_script.pl # one I want to know about # cd /tmp; perl myscripts/some_script.pl &

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


In reply to Working With The Process Table (AIX) by Limbic~Region

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.