python script is not running

A

Avnesh Shakya

hi,
i want to run python script which generating data into json fromat, I am using crontab, but it's not executing...
my python code--
try.py --

import json
import simplejson as json
import sys

def tryJson():
saved = sys.stdout
correctFile = file('data.json', 'a+')
sys.stdout = correctFile
result = []
i = 1
for i in range(5):
info = {
'a': i+1,
'b': i+2,
'c': i+3,
}
result.append(info)

if result:
print json.dumps(result, indent=6)
sys.stdout = saved
correctFile.close()
tryJson()

now i m doing ion terminal-
avin@hp:~$ crontab -e
then type -
*/2 * * * * python /home/avin/data/try.py

and save

but it's not executing.
 
C

Chris Angelico

avin@hp:~$ crontab -e
then type -
*/2 * * * * python /home/avin/data/try.py

You may need to put an explicit path to your Python interpreter. Type:

$ which python

and put that into your crontab.

ChrisA
 
R

Roy Smith

Chris Angelico said:
You may need to put an explicit path to your Python interpreter. Type:

$ which python

and put that into your crontab.

True. Somewhat more generally, jobs run under cron have a far more
barren environment than most people realize. Or, looking at it a
different way, most people don't even realize all the ways they depend
on their environment being set up properly by the login process.

If you've set things like PYTHONPATH, you won't have them set right for
cron jobs unless you explicitly reset them in your crontab.

It's often instructive to run something like "env > /tmp/xxx" under
cron, and compare that to what you get when you run "env" at a
command-line prompt.
 
T

Terry Jan Reedy

hi,
i want to run python script which generating data into json fromat, I am using crontab, but it's not executing...
my python code--
try.py --

import json
import simplejson as json
import sys

def tryJson():
saved = sys.stdout
correctFile = file('data.json', 'a+')
sys.stdout = correctFile

Don't need to change stdout.
result = []
i = 1
for i in range(5):
info = {
'a': i+1,
'b': i+2,
'c': i+3,
}
result.append(info)

What is you want to add print result for debugging?
if result:
print json.dumps(result, indent=6)

Either use print >> correctFile, correctFile.write (do you really want
the extra '\n' that print adds?), or, do the future import of print as
function and pass correctFile as the file argument
 
W

woooee

The obvious question, do you have the shebang on the first line so the
OS knows it's to be run as a Python program? Also I would change
tryJson() to
if __name__ == "__main__':
tryJson()
This probably won't make any difference but you will have the bases
covered.
 
C

Chris Angelico

The obvious question, do you have the shebang on the first line so the
OS knows it's to be run as a Python program?

That won't make any difference; the cron job specifically stipulates
the interpreter. It just needs to be done with a full path.

ChrisA
 
V

Vincent Vande Vyvre

Le 18/05/2013 19:59, Chris Angelico a écrit :
That won't make any difference; the cron job specifically stipulates
the interpreter. It just needs to be done with a full path.

ChrisA
Not necessary, I use crontab without problem with this line:

25 16 18 5 * export DISPLAY=:0 & LC_CTYPE="fr_FR.utf-8"
Lang="fr_FR.utf-8" python /usr/bin/qarte -a 1

.... on Ubuntu.
 
C

Chris Angelico

Le 18/05/2013 19:59, Chris Angelico a écrit :


Not necessary, I use crontab without problem with this line:

25 16 18 5 * export DISPLAY=:0 & LC_CTYPE="fr_FR.utf-8" Lang="fr_FR..utf-8"
python /usr/bin/qarte -a 1

... on Ubuntu.

Everything's configurable. I'd like to hear back from the OP though;
as Roy said, checking 'env' from a cron job will reveal much.

ChrisA
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top