我如何使用 cron 运行 来自 bash 的节点脚本

How can I run a node script from bash using cron

我在 Ubuntu

上有这个 cron 作业 运行ning
02 12 * * * quigley-user /mnt/block/alphabits/start.sh >> /mnt/block/alphabits/start.log

cron 作业运行如期进行。

我在 start.sh 脚本中有这个片段

while read -r ticker apiName ;
   do
      echo "$ticker $apiName"
      sudo /usr/bin/node /mnt/block/alphabits/index.js $ticker $apiName
   done < /mnt/block/alphabits/fmpList.txt

回显行显示了预期的结果,但是 index.js 脚本没有 运行。我找不到错误,但也许我遗漏了什么。如果我 运行 来自命令行的 cron 作业中的命令它会正确执行

我错过了什么?谢谢!

crontab 上下文中可能缺少您的 node.js 相对导入路径。

以及 node.js

的其他可能的隐式环境变量配置

假设您的用户 $HOME 目录是 /home/user1

建议:

将以下行添加到 start.sh

的第二行
  #!/bin/bash
  source /home/user1/.bash_profile

这一行会将您的用户的上下文路径和环境变量添加到您的脚本中。