Re: WScript.Shell.Exec().StdOut.AtEndOfStream waits for text - solution



For those interested, my solution to the problem was complicated.

1. Execute the app.
2. Execute the monitor.
3. Read output from the app.
4. Read output from the monitor.

The monitor is another script. It has two parameters: the process ID
of the app to monitor, and the timeout in seconds. It uses WMI to
watch the specified process. If the process terminates, the monitor
ends. If the timeout elapses, the monitor terminates the process and
ends.

The next two posts will be the final code:
1. ExecWithTimeout.wsf: the main script.
2. ProcessTimeout.wsf: the monitor.


I'm sure this would have been much simpler in a compiled language or
dotNET. But I don't have that expertise or time to devote to learning
it.

On Jan 11, 10:21 am, don.jscript.use...@xxxxxxxxx wrote:
I want to execute an app, process its output, but terminate the app if
it takes too long. I thought using WScript.Shell.Exec() would be the
ticket. However, I'm finding that AtEndOfStream waits until text
appears in the executed app's stdout, or the app terminates. Read()
works the same, and ReadAll() waits for the app to terminate, though I
expect that behavior.

Is there a workaround or alternative? As it stands, I can either
process the app's output as it comes, or terminate the app if it takes
too long, but not both.

Here's a simple example. output.js pauses before and after outputting
a single character. process.js executes output.js, checks
AtEndOfStream, Read()'s the single character, and checks AtEndOfStream
again. You'll notice the pause before each AtEndOfStream is output --
AtEndOfStream waits for the single character, and then waits for the
app to terminate.

process.js
========================================
var WshShell = new ActiveXObject("WScript.Shell");
var oExec = WshShell.Exec("cscript /nologo output.js");
var ch;
var aeos;
WScript.StdOut.WriteLine("starting...");
aeos = oExec.StdOut.AtEndOfStream;
WScript.StdOut.WriteLine("AtEndOfStream=" + aeos);
ch = oExec.StdOut.Read(1);
WScript.StdOut.WriteLine("Read(1)=" + ch);
aeos = oExec.StdOut.AtEndOfStream;
WScript.StdOut.WriteLine("AtEndOfStream=" + aeos);

output.js
========================================
WScript.Sleep(3000);
WScript.StdOut.Write("a");
WScript.Sleep(3000);

.



Relevant Pages

  • Re: WScript.Shell.Exec().StdOut.AtEndOfStream waits for text - solution - ExecWithTimeout.ws
    ... Execute the monitor. ... Read output from the app. ... Read output from the monitor. ... execute a Windows command, copying stdout and stderr. ...
    (microsoft.public.scripting.jscript)
  • Requested Registry Access is Not Allowed
    ... We have several applications that will be executing on multiple machines. ... will execute the apps. ... There are Permission Sets within the framework configuration tool, ... The app fails on the WriteEntry and the exception that is thrown is ...
    (microsoft.public.dotnet.framework)
  • RE: Requested Registry Access is Not Allowed
    ... > We have several applications that will be executing on multiple machines. ... > will execute the apps. ... > There are Permission Sets within the framework configuration tool, ... > machine that the app will run. ...
    (microsoft.public.dotnet.framework)
  • Re: disable double clicking attachments
    ... Double click a file and the app opens. ... There's no prompt to save it before the default action takes place. ... It does once you flag their attachment directory as deny execute. ...
    (microsoft.public.outlook.general)
  • Re: Auditing SP Execute.
    ... > As long as the users only have execute permissions on the proc i.e. they ... This assumes that the app doesn't use a single ... > Jasper Smith (SQL Server MVP) ... >> particular sp was execute and by whom. ...
    (microsoft.public.sqlserver.security)