简体   繁体   English

通过 cron 作业运行 ts-node

[英]Run ts-node via cron job

I use a script written in TypeScript in order to redetermine item statuses in my app.我使用用 TypeScript 编写的脚本来重新确定我的应用程序中的项目状态。 I usually run this line:我通常运行这一行:

npx ts-node statuscheck.ts

I wanted this to run everyday at midnight, and for that I tried to schedule a cron job as below:我希望它每天午夜运行,为此我尝试安排一个 cron 作业,如下所示:

0 0 * * * npx ts-node statuscheck.ts

It doesn't really work, I believe because $PATH is different in cron.我相信它并没有真正起作用,因为 $PATH 在 cron 中是不同的。 Does anyone know the correct way of scheduling this script preferrably with ts-node as in the orinal npx line?有谁知道在 orinal npx 行中最好使用 ts-node 调度此脚本的正确方法?

You need to specify the path to the folder where your ts file.您需要指定ts文件所在文件夹的路径。

0 0 * * * /path/to/the/folder/node_modules/.bin/ts-node statuscheck.ts

I finally got it figured out.我终于弄明白了。 It seems adding "cd" to the command completely resolved it.似乎在命令中添加“cd”可以完全解决它。 This is how my crontab looked like in the end, with no errors and no need to install anything:这就是我的 crontab 最终的样子,没有错误,也不需要安装任何东西:

PATH=/root/.nvm/versions/node/v12.18.3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

0 0 * * * cd /root/app/server/ && npx ts-node ./src/scripts/statuscheck.ts > /root/app/logs/statuscheck.ts.log 2>&1

I have updated to:我已更新为:

PATH=/root/.nvm/versions/node/v12.18.3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

30 20 * * * npx ts-node /root/app/server/src/scripts/statuscheck.ts > /root/app/logs/statuscheck.ts.log 2>&1
The log now says the following:
 npx: installed 9 in 2.19s Cannot find module 'typescript' Require stack:

/root/.npm/_npx/80229/lib/node_modules/ts-node/dist/index.js
/root/.npm/_npx/80229/lib/node_modules/ts-node/dist/repl.js
/root/.npm/_npx/80229/lib/node_modules/ts-node/dist/bin.js

I kind of didn't get it because Path now equals what I have in terminal... and I'm not asked this in terminal... It Just works with no issues...我有点不明白,因为路径现在等于我在终端中拥有的......而且我没有在终端中被问到这个......它只是没有问题......

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

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