为什么我可以 执行, cron 没法执行 bash 脚本?

问题:我可以以root身份通过terminal去执行 cd ~/aaa && sh ./bin/a.sh ,且成功。
但是,crontab 却无法执行 cd ~/aaa && sh ./bin/a.sh

a.sh 脚本如下:

git fetch
git checkout stable
git pull | grep Already
if [ "$?" = 0 ]; then
    echo 'no'
else
    pm2 kill
    rm -r ./build
    cnpm install
    ......
fi

crontab 如下:

*/10 * * * * cd ~/aaa && sh bin/a.sh >> /tmp/crontab.log 2>&1

crontab 抓的错误log 如下:

Your branch is behind 'origin/stable' by 1 commit, and can be fast-forwarded.
  (use "git pull" to update your local branch)
bin/a.sh: 7: bin/a.sh: pm2: not found
bin/a.sh: 9: bin/a.sh: cnpm: not found
module.js:471
    throw err;
    ^

Error: Cannot find module

我看了下报错日志,说是pm2 , cnpm 没有找到。但是我可以在 terminal 下直接输入pm2 去执行的啊,难道cron不可以做到吗?求解

阅读 4.3k
2 个回答

cron的环境变量和系统的用是不一样的,具体可以去搜搜资料。

你把脚本里的pm2cnpm改成绝对路径,比如/usr/bin/cnpm 就可以了。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题