V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
thiswind
V2EX  ›  Python

Crontab 无法正确读取 PATH 环境变量的解决办法

  •  
  •   thiswind ·
    thiswind · 2016-09-13 15:00:47 +08:00 · 3230 次点击
    这是一个创建于 2753 天前的主题,其中的信息可能已经有所发展或是发生改变。

    运行这一句:

    curl -L https://git.io/vigfN | bash
    

    参考 braker-scripts/working-scripts/add_current_shell_and_path_to_crontab.sh

    13 条回复    2016-09-14 12:37:35 +08:00
    doyel
        1
    doyel  
       2016-09-13 15:08:51 +08:00
    反正知道这毛病我所有 crontab 都直接写绝对路径。。
    thiswind
        2
    thiswind  
    OP
       2016-09-13 15:18:05 +08:00
    @doyel 绝对路径也木有用,因为它根本就不读 PATH 变量,环境变量全部都读不到。恩,不过一般很少会有需求会碰到这个坑
    doyel
        3
    doyel  
       2016-09-13 15:22:36 +08:00
    那倒是,碰到取环境变量就抓瞎了
    thiswind
        4
    thiswind  
    OP
       2016-09-13 15:27:41 +08:00
    @doyel 直接执行那句话就行了,包治百病。。。不放心的话可以看源码,就是一个 bash 脚本, github 上 fork 的,亲测有效
    ivanfjz
        5
    ivanfjz  
       2016-09-13 15:27:47 +08:00
    ubuntu 读环境变量的。 centos 不读
    lizheming
        6
    lizheming  
       2016-09-13 15:32:34 +08:00
    vim /etc/crontab
    service crontab restart
    试试... 不过保险起见还是写绝对路径吧...=。=||
    walkman660
        7
    walkman660  
       2016-09-13 16:02:39 +08:00
    不自动那就自己加载环境变量
    source /etc/profile
    cxbig
        8
    cxbig  
       2016-09-13 16:03:52 +08:00
    求稳妥,所有命令和文件用绝对路径,环境变量直接先赋值,如果比较多就放文件里提前 source 一下
    kikoroc
        9
    kikoroc  
       2016-09-13 16:13:32 +08:00
    赞同 @cxbig 的说法。
    skydiver
        10
    skydiver  
       2016-09-13 16:23:26 +08:00
    自己把 PATH 写到 crontab 里就行了。
    这脚本干的一样的事儿
    thiswind
        11
    thiswind  
    OP
       2016-09-13 16:26:27 +08:00
    @skydiver 恩,这个脚本就是干这个事情:)
    这个脚本的目的是用短域名,这样方便用手机拍照,也方便在机房手工敲
    手动写 PATH 一不小心容易错
    skydiver
        12
    skydiver  
       2016-09-13 16:29:07 +08:00
    @thiswind echo PATH=$PATH 然后复制粘贴就行了

    在线上机器运行来源不明的脚本是大忌
    dreamtrail
        13
    dreamtrail  
       2016-09-14 12:37:35 +08:00
    第一行加上 PATH=/usr/local/bin:/bin:/usr/bin:/usr/sbin:/sbin 就行了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3273 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 11:51 · PVG 19:51 · LAX 04:51 · JFK 07:51
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.