Environment Variables when python script run by cron

后端 未结 5 2079
渐次进展
渐次进展 2020-12-16 09:56

I have been looking at other stack overflow questions but couldn\'t get any to work. I have a python script which uses environment variables. This script works exactly as pl

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-16 10:30

    You can add it to the top of your crontab and keep it out of version control. Let's say the environment variable causing you difficulty is export DJANGO_SECRET_KEY="FOOBAR_1241243124312341234":

    crontab

    DJANGO_SECRET_KEY="FOOBAR_1241243124312341234"
    
    SCRIPT_NAME = my_cool_script
    20 21 * * 1-5 bash ~/git_repo/cronjobs/$SCRIPT_NAME.sh 2&>1 | tee ~/git_repo/cronjobs/logs/$SCRIPT_NAME.log
    

    my_cool_script.sh

    #!/usr/bin/env bash
    ~/anaconda3/envs/django/bin/python ~/git_repo/django_project/manage.py run_command
    

    This has worked well for me when the environment variables in question need to be kept secret and the loading of existing .bashrc does not play nice for whatever reason.

提交回复
热议问题