尝试调用 python 函数时出现 Cronjob 错误
Cronjob error when trying to call python function
当我尝试使用 cronjob 调用 python 函数时,它 returns 出错了。
当我从 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()
我认为问题可能出在调用方式上。
你 运行 作为 shell 脚本。 "import" 是一个程序。
将“#!/usr/bin/python2”添加到程序的顶部。 OS 将解释它并开始 python 来解释您的程序。
你能告诉我,你是如何创建规则的吗?
您的规则应如下所示:
0 * * * * /usr/bin/python /path/to/your/script.py
当我尝试使用 cronjob 调用 python 函数时,它 returns 出错了。
当我从 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()
我认为问题可能出在调用方式上。
你 运行 作为 shell 脚本。 "import" 是一个程序。
将“#!/usr/bin/python2”添加到程序的顶部。 OS 将解释它并开始 python 来解释您的程序。
你能告诉我,你是如何创建规则的吗? 您的规则应如下所示:
0 * * * * /usr/bin/python /path/to/your/script.py