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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
03-06-2006, 05:38 PM
|
#1
|
LQ Newbie
Registered: Dec 2003
Location: Portugal
Posts: 22
Rep:
|
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.
|
|
|
03-07-2006, 01:39 AM
|
#2
|
Senior Member
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515
Rep:
|
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 at command line).
|
|
|
03-07-2006, 10:38 AM
|
#3
|
LQ Newbie
Registered: Dec 2003
Location: Portugal
Posts: 22
Original Poster
Rep:
|
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.
|
|
|
03-08-2006, 02:11 AM
|
#4
|
Senior Member
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515
Rep:
|
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.
|
|
|
03-08-2006, 09:21 AM
|
#5
|
LQ Newbie
Registered: Dec 2003
Location: Portugal
Posts: 22
Original Poster
Rep:
|
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.
|
|
|
03-08-2006, 09:59 AM
|
#6
|
Senior Member
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515
Rep:
|
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.
|
|
|
03-08-2006, 10:16 AM
|
#7
|
Senior Member
Registered: Aug 2003
Location: UK
Distribution: Slackware
Posts: 3,467
Rep: 
|
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.
|
|
|
03-09-2006, 02:02 AM
|
#8
|
Senior Member
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515
Rep:
|
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.
|
|
|
All times are GMT -5. The time now is 11:51 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|