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

Hi,

Sorry about not being able to explain my problem clearly. I have a script that retrieves word reports from two countries. The script is run in a remote machine. The script gets the word files, opened them, copy contents then merge it into a single word report. Somehow when a single employee is not able to properly close his doc, the script stops and is not able to continue. Thus winword.exe crashes.

I would like to know what would be the best way to check if winword.exe process have been idle, and kills them or skip that single word opened, free memory and help the app get back on track on the script.

i have this method that i used:

sub KillWinWord { print "Closing Application... -> "; my $flag = 0; @temp = `tasklist.exe /FI "STATUS eq running"`; foreach $temp (@temp) { if ($temp =~ WINWORD) { $flag = 1; } } if ($flag == 1) { $t = `taskkill /IM "WINWORD.EXE"`; print "$t"; } else { print "WORD handle is closed..."; } print "\n\n"; }

I placed this after processing the report file from a working directory

foreach $key (sort (keys(%SReport_Hash))) { # Lets get first Name out from the key $key =~ s/\\\\//g; @key_value_chunks = split(/\\/,$key); $SReport_Name = $key_value_chunks[5]; open WordDocHandleCountry1, ">> $SReport_Country1"; open WordDocHandleATLCountry2, ">> $SReport_Country2"; $key = '\\\\' . $key; # Changing $key to real path &Read_Document($key, \*WordDocHandleCountry1, \*WordDocHandleCountry +2, $SReport_Name, 1); &KillWinWord(); }

Thanks for the time reading this.

Note: I kinda updated and ruined my first post,sorry

Replies are listed 'Best First'.
Re: To end process in windows
by dasgar (Priest) on Aug 10, 2010 at 05:19 UTC

    I'm a bit confused on what "problem/issue" you are encountering. The title you chose and the information that you shared makes it sound like your trying to treat the symptoms rather than the disease (to borrow a medical analogy). In other words, it sounds like you want to find a way to kill an orphaned process (the symptom) rather than fixing the script that is orphaning the process (the disease).

    First, you should probably modify your code so that it first checks to see if an instance of Word is running. If there is one running, use it. If not, start one.

    Second, your script should include error trapping to ensure that your code does not leave an orphaned process.

    If my suspicions about what you're encountering are correct, doing these things might eliminate your "need" to "end a process in windows".

    (By the way, if a user is actively using Word on the remote system, ending the Word process at the start of the Perl script could create more problems for you.)

Re: To end process in windows
by sam_bakki (Pilgrim) on Aug 10, 2010 at 10:02 UTC
    use Win32::Process; module, it gives total control over process, you can open and kill when ever you want. Win32::Process::Create($ProcessObj,...) $ProcessObj->Kill(0); Regrads, Bakki
Re: To end process in windows
by GrandFather (Saint) on Aug 10, 2010 at 02:49 UTC

    Show us the sort of code you are currently using to manage the Windows app from your script.

    True laziness is hard work