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

查看:153
本文介绍了linux - 为什么我可以 执行, 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不可以做到吗?求解

解决方案

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

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

这篇关于linux - 为什么我可以 执行, cron 没法执行 bash 脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆