r@j has asked for the wisdom of the Perl Monks concerning the following question:

I am new to perl. Need to get the running status of process without using ps -ef command and the same code works on both windows and unix environment. Please advice Thanks to all for their suggestions. I need common solution which works on all environments like Unix, Win32, Win64. The requirement is to exit from the code if the previous run of same process is in running state. Below is the example for your quick reference. E.g.: my $JOB_NAME = "ABC123"; my @pid = ps -ef |grep $JOB_NAME; my $len = scalar(@pid); exit if($len > 2); I have a requirement to write the same logic but without using ps -ef in perl script and which works on Unix & all windows versions. Please advice & provide sample code if possible. Thanks in Advance!
  • Comment on get the running status of process without using ps -ef

Replies are listed 'Best First'.
Re: get the running status of process without using ps -ef
by karlgoethebier (Abbot) on Oct 24, 2017 at 17:03 UTC

    See also Better way to search in the process table? for some inspiration. Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

    perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

      I wish I could remember which posts I wrote three and a half years ago :-)


      holli

      You can lead your users to water, but alas, you cannot drown them.

        It comes with the years. Cannibalizing older posts is a good thing ;-)

        In this case i remembered it so well because the issue was extremely urgent in the company i was with at that time.

        Especially odd: The code written with the help of some fellow monks is still in production because the vendor/community couldn't provide some sane solution. Thanks again to all that helped.

        Best regards, Karl

        «The Crux of the Biscuit is the Apostrophe»

        perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

        No need to remember yourself, Super Search can do this for you - however you seem to have paused then :-)
Re: get the running status of process without using ps -ef
by Mr. Muskrat (Canon) on Oct 24, 2017 at 16:20 UTC
Re: get the running status of process without using ps -ef
by hippo (Archbishop) on Oct 24, 2017 at 16:09 UTC

    From kill:

    If SIGNAL is either the number 0 or the string ZERO (or SIGZERO ), no signal is sent to the process, but kill checks whether it's possible to send a signal to it (that means, to be brief, that the process is owned by the same user, or we are the super-user). This is useful to check that a child process is still alive (even if only as a zombie) and hasn't changed its UID. See perlport for notes on the portability of this construct.

    I wonder if that's what you mean by "running status".

Re: get the running status of process without using ps -ef
by thanos1983 (Parson) on Oct 25, 2017 at 08:37 UTC

    Hello r@j,

    Welcome to the Monastery. It seems the fellow Monks have answer your question already but just to add also another possible uni platform (WindowsOS - LinuxOS) oprtion Proc::Background.

    Sample of code:

    #!/usr/bin/perl use strict; use warnings; use feature 'say'; use Proc::Background; my $command = 'ls'; my $arg1 = '-la'; # Add an option to kill the process with die when the variable is # DETROYed. my $opts = {'die_upon_destroy' => 1}; my $bg = Proc::Background->new($opts, $command, $arg1); my $pid = $bg->pid; say $pid; $bg = undef; __END___ $ perl test.pl 9944

    More documentation Review of Proc::Background and similar question to yours process run in background.

    Hope this helps, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!