【问题标题】:Why isn't WebKit working when I launch in cron but works when I launch in terminal?为什么我在 cron 中启动时 WebKit 不工作,但在终端中启动时工作?
【发布时间】:2020-12-01 05:44:34
【问题描述】:

我正在使用 Ubuntu 18.04 的 VPS 上的 WebKit、Firefox 和 Chromium 上启动 playwright 脚本。如果我像cd /home/me/desktop/myCode && /usr/bin/npm run webkit > /home/me/desktop/errors.log 2>&1 这样在终端中键入它,则在我的 VPS 上使用绝对路径所有三个都会启动并正常运行。这让我觉得我已经从这里正确安装了所有依赖项:https://github.com/microsoft/playwright/blob/master/docs/docker/Dockerfile.bionic

但是,当我使用完全相同的脚本但通过 cron 时,Firefox 和 Chromium 启动并正常运行,但 Webkit 版本启动(它在启动 WebKit 之前执行异步请求并且发生这种情况),但是当 WebKit 尝试启动时失败整个程序。 cron 脚本如下所示:

8 14 * * * cd /home/me/desktop/myCode && /usr/bin/npm run webkit > /home/me/desktop/errors.log 2>&1

错误如下所示:

events.js:292 
      throw er; // Unhandled 'error' event
      ^

Error: spawn ldconfig ENOENT
    at Process.ChildProcess._handle.onexit (internal/child_process.js:267:19)
    at onErrorNT (internal/child_process.js:469:16)
    at processTicksAndRejections (internal/process/task_queues.js:84:21)
Emitted 'error' event on ChildProcess instance at:
    at Process.ChildProcess._handle.onexit (internal/child_process.js:273:12)
    at onErrorNT (internal/child_process.js:469:16)
    at processTicksAndRejections (internal/process/task_queues.js:84:21) {
  errno: 'ENOENT',
  code: 'ENOENT',
  syscall: 'spawn ldconfig',
  path: 'ldconfig',
  spawnargs: [ '-p' ]
}
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! myCode@1.0.0 webkit: `cd lib && node runWebkit.js`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the myCode@1.0.0 webkit script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

这是 Typescript 中的一些代码,以防传入 webkit 的 args 出现问题:

export const getBrowser = async ({
  headless,
  browserName,
}: {
  headless: boolean;
  browserName: BrowserName;
}) => {
  switch (browserName) {
    case BrowserName.firefox:
      return await playwright.firefox.launch({
        headless: headless,
        args: ["--no-sandbox", "--disable-setuid-sandbox"],
      });
    case BrowserName.webkit:
      return await playwright.webkit.launch({
        headless: headless,
      });
    case BrowserName.chromium:
      return await playwright.chromium.launch({
        headless: headless,
        args: ["--no-sandbox", "--disable-setuid-sandbox"],
      });
  }
};

【问题讨论】:

  • 我认为这是 Playwright 1.3 的问题,我们正在调查它。您是否尝试过使用 Playwright 1.2.1(以及随附的浏览器版本)?
  • @arjun27 很高兴知道!我回到 1.2.1,它运行良好。感谢您帮助解决这个问题:)

标签: cron webkit playwright


【解决方案1】:

您当前的PATH 似乎没有/sbin。应该可以通过添加以下内容将 Bash 用作您的 cron 作业的外壳:

SHELL=/bin/bash

在顶部的 crontab 文件中配置所有需要的环境变量。

【讨论】:

  • 感谢您的建议,但不幸的是没有奏效。我遇到了与我的 cron 脚本相同的错误:SHELL=/bin/bash 52 14 * * * cd /home/me/desktop/myCode && /usr/bin/npm run webkit > /home/me/desktop/errors.log 2>&1(在 cron 中,SHELL 脚本和 cron 脚本之间有一行)。
  • 我认为你需要把它放在一个单独的行中,如下所示:unix.stackexchange.com/a/94459
  • 谢谢,Max,我这样做了,但遇到了同样的错误。我在评论中的格式是关闭的,但这两行是独立的,单独的行。
【解决方案2】:

这里的核心问题是 Playwright v1.3.0 依赖于 ldconfigPATH 中可用。

在 cronjob 环境中,PATH 默认为PATH=/usr/bin:/bin,不包括ldconfig,通常为/sbin/ldconfig

简单的解决方法:

  • cronjob 前缀为PATH=/usr/bin:/bin:/sbin
  • 降级到 v1.2.1

我提交了https://github.com/microsoft/playwright/issues/3397 来跟踪这个; 我们将作为v1.3.1 的一部分发布修复程序。

很抱歉给您带来不便!

【讨论】:

  • 感谢上下文,安德烈,非常感谢 :)
猜你喜欢
  • 2015-12-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-19
  • 1970-01-01
  • 2021-03-02
  • 2017-12-17
  • 2017-05-07
  • 2016-02-18
相关资源
最近更新 更多