Where can I set environment variables that crontab will use?

后端 未结 17 1916
说谎
说谎 2020-11-22 05:47

I have a crontab running every hour. The user running it has environment variabless in the .bash_profile that work when the user runs the job from the terminal,

17条回答
  •  傲寒
    傲寒 (楼主)
    2020-11-22 06:23

    I tried most of the provided solutions, but nothing worked at first. It turns out, though, that it wasn't the solutions that failed to work. Apparently, my ~/.bashrc file starts with the following block of code:

    case $- in
        *i*) ;;
        *) return;;
    esac
    

    This basically is a case statement that checks the current set of options in the current shell to determine that the shell is running interactively. If the shell happens to be running interactively, then it moves on to sourcing the ~/.bashrc file. However, in a shell invoked by cron, the $- variable doesn't contain the i value which indicates interactivity. Therefore, the ~/.bashrc file never gets sourced fully. As a result, the environment variables never got set. If this happens to be your issue, feel free to comment out the block of code as follows and try again:

    # case $- in
    #     *i*) ;;
    #     *) return;;
    # esac
    

    I hope this turns out useful

提交回复
热议问题