简体   繁体   English

Cron作业无法正确启动Node.js

[英]Cron job doesn't start nodejs correctly

This is a bit of a weird one and I have no clue what to do. 这有点奇怪,我不知道该怎么办。 I have a small node express api which is hosted on my Raspberry Pi which is running raspbian. 我有一个小节点express api,该API托管在运行raspbian的Raspberry Pi上。

The js file is started in a cron job: js文件在cron作业中启动:

@reboot sudo /usr/bin/node /var/www/html/api/server.js &

And the API itself works fine, I can access it and it returns my requests without a problem. 而且API本身运行良好,我可以访问它,并且它可以毫无问题地返回我的请求。

But the API is also supposed to write to a log file when someone makes a request: 但是,当有人提出请求时,API也应该写入日志文件:

fs.appendFile("./ServerLog.log", "[" + dateTime + "]" + " [" + status + "] " + message + "\n", null, () => {
});

This does not work. 这是行不通的。 But when I kill the process which was started through the cronjob and start it manually it creates a log. 但是,当我终止通过cronjob启动的进程并手动启动它时,它将创建一个日志。

I tried launching node with bash, ie: 我尝试用bash启动节点,即:

@reboot sudo bash -c "sudo  /usr/bin/node /var/www/html/api/server.js &"

But that did not change anything. 但这并没有改变任何东西。 I also changed the output to just write "TEST" but that did not work either. 我也将输出更改为只写“ TEST”,但这也不起作用。 I do have a shebang at the beginning of my js file : #!/usr/bin/env node 我的js文件开头确实有一个shebang: #!/usr/bin/env node

These are the lines which are printed to the syslog: 这些是打印到系统日志的行:

Nov 18 20:20:05 raspberrypi cron[331]: (CRON) INFO (pidfile fd = 3)
Nov 18 20:20:05 raspberrypi cron[331]: (CRON) INFO (Running @reboot jobs)

And in auth.log: 并在auth.log中:

Nov 18 20:20:05 raspberrypi systemd-logind[309]: New seat seat0.
Nov 18 20:20:05 raspberrypi CRON[343]: pam_unix(cron:session): session opened for user root by (uid=0)
Nov 18 20:20:05 raspberrypi CRON[343]: pam_unix(cron:session): session closed for user root
Nov 18 20:20:05 raspberrypi sudo:     root : TTY=unknown ; PWD=/root ; USER=root ; COMMAND=/usr/bin/node /var/www/html/api/server.js
Nov 18 20:20:05 raspberrypi sudo: pam_unix(sudo:session): session opened for user root by (uid=0)

You get a different environment in cron started jobs than you have in an interactive shell. 在cron启动作业中,您获得的环境与在交互式shell中获得的环境不同。 Typically the PATH is by far not as elaborate as in an interactive shell. 通常,PATH到目前为止没有交互式shell中的复杂。 Easiest to fix this is to explicitly set the full path. 解决此问题最简单的方法是显式设置完整路径。

#!/usr/bin/env node actually uses the environment to find the executable called 'node'. #!/usr/bin/env node实际上使用环境来查找名为“ node”的可执行文件。 Simplest is to replace it with an explicit path to node eg #!/usr/local/bin/node or wherever it is installed on your system. 最简单的方法是用到节点的显式路径替换它,例如#!/usr/local/bin/node或系统上任何安装的路径。 To find where it is installed: use which node in an interactive shell. 要查找它的安装位置:使用交互式外壳程序中的which node

sudo is a command that lets you run a command as another user, it'll not help you as such to set PATH values in cron like you have in an interactive shell. sudo是一个命令,它允许您以其他用户身份运行命令,因此像在交互式shell中那样在cron中设置PATH值将无济于事。

the issue is with the PATH. 问题出在PATH。

for me I was using serveStatic and providing the path to the static folder like ./public . 对我来说,我使用serveStatic和提供的路径,像静态文件夹./public This was resolving to the wrong path. 这解决了错误的道路。 By providing complete URL i was able to resolve the issue. 通过提供完整的URL,我能够解决此问题。

You may solve the same issue with other solutions also, the point is that your code is not resolving to the write file because of PATH. 您也可以使用其他解决方案来解决相同的问题,关键是由于PATH,您的代码无法解析到写入文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM