Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

python script is not running

30 views
Skip to first unread message

Avnesh Shakya

unread,
May 18, 2013, 6:12:30 AM5/18/13
to
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.

fjc...@gmail.com

unread,
May 18, 2013, 7:18:40 AM5/18/13
to Avnesh Shakya, pytho...@python.org
make sure data.json is absolute path
make sure you test it directly without crontab
in the crontab, execute the script in such way, python scripty > /tmp/log 2>&1 check the log

发自我的小米手机

Avnesh Shakya <avnes...@gmail.com>编写:

>--
>http://mail.python.org/mailman/listinfo/python-list

Chris Angelico

unread,
May 18, 2013, 9:28:03 AM5/18/13
to pytho...@python.org
On Sat, May 18, 2013 at 8:12 PM, Avnesh Shakya <avnes...@gmail.com> wrote:
> 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

Roy Smith

unread,
May 18, 2013, 9:44:25 AM5/18/13
to
In article <mailman.1801.1368883...@python.org>,
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.

Terry Jan Reedy

unread,
May 18, 2013, 12:35:19 PM5/18/13
to pytho...@python.org
On 5/18/2013 6:12 AM, Avnesh Shakya wrote:
> 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

woooee

unread,
May 18, 2013, 1:35:23 PM5/18/13
to
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.

Chris Angelico

unread,
May 18, 2013, 1:59:12 PM5/18/13
to pytho...@python.org
On Sun, May 19, 2013 at 3:35 AM, woooee <woo...@gmail.com> wrote:
> 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

Vincent Vande Vyvre

unread,
May 18, 2013, 2:43:00 PM5/18/13
to pytho...@python.org
Le 18/05/2013 19:59, Chris Angelico a �crit :
> On Sun, May 19, 2013 at 3:35 AM, woooee <woo...@gmail.com> wrote:
>> 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
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.

--
Vincent V.V.
Oqapy <https://launchpad.net/oqapy> . Qarte
<https://launchpad.net/qarte> . PaQager <https://launchpad.net/paqager>

Chris Angelico

unread,
May 18, 2013, 7:33:43 PM5/18/13
to pytho...@python.org
On Sun, May 19, 2013 at 4:43 AM, Vincent Vande Vyvre
<vincent.v...@swing.be> wrote:
> Le 18/05/2013 19:59, Chris Angelico a écrit :
>
>> On Sun, May 19, 2013 at 3:35 AM, woooee <woo...@gmail.com> wrote:
>>>
>>> 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
>
> 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
0 new messages