Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
HarryQu
V2EX  ›  Linux

为什么 Linux crontab 使用 source ~/.bash_profile 不生效 ?

  •  
  •   HarryQu · Mar 6, 2019 · 6694 views
    This topic created in 2649 days ago, the information mentioned may be changed or developed.

    问题: 我使用 crontab 做定时任务 。写了一个脚本如下 :

    #!/bin/bash
    cd  ~/project/python/spider/spider
    nohup pipenv run python  main.py  >> log.txt 2>&1  &
    

    因为环境变量的问题,以上脚本并不能正常执行。 因此我添加了环境变量 :

    #!/bin/bash
    source $HOME/.bash_proifle
    echo $PATH  >> ~/hello.txt
    cd  ~/project/python/spider/spider
    nohup pipenv run python  main.py  >> log.txt 2>&1  &
    

    然而我发现使用 source $HOME/.bash_proifle 后,环境变量 $PATH 仍然没有改变。 我必须手动指定 PATH=/home/apple/.local/bin:/home/apple/bin:$PATH crontab 才能正常执行,如下代码 :

    
    #!/bin/bash
    PATH=/home/apple/.local/bin:/home/apple/bin:$PATH
    cd  ~/project/python/spider/spider
    nohup pipenv run python  main.py  >> log.txt 2>&1  &
    
    

    其中 ~/.bash_profile 内容如下 :

    # .bash_profile
    
    # Get the aliases and functions
    if [ -f ~/.bashrc ]; then
            . ~/.bashrc
    fi
    
    # User specific environment and startup programs
    
    PATH=$PATH:$HOME/.local/bin:$HOME/bin
    
    export PATH
    
    
    18 replies    2019-03-07 08:55:49 +08:00
    boris1993
        1
    boris1993  
       Mar 6, 2019 via Android
    直接把 b 变量写在命令前面看看,并且不要涉及$HOME 之类的用户相关的变量

    PATH=$PATH:/home/your_name/bin nohup pipenv (略)
    xiaket
        2
    xiaket  
       Mar 6, 2019
    你可以做这个实验来看看 cron 是用什么环境变量跑的:

    ```
    * * * * * export > /tmp/cron_env
    ```
    wlsnx
        3
    wlsnx  
       Mar 6, 2019
    你可以看一下.bashrc 或者它 source 的其他文件,看看有没有这种语句 [ -z "$PS1" ] && return
    dangyuluo
        4
    dangyuluo  
       Mar 6, 2019
    看看 stderr of line `source $HOME/.bash_proifle`
    shakespaces
        5
    shakespaces  
       Mar 6, 2019 via Android
    crontab 有时候当前环境搞不清,一般我都直接用绝对路径
    zjlletian
        6
    zjlletian  
       Mar 6, 2019
    重启 /usr/sbin/crond
    xiaoxinxiaobai
        7
    xiaoxinxiaobai  
       Mar 6, 2019 via Android
    你需要知道 crontab 运行一个 task 的时候,$HOME 是啥
    zthxxx
        8
    zthxxx  
       Mar 6, 2019   ❤️ 1
    如果你是在云服务器上, `.bashrc` 文件里通常很可能有 `if [ "$BASH" ]` `if [ -z "$PS1" ] ` stty 之类的语句判断不是 shell 环境就退出;

    并且 crontab 是根据 passwd 中启动用户的指向来设置 $HOME 变量的


    > Several environment variables are set up automatically by the cron(8) daemon. SHELL is set to /bin/sh, and LOGNAME and HOME are set from the /etc/passwd line of the crontab's owner.


    https://manpages.debian.org/stretch/cron/crontab.5.en.html
    liubin
        9
    liubin  
       Mar 6, 2019
    用.bashrc 吧,bash_profile 是登录 shell 用的吧。
    Sherlocker
        10
    Sherlocker  
       Mar 6, 2019
    你可以直接配置 crontab 时直接加上 source ~/.bash_proifle && 执行脚本 ,一点问题都没
    defunct9
        11
    defunct9  
       Mar 6, 2019
    开 ssh,让我上去看看
    rootit
        12
    rootit  
       Mar 6, 2019
    要用 . source 好像找不到
    alexsunxl
        13
    alexsunxl  
       Mar 6, 2019
    crontab 尽量用绝对路径, 这个坑踩过几回
    flyingpot
        14
    flyingpot  
       Mar 6, 2019
    profile 打错了,是复制的吗
    a15819620038
        15
    a15819620038  
       Mar 6, 2019 via iPhone
    全路径
    Beebird
        16
    Beebird  
       Mar 6, 2019
    我倒是觉得先要搞清楚哪处需要 /home/apple/bin 或 home/apple/.local/bin 作为 PATH 的搜索路径。是楼主的 pipenv 和 python ?还是 main.py 里面有例如 subprocess 之类的调用? 找到问题再对症下药。
    sparkssssssss
        17
    sparkssssssss  
       Mar 7, 2019 via iPhone
    source /etc/profile
    julyclyde
        18
    julyclyde  
       Mar 7, 2019
    这种情况,我一般会 strace -f 一下
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2486 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 62ms · UTC 01:30 · PVG 09:30 · LAX 18:30 · JFK 21:30
    ♥ Do have faith in what you're doing.