无法从cron执行中获得梦m的结果

问题描述 投票:1回答:1

我一直在使用Nightmare和cron进行一些自动化过程。运行Nightmare之后,它将获取结果的值并将其附加到本地计算机中的文件中(未检测到网络问题)。

var Nightmare = require('nightmare');
var fs = require('fs');
var filePath = `${process.env['HOME']}/Documents/ResultCron.txt`;

Nightmare()
    .goto('http://mywebsite.com/form')
    .type('input[id="email"]', '[email protected]')
    .click('#submit-button')
    .wait('.formSubmitted')
    .evaluate(() => document.querySelector('.formSubmitted').value)
    .end()
    .then(result => {
      console.log(`The form submitted is:\n${result}`)
      fs.appendFileSync(filePath, `\n${result}: ${new Date()}`);
    })
    .catch(error => {
      console.error(error)
      fs.appendFileSync(filePath, `\nERROR: ${error}: ${new Date()}`);
    })

[当我使用node /home/user/Documents/nightmareForm.js运行它时,它工作正常并且正在文件中写入。但是,从cron执行时,它不会向文件添加任何内容。

我已将配置添加到crontab中,如下所示:

crontab -e

# It will be executed everyday at 13:00
0 13 * * * node /home/user/Documents/nightmareForm.js

如果执行grep nightmareForm.js /var/log/syslog -C 10,我可以看到它已执行:Apr 24 13:00:00 PC CRON[1223]: (root) CMD (node /home/user/Documents/nightmareForm.js)

继续调查,该文件具有以下权限(Ubuntu 18.10):-rwxr-xr-x,因此它应该是可执行文件,并且创建的文件具有以下权限:-rw-rw-rw-

更新:@ ponury-kostek提出想法后,我验证了脚本可以写入文件,但从未到达then()或catch()块

Update2:我试图从crontab捕获日志,但是它不显示任何错误:0 13 * * * /usr/local/bin/node /home/user/Documents/nightmareForm.js >> /home/user/Documents/nightmareForm.log 2>&1

任何想法为何从crontab中脚本无法到达then()或catch()块?谢谢

javascript node.js cron fs nightmare
1个回答
0
投票

我有同样的问题。

对我来说,工作是使用xvfb-run启动节点进程。

xvfb-run node myscript.js

实际上我制作了仅包含3行的shellscript(go.sh):

#!/bin/bash
cd /home/me/nodestuff/spkfetch
xvfb-run node fetch_umsaetze.js

并将其添加到crontab:

* * * * *  cd /home/norbert/nodestuff/spkfetch && sh go.sh  >> /home/norbert/nodestuff/spkfetch/log/log.txt  2>&1

我从未尝试将xvfb-run调用直接放入crontab,但是我认为这可能是脚本不允许启动X进程(窗口)或类似问题的问题。也许值得将其直接放入cron命令中。

以下是引导我前往此解决方案的页面:https://www.nickhart.co.uk/2019/08/09/scraping-with-nightmarejs-getting-started/#run_on_server

© www.soinside.com 2019 - 2024. All rights reserved.