mark4444az has asked for the wisdom of the Perl Monks concerning the following question:

I have a test scenario where I will need to be running iperf.exe, is there some way to monitor this exe and restart if it goes down? I think I can start the app with a system call, the monitoring I'm not sure about. The app would be running from a dos cmd prompt in Windows 7. Anyone have any ideas?

Replies are listed 'Best First'.
Re: monitor iperf from perl
by ig (Vicar) on Nov 15, 2013 at 01:10 UTC

    You might be able to use Win32::Process.

    Here is an example of finding a process (Notepad) by executable name then monitoring it and taking action when it terminates:

    use strict; use warnings; use Data::Dumper::Concise; use Win32::Process::List; use Win32::Process; my $p = Win32::Process::List->new(); my %list = $p->GetProcesses(); foreach my $pid (keys %list) { if($list{$pid} eq 'notepad.exe') { my ($obj, $iflags); Win32::Process::Open($obj, $pid, $iflags) or die "Win32::Process::Open failed"; $obj->Wait(INFINITE); print "Done waiting\n"; exit(0); } } print "Notepad process not found\n";

    And here is an example of starting Notepad from Perl, and restarting it when it exits:

    use strict; use warnings; use Data::Dumper::Concise; use Win32::Process; my $obj; while(1) { Win32::Process::Create($obj, "C:\\windows\\system32\\notepad.exe", "notepad", 0, NORMAL_PRIORITY_CLASS, ".") or die "Failed to start notepad"; $obj->Wait(INFINITE); print "Notepad exited - restarting\n"; }

    If you want to do something while the process continues to run, you can do that too:

    use strict; use warnings; use Data::Dumper::Concise; use Win32::Process; my $obj; while(1) { Win32::Process::Create($obj, "C:\\windows\\system32\\notepad.exe", "notepad", 0, NORMAL_PRIORITY_CLASS, ".") or die "Failed to start notepad"; while(1) { $obj->Wait(10000); my $exit_code; $obj->GetExitCode($exit_code); if($exit_code == Win32::Process::STILL_ACTIVE()) { print localtime() . ": Do something while notepad runs\n"; } else { print localtime() . ": Do something when notepad terminate +s\n"; last; } } }
Re: monitor iperf from perl
by MidLifeXis (Monsignor) on Nov 14, 2013 at 18:25 UTC

    Can you start it as a service? It could then be set to restart on crash.

    --MidLifeXis

Re: monitor iperf from perl
by dasgar (Priest) on Nov 14, 2013 at 18:57 UTC

    One approach would be to leverage the tasklist command from the command prompt. Your script can call that every so often to check to see if iperf.exe is running and then restart it if it isn't. Not sure what iperf.exe is, but you also might want to add a check to see if properly completed its work and cleanly exited.

      Just a guess:
      IPERF(1)                         User Manuals                         IPERF(1)
      
      NAME
             iperf - perform network throughput tests
      
      ...
      
      DESCRIPTION
             iperf is a tool for performing network throughput measurements.  It can
             test either TCP or UDP throughput.  To perform an iperf test  the  user
             must establish both a server (to discard traffic) and a client (to gen‐
             erate traffic).
      

      It doesn't seem like something that would crash often...

        Ordinarily it doesn't, but I'm operating it using a processor who's core voltage is intentionally lower than spec, I occasionally get "stack overflow" and it's ko'd at that point.
      I think that may be the correct approach, I am going to try that.