Where can I set environment variables that crontab will use?

后端 未结 17 1946
说谎
说谎 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:29

    Expanding on @carestad example, which I find easier, is to run the script with cron and have the environment in the script.

    In crontab -e file:

    SHELL=/bin/bash
    
    */1 * * * * $HOME/cron_job.sh
    

    In cron_job.sh file:

    #!/bin/bash
    source $HOME/.bash_profile
    some_other_cmd
    

    Any command after the source of .bash_profile will have your environment as if you logged in.

提交回复
热议问题