【问题标题】:How to kill child process/close Chrome?如何杀死子进程/关闭 Chrome?
【发布时间】:2019-06-05 07:34:25
【问题描述】:

让我们从代码开始:

#!/usr/bin/env node
const ChildProc = require('child_process');

const chrome = ChildProc.spawn('/usr/bin/google-chrome',['--incognito',`--app=data:text/html,<p>Hello World</p>`],{detached: true, stdio: 'ignore'});
chrome.unref();

setTimeout(() => {
  console.log('killing');
  chrome.kill();
}, 2500); // pretend we did some stuff, and now we're done with chrome.

我的 Chrome 窗口正在打开,但我无法以编程方式关闭它。还有什么我可以尝试的吗?

【问题讨论】:

  • 您可能必须从控制台捕获实际的进程 id 并将特定进程 id 发送到 kill 命令
  • 查看this question 看看它是否可以帮助您
  • @Sterling Archer:'tree-kill' 不起作用,child.stdin.pause() 不起作用(我已经忽略了 stdio),psTree 不起作用......
  • 也许它是 chrome 中的一个功能来阻止程序关闭它?可能需要查看 chromium 以查看它是否允许 nodejs 向其发送 SIGKILL 信号
  • 没有setTimeout() 是否可以工作?

标签: node.js child-process


【解决方案1】:

我找到了chrome-launcher 并查看了源代码。显然,完成这项工作的诀窍是--user-data-dir=/tmp/lighthouse.6ppUDxi 命令行标志。

例如,这有效:

#!/usr/bin/env node
const ChildProc = require('child_process');

function makeUnixTmpDir() {
  return ChildProc.execSync('mktemp -d -t lighthouse.XXXXXXX').toString().trim();
}

const chrome = ChildProc.spawn('/usr/bin/google-chrome',[
  '--disable-translate',
  '--disable-extensions',
  '--disable-background-networking',
  '--safebrowsing-disable-auto-update',
  '--disable-sync',
  '--metrics-recording-only',
  '--disable-default-apps',
  '--mute-audio',
  '--no-first-run',
  '--user-data-dir='+makeUnixTmpDir(),
  'https://google.com',
],{detached: true, stdio: 'ignore'});
// chrome.unref();

setTimeout(() => {
  console.log('killing');
  chrome.kill();
}, 5000); // pretend we did some stuff, and now we're done with chrome.

这可能与 Chrome 合并进程的方式有关,但我想如果窗口使用它自己的用户配置文件,它就不会这样做。

【讨论】:

  • 这些怎么可能相关?我什至尝试过,但没有任何效果。
  • @AliTou 不知道的人。我把我最好的猜测放在最后一句话。
猜你喜欢
  • 2021-11-21
  • 1970-01-01
  • 2013-05-03
  • 1970-01-01
  • 2021-07-24
  • 1970-01-01
  • 2010-12-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多