By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,364 Members | 1,493 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,364 IT Pros & Developers. It's quick & easy.

Using Cron to run a python program

P: n/a
I'm using python 2.4 running on Fedora Core 4. I have written a python
program called ipscan.py that checks the external ip address of my
cable internet connection, and on change, will update the dns records
at my dns provider, zoneedit. So basically, I've setup dynamic dns
using python. Once the ip compare and/or update is complete, I log the
results to a text file called update.log. When I run the program in a
bash shell with the command "python ipscan.py", the program runs fine
and the results are appended to update.log. When I run this program as
a cron job under the root user with the previously mentioned command,
the program runs without errors but does not append an entry to the
log. The permissions on the update.log file should not be an issue
since I'm running the cron job as root, but I've given root write
permissions just to be safe. What would cause the logging to work at a
command prompt but fail in cron? I'm not getting any errors in the
cron logs or /var/spool/mail/root.

Thanks for your assitance.

Nov 24 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On 23 Nov 2005 16:23:11 -0800, vagrantbrad wrote
I'm using python 2.4 running on Fedora Core 4. I have written a python
program called ipscan.py that checks the external ip address of my
cable internet connection, and on change, will update the dns records
at my dns provider, zoneedit. So basically, I've setup dynamic dns
using python. Once the ip compare and/or update is complete, I log the
results to a text file called update.log. When I run the program in
a bash shell with the command "python ipscan.py", the program runs fine
and the results are appended to update.log. When I run this program
as a cron job under the root user with the previously mentioned
command, the program runs without errors but does not append an
entry to the log. The permissions on the update.log file should not
be an issue since I'm running the cron job as root, but I've given
root write permissions just to be safe. What would cause the
logging to work at a command prompt but fail in cron? I'm not
getting any errors in the cron logs or /var/spool/mail/root.


You're not giving us much detail about your script, so we can only guess. My
guess is that your python script is either not invoked at all or it dies
(raises an exception) before appending to the update.log.

You should keep in mind that cron jobs don't run in a normal login shell,
don't have the normal environment variables, and are not attached to a tty.
Any of those factors can conceivably cause a script to fail under cron when it
works fine from a shell.

HTH,

Carsten.
Nov 24 '05 #2

P: n/a
Carsten Haese wrote:
On 23 Nov 2005 16:23:11 -0800, vagrantbrad wrote
I'm using python 2.4 running on Fedora Core 4. I have written a python
program called ipscan.py that checks the external ip address of my
cable internet connection, and on change, will update the dns records
at my dns provider, zoneedit. So basically, I've setup dynamic dns
using python. Once the ip compare and/or update is complete, I log the
results to a text file called update.log. When I run the program in
a bash shell with the command "python ipscan.py", the program runs fine
and the results are appended to update.log. When I run this program
as a cron job under the root user with the previously mentioned
command, the program runs without errors but does not append an
entry to the log. The permissions on the update.log file should not
be an issue since I'm running the cron job as root, but I've given
root write permissions just to be safe. What would cause the
logging to work at a command prompt but fail in cron? I'm not
getting any errors in the cron logs or /var/spool/mail/root.

You're not giving us much detail about your script, so we can only guess. My
guess is that your python script is either not invoked at all or it dies
(raises an exception) before appending to the update.log.

You should keep in mind that cron jobs don't run in a normal login shell,
don't have the normal environment variables, and are not attached to a tty.
Any of those factors can conceivably cause a script to fail under cron when it
works fine from a shell.

Since we're guessing, *my* guess would be that the cron-triggered runs
are running in some directory other than the one the manually-triggered
jobs are, and that there is a separate update.log file in that directory.

The fix, of course, would be to use an absolute path to identify the log
file (if I'm right).

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

Nov 24 '05 #3

P: n/a

<va*********@yahoo.com> skrev i en meddelelse
news:11**********************@o13g2000cwo.googlegr oups.com...
permissions just to be safe. What would cause the logging to work at a
command prompt but fail in cron?


Because the environment is different; "man cron" might tell *how* it is
different (I cannot because it varies with platform, distribution ... )
Nov 24 '05 #4

P: n/a
You were right!! I had a relative path to the update.log file, so I
looked in the home path of the cron environment and found the
update.log file that the cron job was writing to. I updated my program
to point to the absolute path of the update.log file that I wanted the
logs written to, and now it works.

Thanks,
Brad
Steve Holden wrote:
Carsten Haese wrote:
On 23 Nov 2005 16:23:11 -0800, vagrantbrad wrote
I'm using python 2.4 running on Fedora Core 4. I have written a python
program called ipscan.py that checks the external ip address of my
cable internet connection, and on change, will update the dns records
at my dns provider, zoneedit. So basically, I've setup dynamic dns
using python. Once the ip compare and/or update is complete, I log the
results to a text file called update.log. When I run the program in
a bash shell with the command "python ipscan.py", the program runs fine
and the results are appended to update.log. When I run this program
as a cron job under the root user with the previously mentioned
command, the program runs without errors but does not append an
entry to the log. The permissions on the update.log file should not
be an issue since I'm running the cron job as root, but I've given
root write permissions just to be safe. What would cause the
logging to work at a command prompt but fail in cron? I'm not
getting any errors in the cron logs or /var/spool/mail/root.

You're not giving us much detail about your script, so we can only guess. My
guess is that your python script is either not invoked at all or it dies
(raises an exception) before appending to the update.log.

You should keep in mind that cron jobs don't run in a normal login shell,
don't have the normal environment variables, and are not attached to a tty.
Any of those factors can conceivably cause a script to fail under cron when it
works fine from a shell.

Since we're guessing, *my* guess would be that the cron-triggered runs
are running in some directory other than the one the manually-triggered
jobs are, and that there is a separate update.log file in that directory.

The fix, of course, would be to use an absolute path to identify the log
file (if I'm right).

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/


Nov 24 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.