Running a Python script from crontab

Philip Semanchuk philip at
Wed Dec 3 21:32:11 CET 2008

On Dec 3, 2008, at 3:06 PM, Astley Le Jasper wrote:

> On 3 Dec, 19:49, Philip Semanchuk <phi... at> wrote:
>> On Dec 3, 2008, at 1:33 PM, Astley Le Jasper wrote:
>>> On 3 Dec, 16:41, Philip Semanchuk <phi... at> wrote:
>>>> On Dec 3, 2008, at 10:29 AM, Astley Le Jasper wrote:
>>>>> I've included a switch to include or exclude theloggingto console.
>>>>> Whenloggingonly to file, the script runs fine.
>>>>> Of course, I still don't understand whyduallogging, and  
>>>>> specifically
>>>>> to the console, causes a problem and if anyone has any comments
>>>>> about
>>>>> thedualoutputloggingcode above then I'd still be happy to hear
>>>>> about it.
>>>> Trying to write non-ASCII characters perchance?
>>> Errmmm ... that's kind of spoookey. I using UTF-8 encoding as I  
>>> have a
>>> lot of European language characters. But would that cause a problem
>>> when running from crontab but not in the terminal?
>>> Go on then ... spill the beans.
>> Oh, I don't know exactly. It's just what I thought of when you said
>> that the problem occurs when logging to the console but not to files.
>> I don't have a deep Unix background so I can't give you the details  
>> on
>> what "the console" means to a cron job.
>> In my experience, the environment in which a cron job runs is
>> different from the environment in which some command line scripts run
>> (remember my earlier suggestion about needing to explicitly set the
>> PATH?) and so if the console for a cron job differed from the console
>> that a Python program sees when run in a terminal, that would not
>> surprise me.
>> Hope it's a useful suggestion
>> Philip
> I tried using the path variables but it didn't help. There again, I
> didn't really understand what was being passed in the path variables
> and, rather than just copying and pasting, I wanted to look up further
> info to get to grips with it. I've got something that works, so now
> the pressure is off I can spend some time looking it up.
> [sigh ... my experiences with linux haven't been that good so far. It
> is, if nothing else, interesting]

Sorry, I was misleading. I don't think the PATH has anything to do  
with why your script can't log to the console when run as a cron job.  
I was just pointing out that my Python scripts have had environment- 
related problems when run as cron jobs, so it would not surprise me if  
that's your problem too. The PATH was just an example of an  
environment element that wasn't what I expected.

Just a thought -- if my weak hunch that non-ASCII output is your  
problem, you might be able to test this easily (depending on how you  
use the logger) by forcing all output messages to "foo". If your cron  
job doesn't crash then, you know that the *content* of the output is  
probably the culprit which would indeed point to an encoding problem.


More information about the Python-list mailing list