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

I was developing a script to work on a list of machines - basically finding out whether the Anti virus in the machine is working or not and if not try to do some remediation. I observed the script getting stuck on certain machines when it fails to stop the Anti virus client due to some particular reason which I'm not sure. My question is - Is there a way in Perl to skip the problem machine ? say after the script try for sometime and go to the next machine if it fails on the first machine.
foreach $machine (@old) { chomp ($machine); stopsmc(); #Function to Stop the AV client timedelay(); #Function to delay time startsmc(); #Function to Start the AV client } sub stopsmc{ my $stop='psexec' . " " . '\\\\' . $machine . " " .$smc. " " .' - +p ' . " " . "[password]". " " . "-stop"; if(system("$stop")) { print "SMC Stopped,"; } else { print "SMC Not Stopped,"; } }
I'm using a sysinternal psexec utility to stop the AV client service. Here the stopsmc() function may continuously try to stop the service but does not succeed and hence the script is unable to go forward with the next machine. I would like to know whether any logic can help me - to go to the next machine if one machine get stuck for a particular time period. I was thinking something like this if stopsmc did not give a result for say 5 minutes or so go to next machine. Let me know If I did not make myself clear.

Replies are listed 'Best First'.
Re: A way to break if an action is not responding and go to next?
by cdarke (Prior) on Jul 17, 2011 at 10:44 UTC
    In your stopsmc() function you are calling system. On Microsoft Windows only, system has an additional feature. It has an optional first parameter which, if non-zero, runs the job asynchronously in the background - a bit like appending '&' to a command on UNIX. For example:
    my $pid = system(1, 'background.exe');
    In this case system returns the process id of the child. This is documented in perlport - probably not the first place you would look for it!

    You have a function called timedelay() and you don't say why you need that. Maybe that could be utilised as a timeout?
Re: A way to break if an action is not responding and go to next?
by Anonymous Monk on Jul 17, 2011 at 07:35 UTC