2

When I run python deamon.py, the test suite runs properly; however, when I run it with /usr/bin/python it throws a warning for a Casper.waitfor() timeout as it apparently can't read the DOM elements.

// Fails:
/usr/bin/python  /path_to_deals/deals/deamon.py

// Succeeds:
python  /path_to_deals/deals/deamon.py

Below is the directory structure:

deals/

 deamon.py
 test.js

deamon.py is the script which initiates the casperjs test suite in the test.js file.

Can any one explain why python works but /usr/bin/python does not -- and how I might fix this?

Update from discussion: "When I do python deamon.py it fails only when I put it in a Cron (where I have to specify the exact python path as well as the script path)."

9
  • 1
    Are you sure /usr/bin/python and python are referencing the same Python version? Commented Jul 18, 2014 at 12:10
  • You can see the Python version used with which python Commented Jul 18, 2014 at 12:16
  • yes, they are same. In fact I have only one python version i.e python2.7 Commented Jul 18, 2014 at 12:16
  • 1
    Out of curiosity, does it also fail if you use /usr/bin/env python instead? Commented Jul 18, 2014 at 12:18
  • (Perhaps a very relevant AskUbuntu post: askubuntu.com/questions/372672/…) Commented Jul 18, 2014 at 12:19

1 Answer 1

2

As Arcege explains here, "Cron knows nothing about your shell; it is started by the system, so it has a minimal environment. If you want anything, you need to have that brought in yourself."

Most likely, there is some variable that is set in your session that is being forgotten by Cron. Here are three options:

1.) You could either set the environmental variables for a specific command as Nischal does here.

Gist:

* * * * * . $HOME/.profile; python /path/to/myScript.py

2.) Set the an environmental variable for the entire Crontab as they do here.

Gist:

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
* * * * *   python /path/to/myScript.py

(More on setting environmental variables in Cron.)


3.) Set the environmental variables in the script itself. Python's urllib2 apparently needs the http_proxy variable, so if you're using that you might have a problem. You could use os to set it (source) or -- what might be better -- you could specify the proxy that urllib2 should use as ZelluX does here with a ProxyHandler.

Gist:

proxy = urllib2.ProxyHandler({'http': '127.0.0.1'})
opener = urllib2.build_opener(proxy)
urllib2.install_opener(opener)
urllib2.urlopen('http://www.example.com')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.