如何启动一个子进程(通过shell)并在子进程返回之前读取其输出?
我正在通过Mac终端使用以下命令启动Google Chrome:/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --remote-debugging-port=9222 --no-first-run --no-default-browser-check --user-data-dir=$(mktemp -d -t 'chrome-remote_data_dir')
这工作得很好,但该过程不会结束/返回,直到我完成与浏览器.但是,一旦启动,它就会将以下内容打印到控制台:“DevTools在ws://www.example.com上侦听127.0.0.1:9222/devtools/browser/d43342be-66bc-41d4-b591-a7ee22d1c528“
我要刮一下。我可以在Deno中成功地启动它(v。1.36.3)使用:
const myCMD = `/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome`;
const p = Deno.run({
cmd: [
myCMD,
"--remote-debugging-port=9222",
"--no-first-run",
"--no-default-browser-check",
"--user-data-dir=$(mktemp -d -t 'chrome-remote_data_dir')",
],
stdout: "piped",
stderr: "piped",
stdin: "piped",
});
await p.stdin.write(new TextEncoder().encode("Coming from stdin\n"));
await p.status();
但是我不能读取我需要的值,因为过程没有完成,因此输出永远不会返回。有没有办法在子进程返回之前读取它写入控制台的内容?
FYI.我使用了以下文章作为参考:https://medium.com/deno-the-complete-reference/run-shell-commands-in-deno-26c3e9b72e03
1条答案
按热度按时间mm5n2pyu1#
您需要从
p.stdout
和p.stderr
读取,两者都是FsFile
的示例,因此您可以使用.readable
访问ReadableStream
或使用FsFile
的.read
方法现在您将看到该过程的输出。