eduliu
asked on
How to set up environment for Python in cron?
I have a Python script that runs fine standalone. When I add it to the cron on a Solaris, it fails. From the output log, the problem is it cannot find the PATH and LID_LIBRARY_PATH of the executable embedded in the Python. My question is how to set this up? Should this be done in the Python or in the cron? I have tried various ways without success. Here are two examples:
02 * * * * /home/<user>/test.py input1 > /tmp/test.log 2>&1
02 * * * * SHELL=/usr/bin/bash; export SHELL; . $HOME/.profile_user; /home/<user>/test.py input1 > /tmp/test.log 2>&1
Basically the Python runs with one input parameter. In the Python script, it calls a 3rd party executable. I see two env variable scripts I need to run - one is the .profile_user and another one is the app specific env.
Many thanks!
02 * * * * /home/<user>/test.py input1 > /tmp/test.log 2>&1
02 * * * * SHELL=/usr/bin/bash; export SHELL; . $HOME/.profile_user; /home/<user>/test.py input1 > /tmp/test.log 2>&1
Basically the Python runs with one input parameter. In the Python script, it calls a 3rd party executable. I see two env variable scripts I need to run - one is the .profile_user and another one is the app specific env.
Many thanks!
ASKER
I just tried, but it didn't work - it didn't run. I guess it's becuase I'm not running cron as root. The account I use can create cron but under my account I assume. Any other way to insert a couple of scripts to set up the environment? Thanks!
if you want to call 3rd party executables, then it is enough to set the PATH variable from within python before you call your executable.
Alternatively you can create a wrapper shell script setting up the env before calling your python script.
import os
os.environ['PATH'] = 'newpath:' + os.environ['PATH']
os.environ['LD_LIBRARY_PAT H'] = 'newpath:' + os.environ['LD_LIBRARY_PAT H']
Alternatively you can create a wrapper shell script setting up the env before calling your python script.
import os
os.environ['PATH'] = 'newpath:' + os.environ['PATH']
os.environ['LD_LIBRARY_PAT
the wrapper script solution would be:
#!/bin/sh
export PATH="newpath:$PATH"
export LD_LIBRARY_PATH="newpath:$ LD_LIBRARY _PATH"
mypythonscript "$@"
#!/bin/sh
export PATH="newpath:$PATH"
export LD_LIBRARY_PATH="newpath:$
mypythonscript "$@"
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you so much for the detailed and various options. Quite embarrassing, the cause was one variable in one of the env scripts had a value assigned wrong. Things are working now. Appreciate the help!
02 * * * * su - normal_user -c "/home/<user>/test.py input1 > /tmp/test.log 2>&1"
wmp