Question

I am really puzzled on this issue. I am using python's SimpleXMLRPC to provide services to a web application.

The problem is that when I start my xmlrpc server from the command line everything runs smoothly but when it is started through crontab it doesn't.

I have tried to hold the start-up via sleep and checking /sys/class/net/eth0/device/net/eth0/operstate but got no luck.

Please find attached the source for the script:

#!/usr/local/bin/python2.5
# -*- coding: utf-8 -*-
# License: GNU
# startxmlrpc.py: startup script for xmlrpc server to deal with processing

## {{{ http://code.activestate.com/recipes/439094/ (r1)
import socket
import fcntl
import struct

def get_ip_address(ifname):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    return socket.inet_ntoa(fcntl.ioctl(
        s.fileno(),
        0x8915,  # SIOCGIFADDR
        struct.pack('256s', ifname[:15])
        )[20:24])
## end of http://code.activestate.com/recipes/439094/ }}}

import xmlrpclib
import urllib2
import os
from SimpleXMLRPCServer import SimpleXMLRPCServer
from time import sleep

def send(img1,img2,lib,filters):

    global HOST_IP

    path = '/var/www/%s/' % MD5Cypher(HOST_IP)
    makedirs(path)
    print "Path: %s" % path

    if lib=='devel':
        os.system("""python ~/devel_funcs.py %s %s "%s" &""" % (img1_path,img2_path, filters))

    if lib=='milena':
            import milena_funcs
            milena_funcs.mln_process(img1_path, filters)

    return HOST_IP + '/' + path.split('/var/www/')[1] + 'out.pgm'


while open('/sys/class/net/eth0/operstate').read().strip() != 'up':
    sleep(5)

HOST_IP = get_ip_address('eth0')
server = SimpleXMLRPCServer((HOST_IP, 7070))
server.register_function(send)
server.serve_forever()

This is the error I get if I try to launch my process just after a clean boot:

<class 'xmlrpclib.Fault'>: <Fault 1: "<class 'xmlrpclib.ProtocolError'>:<ProtocolError for 192.168.0.5:7070/RPC2: -1 >"> 
      args = () 
      faultCode = 1 
      faultString = "<class 'xmlrpclib.ProtocolError'>:<ProtocolError for 192.168.0.5:7070/RPC2: -1 >" 
      message = ''

If I kill it and run it again, it works.

This is the crontab:

usrmln@Slave1:~$ crontab -l
# m h  dom mon dow   command
* * * * * python ~/master_register.py > /dev/null 2>&1
* * * * * python ~/startxmlrpc.py > /dev/null 2>&1
0   5 * * * find /var/www/ -type d -mtime +3 -exec rm -rf {} \; > /dev/null 2>&1
Was it helpful?

Solution

You don't show what the error is, but, it is possibly that PYTHONPATH is not being set when run from cron. You could set it before running the script.

Or, of course, you are running it as a different user and file permissions are not correctly set. Also ~/devel_funcs.py will not refer to your home directory if cron runs your script as a different user.

OTHER TIPS

I finally got it, I was using python 2.5 locally and I had to add to the execution like this:

          • /usr/local/bin/python2.5 /home/username/startxmlrpc.py > /dev/null 2>&1
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top