当我尝试使用cronjob调用python函数时,它会返回错误。
当我从python shell运行它时,该函数可以正常工作
这是错误:
: 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):
文件 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 = "hrrsnkimani103@gmail.com"
# 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("muiruri.samuel@gmail.com", "xdjmfppkffwmiwvh")
server.sendmail("muiruri.samuel@gmail.com", ['muiruri.samuel@gmail.com','hrrsnkimani103@gmail.com'], result)
server.quit()
if __name__ == '__main__':
send_email()
我认为这个问题可能与它被调用的方式有关。
答案 0 :(得分:3)
您将其作为shell脚本运行。 "进口"是一个程序。
添加"#!/ usr / bin / python2"到你的程序的顶部。操作系统会插入并启动python来解释你的程序。
答案 1 :(得分:1)
你能说出来,你是如何制定规则的? 您的规则应如下所示:
0 * * * * / usr / bin / python /path/to/your/script.py