LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 03-06-2006, 05:38 PM   #1
spiri
LQ Newbie
 
Registered: Dec 2003
Location: Portugal
Posts: 22

Rep: Reputation: 15
Unhappy Python program not running from cron


Hi,
I made a small program in python to send mail (without the need of mail server in the running machine)
it work fine if started from the command line but executed from a bash script in a cron schedule it
doesn't run (send the mail) but the bash script runs ok.

I made a lot of tests.

Any help will be very grateful.

Thanks.
 
Old 03-07-2006, 01:39 AM   #2
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
In many cases, when a program runs fine at your regular command line and doesn't work when used in a cron schedule, the problem is the setting of the environment variables.
Cron uses it's own, limited settings of environment variables. If your Python program requires any, please make sure that you set them up properly. The easiest way for you to do this is to include some exports in
the bash script that runs the Python program.

Maybe the man pages for python show you what environment settings you'll need. Or simply post the contents of your environment settings (output of command
Code:
env
at command line).
 
Old 03-07-2006, 10:38 AM   #3
spiri
LQ Newbie
 
Registered: Dec 2003
Location: Portugal
Posts: 22

Original Poster
Rep: Reputation: 15
Here it goes my env output:
Quote:
TERM=xterm
SHELL=/bin/bash
SSH_CLIENT=192.168.0.5 3078 22
SSH_TTY=/dev/pts/0
USER=root
LS_COLORS=no= (ommited)
MAIL=/var/mail/root
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11
PWD=/home2/tmp
LANG=en_US
PS1=\h:\w\$
SHLVL=1
HOME=/root
LANGUAGE=en_PT:en_US:en_GB:en
LS_OPTIONS=--color=auto
LOGNAME=root
SSH_CONNECTION=192.168.0.5 3078 192.168.0.1 22
_=/usr/bin/env
OLDPWD=/
Thanks for your replay.
 
Old 03-08-2006, 02:11 AM   #4
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
And what are your PhP e-mail settings (probably in a file called php.ini) and commands?

Are you sure that crontab finds and runs your program? ie do you see a PhP error message? Or a shell error message (like "file not found")?

Please post your crontab settings as well.

From your "env" output, I don't see much abnormal. The only suspects for me, are $PATH and $PWD so far.
 
Old 03-08-2006, 09:21 AM   #5
spiri
LQ Newbie
 
Registered: Dec 2003
Location: Portugal
Posts: 22

Original Poster
Rep: Reputation: 15
Hi,
I'am not using PhP!
this bash script calls the python program,
it makes a file with the df output

Code:
#!/bin/bash
rm diskfree.txt -f
echo `date` > /var/log/diskfree.txt
df -h >> /var/log/diskfree.txt
python anexo_shell.py /var/log/diskfree.txt

this is the python code
it sends the diskfree.txt by mail.

anexo means attachment

Code:
import smtplib
import sys
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
from email import Encoders
import os

def sendMail(to, subject, text, files=[],server="mail.mypythontest.com"):
    assert type(to)==list
    assert type(files)==list
    fro = "<xeo@mypythontest.com>"

    msg = MIMEMultipart()
    msg['From'] = fro
    msg['To'] = COMMASPACE.join(to)
    msg['Date'] = formatdate(localtime=True)
    msg['Subject'] = subject

    msg.attach( MIMEText(text) )

    for file in files:
        part = MIMEBase('application', "octet-stream")
        part.set_payload( open(file,"rb").read() )
        Encoders.encode_base64(part)
        part.add_header('Content-Disposition', 'attachment; filename="%s"'
                       % os.path.basename(file))
        msg.attach(part)

    smtp = smtplib.SMTP(server)
    smtp.sendmail(fro, to, msg.as_string() )
    smtp.close()


sendMail(
        ["xeo@mypithontest.com"],
        "Registo de backups","Mensagem automatica.",
        [sys.argv[1]]
    )
If I run the script it works as expected, by if it as to be run by cron
the script create the file output but don't send the mail.

Thanks a lot for the help.
 
Old 03-08-2006, 09:59 AM   #6
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
Sorry, I mistook your thread for another one. Of course you're not working with PhP but with Python.

There may be an easier way to send the mails you want (ie using /bin/mail or the "nail" program you can download for free). But let's try to make the Python program work first.

Do you get any error messages? Cron usually sends them by mail to the user who submitted the cron job, unless you configured it otherwise. But you may be able to redirect standard output of the cron job into a file so that you can read it afterwards.

Furthermore, does Python know where to find it's libraries (for the import-statements)?

You can also try putting the full path to the Python interpreter in your Bash script, rather than just putting "python". When running a program using cron, it doesn't use your normal $PATH settings, so it may be unable to find your python executable.
 
Old 03-08-2006, 10:16 AM   #7
dive
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Slackware
Posts: 3,467

Rep: Reputation: Disabled
Quote:
Originally Posted by spiri
Code:
#!/bin/bash
rm diskfree.txt -f
echo `date` > /var/log/diskfree.txt
df -h >> /var/log/diskfree.txt
python anexo_shell.py /var/log/diskfree.txt
You probably need to put full path to diskfree.txt, python and anexo_shell.py
Cron is notorious for not knowing $PATH

Last edited by dive; 03-08-2006 at 10:18 AM.
 
Old 03-09-2006, 02:02 AM   #8
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
Quote:
Originally Posted by dive
Cron is notorious for not knowing $PATH
Indeed it is and for obvious and clearly documented reasons.
This is mentioned explicitly in the man pages of cron and/or crontab.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
running java program through cron manu82 Linux - Software 2 02-28-2006 10:35 PM
First Python Program dudeman41465 Programming 3 01-21-2006 02:22 PM
cron job not running vincebs Linux - Software 34 10-30-2004 01:27 PM
Making a Python program gamehack Programming 0 04-11-2004 05:12 AM
Cron not running Half_Elf Linux - Software 4 01-10-2003 02:38 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 11:51 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration