Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Environment Variables when python script run by cron

People also ask

Does cron have environment variables?

You can define environment variables in the crontab itself when running crontab -e from the command line. 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).

Can I run Python script from cron?

Python can be used to schedule the automated execution of preprogrammed tasks using cron. But before we dive into exactly how to automate the execution of such Python scripts, let's quickly discuss cron and Python.

Can I use variables in crontab?

Yes, you can define and use variables in this way.

How do you use an environment variable in a Python script?

The name of the environment variable is used as the index of the environ[] array to set or get the value of that variable. The get() function is used to get the value of a particular variable, and setdefault() function is used to set the value of the particular variable.


Instead of executing the whole ~/.profile what I'd do is move the variables that must be shared between your cron jobs and the account that has the profile, then I'd source these both in ~/.profile and in the cron job.

The last attempt you show in the question is not properly formatted. The user id should be coming right after the scheduling information, but you've added the sourcing of the profile before the user id, which surely cannot work.

Here's an example setup that I've tested here:

*/1 * * * * someuser . /tmp/t10/setenv && /usr/bin/python /tmp/t10/test.py

I've set it to execute every minute for testing purposes. Replace someuser with something that makes sense. The /tmp/t10/setenv script I used had this:

export FOO=foovalue
export BAR=barvalue

The /tmp/t10/test.py file had this:

import os

print os.environ["FOO"], os.environ["BAR"]

My cron emails me the output of the scripts it runs. I got an email with this output:

foovalue barvalue

You can set the env variable inline:

* * * * * root ENV_VAR=VALUE /usr/bin/python3.5 /code/scraper.py

Another way is use honcho that you can pass a file with env variables.

honcho -e /path/to/.env run /code/scraper.py


You can specify your two environment variables by this:

* * * * * root env A=1 B=2 /usr/bin/python3.5 /code/scraper.py

env is a system program that runs a specified program with additional variables:

$ env A=1 B=2 /bin/sh -c 'echo $A$B'  # or just 'sh': would search in $PATH
12

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