gpt4 book ai didi

node.js - 从 crontab 运行全局 Node 模块二进制文件

转载 作者:太空宇宙 更新时间:2023-11-04 00:03:54 28 4
gpt4 key购买 nike

我正在尝试从我的 crontab 中运行 Node 模块 (npm install -g lungo-cli)。

我尝试的是从 crontab 调用 Node 二进制文件:

* * * * * lungo

运气不好。

然后我尝试了:

* * * * * /usr/local/bin/lungo

再次,没有运气。

最后我做了我认为的解决方法,使用 shelljs 直接从 javascript 文件调用 Node 模块。

Javascript:

const shell = require('shelljs')
shell.exec('lungo')

我什至尝试在本地运行它!

const lungo = require('lungo-cli/bin/lungo')
const shell = require('shelljs')

const shell.exec('lungo')

定时任务:

* * * * * /usr/local/bin/node $HOME/scripts/lungo.js

而且是一样的,它不起作用,似乎 crontab 运行在与我的 zsh shell 完全不同的环境中,并且找不到除OSX 特定的,例如 catrmcd 等。

我使用 Mac OSX 10.14.1 (Mojave) 和 zsh 作为 shell。

我怎样才能做到这一点?我希望能够使用 crontab 调用我的 Node cli 程序。

最佳答案

我在 macbook 中使用 Oh-my-zsh,因此我尝试了很多方法来运行 crontab 任务,但最后,我的解决方案是在运行命令之前添加 .zshrc。

*/30 * * * * . $HOME/.zshrc; node  /path/for/my_script.js

此任务每 30 分钟运行一次,并使用 .zshrc 配置文件来执行我的 Node 命令。不要忘记在 $HOME 变量之前使用点。

顺便说一句,您可以运行附加到 zsh 环境的任何命令。

关于node.js - 从 crontab 运行全局 Node 模块二进制文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53243987/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com