Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Unix process number on a mac...

by zakzebrowski (Curate)
on Mar 15, 2005 at 04:59 UTC ( #439545=perlquestion: print w/ replies, xml ) Need Help??
zakzebrowski has asked for the wisdom of the Perl Monks concerning the following question:

Howdy all.
I'm using mac osx. I have a process which can go up and down as the day progresses. I want to be able to have a perl program (via a cronjob) restart the process when the process has terminated. I store the process pid ($$) to a database, which the restore process can access. Proc::Processtable doesn't support MacOS. I then tried using Mac::Processes, however this process number is not the same as the process pid. However, the Processes::Processes.xs xs code seems to support translation from the process number to the process id, via the GetProcessPid(PSN) function call, but how do I acess this method in the xs code?
Thanks.


----
Zak - the office

Comment on Unix process number on a mac...
Re: Unix process number on a mac...
by brian_d_foy (Abbot) on Mar 15, 2005 at 05:32 UTC

    What are you trying to do? If you just want to check if the process is still running, use kill() with the 0 signal. It tells you if the process is still running. If it isn't running, just restart it.

    kill 0, $pid;
    --
    brian d foy <bdfoy@cpan.org>
      brian_d_foy++ :)
      This works on the mac. I didn't know about the 0 kill signal. (I knew 9 to 'really' kill a process but I will google the rest...) Thanks!


      ----
      Zak - the office
Re: Unix process number on a mac...
by jhourcle (Prior) on Mar 15, 2005 at 14:28 UTC

    You may want to take a look at Watchdog::Process. I've never used it before, but 'watchdog' is normally what you call a process that monitors another one, and a quick search turned that up.

    Update: Well, if that one's out, I can offer the following logic, that I wrote ~5 years ago to sense if the process I was called was already running. it was written for Solaris, but you can change the arguments to ps to fit for BSD.]

    sub check_running { my $return = `ps -ef | grep process_incoming | grep -v grep | wc - +l | tr -d '[:space:]'`; # blah...running from cron, there's two processes -- sh, and perl: if ( $return != 2 and $return != 1 ) { # wrap this in an eval to keep it quiet # (or the 'die' will generate output) eval { &log_error("Aborting -- 'process_incoming' showing in ps +[$return]"); }; exit; } return; }

    Okay, I admit...it was from a shell script originally, and I just recycled the same command, rather than spending the time to figure out how to track processes from within Perl.

      Looks like Watchdog::Process uses Proc::ProcessTable, so if the latter doesn't work on a Mac, I wouldn't guess that the former would either.

      Also the pod for W::P states "This class has only been successfully tested on Solaris 2.6.", so I wouldn't hold out much hope.

      "There is a thin line between ignorance and arrogance, and only I have managed to erase that line." - Dr. Science
Re: Unix process number on a mac...
by Anonymous Monk on Mar 15, 2005 at 15:36 UTC
    I don't know more about OSX than that it's a Unix system. Given that's a Unix system, the following ought to work:
    die "Not for (l)users!\n" if $>; open my $fh, ">>", "/etc/inittab" or die "open: $!"; print $fh "xx:2345:respawn:/path/to/your/program arguments\n"; close $fh or die "close: $!";
    No cron job needed.
      Mac OS X is BSD Unix, not SysV. There is no /etc/inittab.
Re: Unix process number on a mac...
by Tuppence (Pilgrim) on Mar 15, 2005 at 22:27 UTC
    you can check the output of `ps x` to find out if the process is still running. PID is the first column of the output, extracting this in a programmatic form is 'easy' in perl and so left to the poster.

      You don't need to extract the PID because you already have it. That's how you got the output, after all. If you wanted to do it this way (and it's not the way you should do it), you want to look if you get output besides the header line.

      --
      brian d foy <bdfoy@cpan.org>

        Yes, your kill 0, $pid is indeed a much better solution.

        The only reason to extract the PID with my solution is to check it against the PID that you have stored, which as you say isn't that great when we can just ask the system directly.

        It still would have worked tho :)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://439545]
Approved by kvale
Front-paged by monkfan
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (9)
As of 2014-09-22 16:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (198 votes), past polls