[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

How to set up environment for Python in cron?

Posted on 2010-11-09
6
Medium Priority
?
1,408 Views
Last Modified: 2012-05-10
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!
0
Comment
Question by:eduliu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 34096937
Simplest way would be running the script from root's crontab as the "normal" user with "su -", so that the full environment of that user will be initialized.

02 * * * * su - normal_user -c "/home/<user>/test.py input1 > /tmp/test.log 2>&1"

wmp
0
 

Author Comment

by:eduliu
ID: 34097024
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!
0
 
LVL 17

Expert Comment

by:gelonida
ID: 34100058
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_PATH'] = 'newpath:' + os.environ['LD_LIBRARY_PATH']
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Expert Comment

by:gelonida
ID: 34100068
the wrapper script solution would be:

#!/bin/sh
export PATH="newpath:$PATH"
export LD_LIBRARY_PATH="newpath:$LD_LIBRARY_PATH"

mypythonscript "$@"


0
 
LVL 17

Accepted Solution

by:
gelonida earned 2000 total points
ID: 34100091
Third way is to set the vars directly in the crontab entry:


PATH=/usr/bin:mypath LD_LIBRARY_PATH=/usr/bin:mypath  /home/<user>/test.py input1


Please note.
no semicolons and no export statementments.

In all three cases you must be sure to add ALL required directories and all required environment variables, that are needed
for your third party executable to be run.
As I don't knowit you might have to experiment
0
 

Author Comment

by:eduliu
ID: 34102669
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!
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7. Lets install Flask! To install Flask you can use a python repository for libraries tool called pip. Download this f…
Article by: Swadhin
Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question