Where can I set environment variables that crontab will use?

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

    You can define environment variables in the crontab itself when running crontab -e from the command line.

    LANG=nb_NO.UTF-8
    LC_ALL=nb_NO.UTF-8
    # m h  dom mon dow   command
    
    * * * * * sleep 5s && echo "yo"
    

    This feature is only available to certain implementations of cron. Ubuntu and Debian currently use vixie-cron which allows these to be declared in the crontab file (also GNU mcron).

    Archlinux and RedHat use cronie which does not allow environment variables to be declared and will throw syntax errors in the cron.log. Workaround can be done per-entry:

    # m h  dom mon dow   command
    * * * * * export LC_ALL=nb_NO.UTF-8; sleep 5s && echo "yo"
    

提交回复
热议问题