Environment Variables when python script run by cron

后端 未结 5 2061
渐次进展
渐次进展 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条回答
  •  有刺的猬
    2020-12-16 10:36

    This is one of the approach I like, write a script to set environment and execute the script with its parameters as its parameters

    set_env_to_process.sh

    #!/usr/bin/env bash
    echo "TEST_VAR before export is: <$TEST_VAR>"
    
    export TEST_VAR=/opt/loca/netcdf
    echo "TEST_VAR after export is: <$TEST_VAR>"
    export PATH=$PATH:/usr/bin/python3.5
    export PYTHTONPATH=$PYTHONPATH:/my/installed/pythonpath
    
    # execute command and its parameters as input for this script
    if [ $# -eq 0 ]; then
        echo "No command to execute"
    else
        echo "Execute commands with its parameters: $@"
        eval $@
    fi
    

    usage

    /usr/bin/python3.5 /code/scraper.pyare taken as input for set_env_to_process.sh set_env_to_process.sh set the correct env for script to run

    It could be used as command line, cron, sudo, ssh to setup env

     * * * * * root set_env_to_process.sh /usr/bin/python3.5 /code/scraper.py
    

提交回复
热议问题