Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Extremely odd behavior with net::ping and fork

by wolfger (Deacon)
on Aug 17, 2004 at 20:22 UTC ( #383786=perlquestion: print w/replies, xml ) Need Help??

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

edit: problem solved. Fork using system() to run an external process was leaving me with double copies (and double again, etc) of my script running. One good suggestion was to use exec() instead of system(). I fixed it on my own in the meantime by placing and "exit" immediately after system() in the child process.
I also pasted the wrong source here... Source from before the fork was added. Finished source code added to the bottom of this message.

My first "professional" perl script... And it just isn't working right.
Proper operation:
Once per minute, ping partner.
If ping succeeds and didn't previously succeed, change .ini file to "oneline", kill running process and restart it.
If ping doesn't succeed, and previously succeeded, change .ini file to "allmachines", kill running process and restart it.

Actual operation: I am seeing the .ini being changed to what it already is (should not be possible). Worse yet, after a period of several minutes the pings started coming more frequently that once per minute. My Win2K laptop quit responding to commands, and I had to yank the battery to reboot. Attached are the code and the log file that shows the misbehavior.
#!perl.exe -w use strict; use Net::Ping; use Win32::PerfLib; use Win32::Process; my $status = 2; while ($status < 3) { &ping; sleep (60); } sub ping { print "ping"; my $time; my $ping = Net::Ping->new("icmp"); open(INPUT, "< ip-address.txt") or die "could not open address fil +e"; my @ip = <INPUT>; close(INPUT); open(OUTPUT, ">> output.txt") or die "could not open output file"; foreach my $host (@ip){ unless ($host =~ "^#") { chop $host; $time = localtime(); if ($host ne "" && $host ne "\n"){ if ($ping->ping($host)) { print OUTPUT "$time /// $host is alive\n"; if ($status != 0) { $status = 0; &subone; } } else { print OUTPUT "$time /// $host is dead\n"; if ($status != 1) { $status = 1; &suball; } } } else {print OUTPUT "$time /// No Ip Entered\n";} } } $ping->close(); close(OUTPUT); } sub killmware { print "killmware"; my $server = ""; my %rtasks; my %counter; Win32::PerfLib::GetCounterNames($server, \%counter); my %r_counter = map { $counter{$_} => $_ } keys %counter; # retrieve the id for process object my $process_obj = $r_counter{Process}; # retrieve the id for the process ID counter my $process_id = $r_counter{'ID Process'}; # create connection to $server my $perflib = new Win32::PerfLib($server); my $proc_ref = {}; # get the performance data for the process object $perflib->GetObjectList($process_obj, $proc_ref); $perflib->Close(); my $instance_ref = $proc_ref->{Objects}->{$process_obj}->{Instance +s}; foreach my $p (sort keys %{$instance_ref}) { my $counter_ref = $instance_ref->{$p}->{Counters}; foreach my $i (keys %{$counter_ref}) { if($counter_ref->{$i}->{CounterNameTitleIndex} == +$process_id) { $rtasks{$counter_ref->{$i}->{Counter}} = $inst +ance_ref->{$p}->{Name}; } } } while ( (my $key, my $value) = each %rtasks ) { if ($value eq "maincnc") {Win32::Process::KillProcess($key, +0)} } } sub startup { system ('maincnc.exe'); print "startup"; } sub suball { print "suball"; open (ALL, '< all_machines.ini') or die "ERROR! all_machines.ini m +issing!"; open (MAIN, '> maincnc.ini') or die "ERROR! Could not write to mai +ncnc.ini"; while (<ALL>){ print MAIN $_ } close MAIN; close ALL; &killmware; &startup; } sub subone { print "subone"; open (ALL, '< oneline.ini') or die "ERROR! oneline.ini missing!"; open (MAIN, '> maincnc.ini') or die "ERROR! Could not write to mai +ncnc.ini"; while (<ALL>){ print MAIN $_ } close MAIN; close ALL; &killmware; &startup; }

Tue Aug 17 15:11:49 2004 /// 53.236.33.28 is alive Switching maincnc.ini to oneline.ini Tue Aug 17 15:12:49 2004 /// 53.236.33.28 is alive Tue Aug 17 15:13:50 2004 /// 53.236.33.28 is alive Tue Aug 17 15:14:50 2004 /// 53.236.33.28 is alive Tue Aug 17 15:15:50 2004 /// 53.236.33.28 is alive Tue Aug 17 15:16:50 2004 /// 53.236.33.28 is alive Tue Aug 17 15:17:50 2004 /// 53.236.49.40 is dead Switching maincnc.ini to allmachines.ini Tue Aug 17 15:18:57 2004 /// 53.236.49.40 is dead Tue Aug 17 15:18:56 2004 /// 53.236.49.40 is dead Switching maincnc.ini to allmachines.ini Tue Aug 17 15:20:02 2004 /// 53.236.49.40 is dead Tue Aug 17 15:20:02 2004 /// 53.236.49.40 is dead Tue Aug 17 15:20:02 2004 /// 53.236.49.40 is dead Tue Aug 17 15:22:57 2004 /// 53.236.49.40 is dead Switching maincnc.ini to allmachines.ini Tue Aug 17 15:24:03 2004 /// 53.236.49.40 is dead Tue Aug 17 15:25:08 2004 /// 53.236.49.40 is dead Tue Aug 17 15:26:13 2004 /// 53.236.49.40 is dead Tue Aug 17 15:27:18 2004 /// 53.236.49.40 is dead Tue Aug 17 15:28:23 2004 /// 53.236.49.40 is dead Tue Aug 17 15:29:28 2004 /// 53.236.49.40 is dead Tue Aug 17 15:30:33 2004 /// 53.236.49.40 is dead Tue Aug 17 15:31:38 2004 /// 53.236.49.40 is dead Tue Aug 17 15:32:43 2004 /// 53.236.49.40 is dead Tue Aug 17 15:33:48 2004 /// 53.236.33.28 is alive Switching maincnc.ini to oneline.ini Tue Aug 17 15:34:49 2004 /// 53.236.33.28 is alive Tue Aug 17 15:34:49 2004 /// 53.236.33.28 is alive Switching maincnc.ini to oneline.ini Tue Aug 17 15:35:49 2004 /// 53.236.33.28 is alive Tue Aug 17 15:35:49 2004 /// 53.236.33.28 is alive Tue Aug 17 15:35:50 2004 /// 53.236.33.28 is alive Tue Aug 17 15:36:49 2004 /// 53.236.33.28 is alive Tue Aug 17 15:36:49 2004 /// 53.236.33.28 is alive Tue Aug 17 15:36:50 2004 /// 53.236.33.28 is alive Tue Aug 17 15:37:49 2004 /// 53.236.33.28 is alive Tue Aug 17 15:37:49 2004 /// 53.236.33.28 is alive Tue Aug 17 15:37:50 2004 /// 53.236.33.28 is alive Tue Aug 17 15:38:49 2004 /// 53.236.49.41 is dead Switching maincnc.ini to allmachines.ini Tue Aug 17 15:38:49 2004 /// 53.236.49.41 is dead Switching maincnc.ini to allmachines.ini Tue Aug 17 15:38:50 2004 /// 53.236.49.41 is dead Switching maincnc.ini to allmachines.ini Tue Aug 17 15:39:55 2004 /// 53.236.49.41 is dead Tue Aug 17 15:39:54 2004 /// 53.236.49.41 is dead Switching maincnc.ini to allmachines.ini Tue Aug 17 15:39:56 2004 /// 53.236.49.41 is dead Tue Aug 17 15:40:05 2004 /// 53.236.49.41 is dead Tue Aug 17 15:40:05 2004 /// 53.236.49.41 is dead Tue Aug 17 15:40:06 2004 /// 53.236.49.41 is dead Tue Aug 17 15:41:00 2004 /// 53.236.49.41 is dead Tue Aug 17 15:41:00 2004 /// 53.236.49.41 is dead Tue Aug 17 15:41:01 2004 /// 53.236.49.41 is dead Tue Aug 17 15:41:01 2004 /// 53.236.49.41 is dead Tue Aug 17 15:41:10 2004 /// 53.236.49.41 is dead Tue Aug 17 15:41:10 2004 /// 53.236.49.41 is dead Tue Aug 17 15:41:11 2004 /// 53.236.49.41 is dead Tue Aug 17 15:42:05 2004 /// 53.236.49.41 is dead Tue Aug 17 15:42:05 2004 /// 53.236.49.41 is dead Tue Aug 17 15:42:06 2004 /// 53.236.49.41 is dead Tue Aug 17 15:42:06 2004 /// 53.236.49.41 is dead Tue Aug 17 15:42:15 2004 /// 53.236.49.41 is dead Tue Aug 17 15:42:15 2004 /// 53.236.49.41 is dead Tue Aug 17 15:42:16 2004 /// 53.236.49.41 is dead Tue Aug 17 15:43:10 2004 /// 53.236.33.30 is alive Switching maincnc.ini to oneline.ini Tue Aug 17 15:43:10 2004 /// 53.236.33.30 is alive Switching maincnc.ini to oneline.ini Tue Aug 17 15:43:12 2004 /// 53.236.33.30 is alive Switching maincnc.ini to oneline.ini Tue Aug 17 15:43:11 2004 /// 53.236.33.30 is alive Switching maincnc.ini to oneline.ini Tue Aug 17 15:43:20 2004 /// 53.236.33.30 is alive Switching maincnc.ini to oneline.ini Tue Aug 17 15:43:20 2004 /// 53.236.33.30 is alive Switching maincnc.ini to oneline.ini Tue Aug 17 15:43:21 2004 /// 53.236.33.30 is alive Switching maincnc.ini to oneline.ini Tue Aug 17 15:44:10 2004 /// 53.236.33.30 is alive Tue Aug 17 15:44:10 2004 /// 53.236.33.30 is alive Switching maincnc.ini to oneline.ini Tue Aug 17 15:44:19 2004 /// 53.236.33.30 is alive Tue Aug 17 15:44:20 2004 /// 53.236.33.30 is alive Tue Aug 17 15:44:24 2004 /// 53.236.33.30 is alive Tue Aug 17 15:44:24 2004 /// 53.236.33.30 is alive Tue Aug 17 15:44:32 2004 /// 53.236.33.30 is alive Tue Aug 17 15:44:38 2004 /// 53.236.33.30 is alive Tue Aug 17 15:44:38 2004 /// 53.236.33.30 is alive Tue Aug 17 15:44:38 2004 /// 53.236.33.30 is alive Tue Aug 17 15:44:38 2004 /// 53.236.33.30 is alive Tue Aug 17 15:44:39 2004 /// 53.236.33.30 is alive Tue Aug 17 15:44:50 2004 /// 53.236.33.30 is alive Tue Aug 17 15:44:52 2004 /// 53.236.33.30 is alive Tue Aug 17 15:45:10 2004 /// 53.236.33.30 is alive Tue Aug 17 15:45:11 2004 /// 53.236.33.30 is alive Tue Aug 17 15:45:11 2004 /// 53.236.33.30 is alive Tue Aug 17 15:45:20 2004 /// 53.236.33.30 is alive Tue Aug 17 15:45:20 2004 /// 53.236.33.30 is alive Tue Aug 17 15:45:24 2004 /// 53.236.33.30 is alive Tue Aug 17 15:45:24 2004 /// 53.236.33.30 is alive Tue Aug 17 15:45:32 2004 /// 53.236.33.30 is alive Tue Aug 17 15:45:38 2004 /// 53.236.33.30 is alive Tue Aug 17 15:45:38 2004 /// 53.236.33.30 is alive Tue Aug 17 15:45:38 2004 /// 53.236.33.30 is alive Tue Aug 17 15:45:38 2004 /// 53.236.33.30 is alive Tue Aug 17 15:45:39 2004 /// 53.236.33.30 is alive Tue Aug 17 15:45:50 2004 /// 53.236.33.30 is alive Tue Aug 17 15:45:52 2004 /// 53.236.33.30 is alive Tue Aug 17 15:46:10 2004 /// 53.236.33.30 is alive Tue Aug 17 15:46:11 2004 /// 53.236.33.30 is alive Tue Aug 17 15:46:11 2004 /// 53.236.33.30 is alive Tue Aug 17 15:46:20 2004 /// 53.236.33.30 is alive Tue Aug 17 15:46:20 2004 /// 53.236.33.30 is alive Tue Aug 17 15:46:24 2004 /// 53.236.33.30 is alive Tue Aug 17 15:46:24 2004 /// 53.236.33.30 is alive Tue Aug 17 15:46:32 2004 /// 53.236.33.30 is alive Tue Aug 17 15:46:38 2004 /// 53.236.33.30 is alive Tue Aug 17 15:46:38 2004 /// 53.236.33.30 is alive Tue Aug 17 15:46:38 2004 /// 53.236.33.30 is alive Tue Aug 17 15:46:38 2004 /// 53.236.33.30 is alive Tue Aug 17 15:46:39 2004 /// 53.236.33.30 is alive Tue Aug 17 15:46:50 2004 /// 53.236.33.30 is alive Tue Aug 17 15:46:52 2004 /// 53.236.33.30 is alive Tue Aug 17 15:47:10 2004 /// 53.236.33.30 is alive Tue Aug 17 15:47:11 2004 /// 53.236.33.30 is alive Tue Aug 17 15:47:11 2004 /// 53.236.33.30 is alive Tue Aug 17 15:47:20 2004 /// 53.236.33.30 is alive Tue Aug 17 15:47:20 2004 /// 53.236.33.30 is alive Tue Aug 17 15:47:24 2004 /// 53.236.33.30 is alive Tue Aug 17 15:47:24 2004 /// 53.236.33.30 is alive Tue Aug 17 15:47:33 2004 /// 53.236.33.30 is alive Tue Aug 17 15:47:38 2004 /// 53.236.33.30 is alive Tue Aug 17 15:47:39 2004 /// 53.236.33.30 is alive Tue Aug 17 15:47:39 2004 /// 53.236.33.30 is alive Tue Aug 17 15:47:39 2004 /// 53.236.33.30 is alive Tue Aug 17 15:47:39 2004 /// 53.236.33.30 is alive Tue Aug 17 15:47:51 2004 /// 53.236.33.30 is alive Tue Aug 17 15:47:52 2004 /// 53.236.33.30 is alive Tue Aug 17 15:48:11 2004 /// 53.236.33.30 is alive Tue Aug 17 15:48:11 2004 /// 53.236.33.30 is alive Tue Aug 17 15:48:11 2004 /// 53.236.33.30 is alive Tue Aug 17 15:48:20 2004 /// 53.236.33.30 is alive Tue Aug 17 15:48:21 2004 /// 53.236.33.30 is alive Tue Aug 17 15:48:24 2004 /// 53.236.49.40 is dead Switching maincnc.ini to allmachines.ini Tue Aug 17 15:48:24 2004 /// 53.236.49.40 is dead Switching maincnc.ini to allmachines.ini Tue Aug 17 15:48:33 2004 /// 53.236.49.40 is dead Switching maincnc.ini to allmachines.ini Tue Aug 17 15:48:39 2004 /// 53.236.49.40 is dead Switching maincnc.ini to allmachines.ini Tue Aug 17 15:48:39 2004 /// 53.236.49.40 is dead Switching maincnc.ini to allmachines.ini Tue Aug 17 15:48:39 2004 /// 53.236.49.40 is dead Switching maincnc.ini to allmachines.ini Tue Aug 17 15:48:39 2004 /// 53.236.49.40 is dead Switching maincnc.ini to allmachines.ini Tue Aug 17 15:48:51 2004 /// 53.236.49.40 is dead Switching maincnc.ini to allmachines.ini Tue Aug 17 15:48:39 2004 /// 53.236.49.40 is dead Switching maincnc.ini to allmachines.ini Tue Aug 17 15:49:31 2004 /// 53.236.49.40 is dead Tue Aug 17 15:49:30 2004 /// 53.236.49.40 is dead Switching maincnc.ini to allmachines.ini Tue Aug 17 15:49:30 2004 /// 53.236.49.40 is dead Switching maincnc.ini to allmachines.ini Tue Aug 17 15:49:30 2004 /// 53.236.49.40 is dead Switching maincnc.ini to allmachines.ini Tue Aug 17 15:49:30 2004 /// 53.236.49.40 is dead Switching maincnc.ini to allmachines.ini Tue Aug 17 15:49:30 2004 /// 53.236.49.40 is dead Switching maincnc.ini to allmachines.ini Tue Aug 17 15:49:37 2004 /// 53.236.49.40 is dead Tue Aug 17 15:49:37 2004 /// 53.236.49.40 is dead Tue Aug 17 15:49:30 2004 /// 53.236.49.40 is dead Switching maincnc.ini to allmachines.ini Tue Aug 17 15:49:43 2004 /// 53.236.49.40 is dead Tue Aug 17 15:49:44 2004 /// 53.236.49.40 is dead Tue Aug 17 15:50:00 2004 /// 53.236.49.40 is dead Tue Aug 17 15:51:02 2004 /// 53.236.49.40 is dead Tue Aug 17 15:51:02 2004 /// 53.236.49.40 is dead Tue Aug 17 15:51:06 2004 /// 53.236.49.40 is dead Tue Aug 17 15:51:06 2004 /// 53.236.49.40 is dead Tue Aug 17 15:51:04 2004 /// 53.236.49.40 is dead Tue Aug 17 15:51:12 2004 /// 53.236.49.40 is dead Tue Aug 17 15:51:06 2004 /// 53.236.49.40 is dead Tue Aug 17 15:51:13 2004 /// 53.236.49.40 is dead Tue Aug 17 15:51:21 2004 /// 53.236.49.40 is dead Tue Aug 17 15:51:16 2004 /// 53.236.49.40 is dead Tue Aug 17 15:51:26 2004 /// 53.236.49.40 is dead Tue Aug 17 15:51:26 2004 /// 53.236.49.40 is dead Tue Aug 17 15:51:27 2004 /// 53.236.49.40 is dead Tue Aug 17 15:51:28 2004 /// 53.236.49.40 is dead Tue Aug 17 15:51:36 2004 /// 53.236.49.40 is dead Tue Aug 17 15:52:46 2004 /// 53.236.49.40 is dead Tue Aug 17 15:52:46 2004 /// 53.236.49.40 is dead Tue Aug 17 15:52:46 2004 /// 53.236.49.40 is dead Tue Aug 17 15:53:00 2004 /// 53.236.49.40 is dead Tue Aug 17 15:53:03 2004 /// 53.236.49.40 is dead Tue Aug 17 15:53:05 2004 /// 53.236.49.40 is dead Tue Aug 17 15:53:05 2004 /// 53.236.49.40 is dead Tue Aug 17 15:53:06 2004 /// 53.236.49.40 is dead Tue Aug 17 15:53:15 2004 /// 53.236.49.40 is dead Tue Aug 17 15:53:14 2004 /// 53.236.49.40 is dead Tue Aug 17 15:53:38 2004 /// 53.236.49.40 is dead Tue Aug 17 15:54:12 2004 /// 53.236.49.40 is dead Tue Aug 17 15:54:12 2004 /// 53.236.49.40 is dead Tue Aug 17 15:54:12 2004 /// 53.236.49.40 is dead Tue Aug 17 15:54:12 2004 /// 53.236.49.40 is dead Tue Aug 17 15:54:12 2004 /// 53.236.49.40 is dead Tue Aug 17 15:54:12 2004 /// 53.236.49.40 is dead Tue Aug 17 15:54:15 2004 /// 53.236.49.40 is dead Tue Aug 17 15:54:11 2004 /// 53.236.49.40 is dead Tue Aug 17 15:54:47 2004 /// 53.236.49.40 is dead Tue Aug 17 15:54:53 2004 /// 53.236.49.40 is dead Tue Aug 17 15:55:00 2004 /// 53.236.49.40 is dead Tue Aug 17 15:55:29 2004 /// 53.236.49.40 is dead Tue Aug 17 15:55:28 2004 /// 53.236.49.40 is dead Tue Aug 17 15:55:29 2004 /// 53.236.49.40 is dead Tue Aug 17 15:55:29 2004 /// 53.236.49.40 is dead Tue Aug 17 15:55:29 2004 /// 53.236.49.40 is dead Tue Aug 17 15:55:47 2004 /// 53.236.49.40 is dead Tue Aug 17 15:55:47 2004 /// 53.236.49.40 is dead Tue Aug 17 15:55:53 2004 /// 53.236.49.40 is dead Tue Aug 17 15:55:53 2004 /// 53.236.49.40 is dead Tue Aug 17 15:55:53 2004 /// 53.236.49.40 is dead Tue Aug 17 15:55:53 2004 /// 53.236.49.40 is dead Tue Aug 17 15:55:53 2004 /// 53.236.49.40 is dead Tue Aug 17 15:55:54 2004 /// 53.236.49.40 is dead Tue Aug 17 15:56:58 2004 /// 53.236.49.40 is dead Tue Aug 17 15:56:59 2004 /// 53.236.49.40 is dead Tue Aug 17 15:57:29 2004 /// 53.236.49.40 is dead Tue Aug 17 15:57:29 2004 /// 53.236.49.40 is dead Tue Aug 17 15:57:29 2004 /// 53.236.49.40 is dead Tue Aug 17 15:57:29 2004 /// 53.236.49.40 is dead Tue Aug 17 15:57:29 2004 /// 53.236.49.40 is dead Tue Aug 17 15:57:23 2004 /// 53.236.49.40 is dead
Edit: finished product
#!perl.exe -w use strict; use Net::Ping; use Win32::PerfLib; use Win32::Process; my $status = 2; my $command; $SIG{CHLD} = 'IGNORE'; while ($status < 3) { &ping; sleep (60); } sub ping { my $time; my $ping = Net::Ping->new("icmp"); open(INPUT, "< ip-address.txt") or die "could not open address fil +e"; my @ip = <INPUT>; close(INPUT); open(OUTPUT, ">> output.txt") or die "could not open output file"; foreach my $host (@ip){ unless ($host =~ "^#") { chop $host; $time = localtime(); if ($host ne "" && $host ne "\n"){ if ($ping->ping($host)) { print OUTPUT "$time /// $host is alive\n"; if ($status != 0) { print OUTPUT "Switching maincnc.ini to oneline +.ini\n"; $status = 0; &subone(); } } else { print OUTPUT "$time /// $host is dead\n"; if ($status != 1) { print OUTPUT "Switching maincnc.ini to allma +chines.ini\n"; $status = 1; &suball(); } } } else {print OUTPUT "$time /// No Ip Entered\n";} } } $ping->close(); close(OUTPUT); } sub killmware { my $server = ""; my %rtasks; my %counter; Win32::PerfLib::GetCounterNames($server, \%counter); my %r_counter = map { $counter{$_} => $_ } keys %counter; # retrieve the id for process object my $process_obj = $r_counter{Process}; # retrieve the id for the process ID counter my $process_id = $r_counter{'ID Process'}; # create connection to $server my $perflib = new Win32::PerfLib($server); my $proc_ref = {}; # get the performance data for the process object $perflib->GetObjectList($process_obj, $proc_ref); $perflib->Close(); my $instance_ref = $proc_ref->{Objects}->{$process_obj}->{Instance +s}; foreach my $p (sort keys %{$instance_ref}) { my $counter_ref = $instance_ref->{$p}->{Counters}; foreach my $i (keys %{$counter_ref}) { if($counter_ref->{$i}->{CounterNameTitleIndex} == +$process_id) { $rtasks{$counter_ref->{$i}->{Counter}} = $inst +ance_ref->{$p}->{Name}; } } } while ( (my $key, my $value) = each %rtasks ) { if ($value eq "maincnc") {Win32::Process::KillProcess($key, +0)} } } sub suball { open (ALL, '< all_machines.ini') or die "ERROR! all_machines.ini m +issing!"; open (MAIN, '> maincnc.ini') or die "ERROR! Could not write to mai +ncnc.ini"; while (<ALL>){ print MAIN $_ } close MAIN; close ALL; &killmware; defined(my $pid = fork) or die "Cannot fork: $!"; unless ($pid) { # Child process is here system ('maincnc.exe'); exit; } # Parent process is here } sub subone { open (ALL, '< oneline.ini') or die "ERROR! oneline.ini missing!"; open (MAIN, '> maincnc.ini') or die "ERROR! Could not write to mai +ncnc.ini"; while (<ALL>){ print MAIN $_ } close MAIN; close ALL; &killmware; defined(my $pid = fork) or die "Cannot fork: $!"; unless ($pid) { # Child process is here system ('maincnc.exe'); exit; } # Parent process is here }

--
Believe nothing, no matter where you read it, or who said it - even if I have said it - unless it agrees with your own reason and your own common sense.
(Buddha)

Replies are listed 'Best First'.
Re: Extremely odd behavior with net::ping and fork
by clscott (Friar) on Aug 17, 2004 at 20:40 UTC

    You're not checking that the application 'maincnc' is actually being killed.

    --
    Clayton
      Excellent observation, but as I continue troubleshooting today, I think the problem is actually the opposite. I'm having script problems because the external process is being killed! I have never used fork before, but it seems that what is happening here is that when the external program dies, that child process stays alive and becomes in effect a new copy of my infinite-loop script. I ran for an hour this morning on a pingable IP, no problems. When I switch to a non-pingable IP (while keeping the script running) I suddenly see two log entries changing the .ini file... I switch back, and there are 4.... So everytime I kill the running .exe, I double my number of running perl scripts? Wow. I "fixed" this by adding a "die" command immediately after the "system" command (to get the child to die) but this has a nasty effect of getting "SPOOLSV.EXE" to suck up 99% of my CPU. So I'm guessing there's a better way to kill my children. I need to find out how.

      --
      Believe nothing, no matter where you read it, or who said it - even if I have said it - unless it agrees with your own reason and your own common sense.
      (Buddha)

        See my reply below. It definitely sounds like you're using system() (which returns) instead of exec() (which doesn't). Keep in mind that fork creates two identical processes, both running your perl script. Unless you want two copies of your script running, one of processes running your script must be overwritten using exec().

        If the application you want to kill is started by your script, you might want to look into using Win32::Process module insted of fork. When you create an object, it returns an object with a ->kill() method.

Re: Extremely odd behavior with net::ping and fork
by ikegami (Pope) on Aug 17, 2004 at 20:46 UTC
    Worse yet, after a period of several minutes the pings started coming more frequently that once per minute.

    If ip-address.txt contains more than one host/address, there's no pause between them because there's no sleep() in the foreach $host (@ip) loop. There's only a pause after the whole list has been processed.

    My Win2K laptop quit responding to commands, and I had to yank the battery to reboot. Attached are the code and the log file that shows the misbehavior.

    You can usually force computers to shut down by holding the power button for 4-6 seconds. It's probably a friendlier option than removing the battery.

    That's all I have time for right now. I hope it helps.

      If ip-address.txt contains more than one host/address, there's no pause between them because there's no sleep() in the foreach $host (@ip) loop. There's only a pause after the whole list has been processed.
      I realize that. The .txt file contains 4 addresses, but 3 of them are commented out, and should not be pinged (on the "unless" command). Also, you can see the IP being pinged is always the same.

      --
      Believe nothing, no matter where you read it, or who said it - even if I have said it - unless it agrees with your own reason and your own common sense.
      (Buddha)
Re: Extremely odd behavior with net::ping and fork
by tedrek (Pilgrim) on Aug 17, 2004 at 23:59 UTC
    It looks like you might be having multiple processes running at once, I'm wondering if your maincnc.exe program is starting your script again when it starts up.
      It looks like you might be having multiple processes running at once, I'm wondering if your maincnc.exe program is starting your script again when it starts up.
      No, the .exe is third-party software, with no knowledge of my script's existence. It does seem like my script is somehow being duplicated, based on the log, but I just don't see how that (or for that matter, the output in my log) is possible.

      --
      Believe nothing, no matter where you read it, or who said it - even if I have said it - unless it agrees with your own reason and your own common sense.
      (Buddha)
Re: Extremely odd behavior with net::ping and fork
by ikegami (Pope) on Aug 18, 2004 at 15:59 UTC

    Where is this fork you keep mentioning? Are you doing if (!fork()) { system(...) } or if (!fork()) { exec(...) }? If you do the former, you'll have two copies of the parent perl script running once the child exits.

    Example use of launching an executable using fork():

    # We are in the only process. printf STDERR ("pid = %d\n", $$) if $DEBUG; $pid = fork(); die("fork failed!\n") unless (defined($pid)); if (!fork()) { # We are in the child process. printf STDERR ("child pid = %d\n", $$) if $DEBUG; exec(...); # We'll never reach here if exec() worked. # Avoiding die(); we don't want eval{} catching this. print STDERR "exec failed!\n"; exit(2); } # We are in the parent. printf STDERR ("parent (pid = %d) successfully spawned child (pid = %d +).\n", $$, $pid) if $DEBUG;

    Alternatively:

    use Win32; use Win32::Process; sub GetLastErrorStr { return Win32::FormatMessage(Win32::GetLastError()); } my $process_obj; Win32::Process::Create( $process_obj, "bla.exe", "arg1 arg2", 0, NORMAL_PRIORITY_CLASS, "." ) || die("Error running bla.exe: " . GetLastErrorStr() . "\n"); printf STDERR ("parent (pid = %d) successfully spawned child (pid = %d +).\n", $$, $process_obj->GetProcessID()) if $DEBUG; ... $process_obj->kill(255);

    By the way, in Windows, I don't think the process has a chance to shut itself down properly when told to die using kill(). If the application has a window, it's better to send WM_QUIT to the window (and kill it if that doesn't work). If it doesn't have a window, it provides some kind of API (usually a DLL call) that will shut it down. This could explain your 100% usage after killing the process.

      Where is this fork you keep mentioning?
      Holy cripes... I posted the wrong source! How did I manage that?
      The call to &startup was replaced in a later version with
      defined(my $pid = fork) or die "Cannot fork: $!"; unless ($pid) { # Child process is here system ('maincnc.exe'); } # Parent process is here
      Problem is fixed now, using exit;after the system(). I must have accidentally opened the older version of the script after rebooting my computer, rather than the new version. Maybe I'd have had beter help if I posted the correct source!
      You guys were great in helping me anyway. Thanks for all the feedback!

      --
      Believe nothing, no matter where you read it, or who said it - even if I have said it - unless it agrees with your own reason and your own common sense.
      (Buddha)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://383786]
Approved by Crackers2
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2021-10-22 06:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (85 votes). Check out past polls.

    Notices?