gpt4 book ai didi

python - 尝试调用 python 函数时出现 Cronjob 错误

转载 作者:太空宇宙 更新时间:2023-11-03 15:05:28 25 4
gpt4 key购买 nike

当我尝试使用 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()

我认为问题可能出在它被调用的方式上。

最佳答案

您将其作为 shell 脚本运行。 “导入”是一个程序。

将“#!/usr/bin/python2”添加到程序的顶部。操作系统将对此进行解释并启动 python 来解释您的程序。

关于python - 尝试调用 python 函数时出现 Cronjob 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33261863/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com