linux|centos的crontab计划任务 运行node.js|npm程序失败

nodejs qmt 85℃

linux/centos下 crontab计划运行node.js npm 程序失败

crontab的内容如下:

0 */3 * * * cd /root/github/nodejs-course/project/ && npm run start

查看crontab的日志:

cat /var/spool/mail/root

内容如下:

From [email protected]  Thu Mar  7 09:00:01 2024
Return-Path: <[email protected]>
X-Original-To: root
Delivered-To: [email protected]
Received: by uc.localdomain (Postfix, from userid 0)
        id 0DF4C34C7858; Thu,  7 Mar 2024 09:00:01 +0800 (CST)
From: "(Cron Daemon)" <[email protected]>
To: [email protected]
Subject: Cron <root@uc> cd /root/github/nodejs-course/project/jandan-net && npm run start
Content-Type: text/plain; charset=UTF-8
Auto-Submitted: auto-generated
Precedence: bulk
X-Cron-Env: <XDG_SESSION_ID=10527>
X-Cron-Env: <XDG_RUNTIME_DIR=/run/user/0>
X-Cron-Env: <LANG=en_US.UTF-8>
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/root>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=root>
X-Cron-Env: <USER=root>
Message-Id: <[email protected]>
Date: Thu,  7 Mar 2024 09:00:01 +0800 (CST)

/bin/sh: npm: command not found

看了又是老问题了,crontab进入的环境和当前的系统环境不同。需要把bash profile手动export进去。

修改后成功了:

0 */3 * * * source /root/.zshrc && cd /root/github/nodejs-course/project/ && npm run start

然后查看效果,重新查看日志:

cat /var/spool/mail/root

可以看到正常输出的日志了(node.js 里面的console.log的输出)

转载请注明:QMT|Ptrade量化交易 » linux|centos的crontab计划任务 运行node.js|npm程序失败

喜欢 (0)