user4910881
user4910881

Reputation:

Cronjob error when trying to call python function

When I try to call a python function using a cronjob it returns an error.

The function works okay when I run it from the python shell

Here's the error:

: command not found
import: unable to open X server `' @ error/import.c/ImportImageCommand/368.
import: unable to open X server `' @ error/import.c/ImportImageCommand/368.
import: unable to open X server `' @ error/import.c/ImportImageCommand/368.
import: unable to open X server `' @ error/import.c/ImportImageCommand/368.
: command not found
: command not found
-bash: from: command not found
: command not found
: command not found
-bash: message: command not found
: command not founde is very slow,
: command not foundway either I restarted the server or it's not responding,
: command not founds down :(}
-bash: address: command not found
: command not found
: command not found
-bash: kb_log.py: line 25: syntax error near unexpected token `('
'bash: kb_log.py: line 25: `def test_speed(url="http://www.kenyabuzz.com",timeouttest=2):

The file kb_log:

import urllib2
import time
import socket
import smtplib


from django.core.mail import send_mail


message = {404:"The Page not found possibly a missing",
            "slow":"The site is very slow",
            502:"502 Gateway either I restarted the server or it's not responding",
            504:"Server is down :("}
# url = "http://www.kenyabuzz.com"
address = "[email protected]"
# timeoutest = 60 #seconds 


def test_speed(url="http://www.kenyabuzz.com",timeouttest=2):     
    try:
        begin_time = time.time()
        requesttest = urllib2.urlopen(url,None,timeouttest)
        requesttest.read()
        return "Site is okay with a response time of {0}".format(time.time() - begin_time)
    except socket.timeout:
        return message["slow"]   
    except urllib2.HTTPError, e:
        return message[e.code]

def send_email():
    result = test_speed()
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.starttls()
    server.login("[email protected]", "xdjmfppkffwmiwvh")
    server.sendmail("[email protected]", ['[email protected]','[email protected]'], result)
    server.quit()

if __name__ == '__main__':    
    send_email()

I think the issue might be with the way it gets called.

Upvotes: 3

Views: 424

Answers (2)

Mariusz
Mariusz

Reputation: 369

Could you tell, how are you create rule? Your rule should look like :

0 * * * * /usr/bin/python /path/to/your/script.py

Upvotes: 1

Chad Miller
Chad Miller

Reputation: 1475

You're running that as a shell script. "import" is a program.

Add "#!/usr/bin/python2" to the top of your program. The OS will interpet that and start python to interpret your program.

Upvotes: 3

Related Questions