cronjob 运行正常,但没有生成输出!当我手动运行我的 python 脚本时,它会给出输出,但在 cron job 中它没有生成输出
Dec 23 15:10:01 raspberrypi CRON[3265]: (pi) CMD (/home/pi/Desktop/sensor_baloon.py >/dev/null 2>&1)
Dec 23 15:11:01 raspberrypi CRON[3334]: (pi) CMD (/home/pi/Desktop/sensor_baloon.py >/dev/null 2>&1)
Dec 23 15:12:01 raspberrypi CRON[3355]: (pi) CMD (/home/pi/Desktop/sensor_baloon.py >/dev/null 2>&1)
Dec 23 15:13:01 raspberrypi CRON[3482]: (pi) CMD (/home/pi/Desktop/sensor_baloon.py >/dev/null 2>&1)
Dec 23 15:14:01 raspberrypi CRON[3569]: (pi) CMD (/home/pi/Desktop/sensor_baloon.py >/dev/null 2>&1)
这是我的工作代码,没有任何错误..!
import RPi.GPIO as GPIO
import time,os
import datetime
TRIG = 24
ECHO = 23
ALARM = 17
# Speed of sound in cm/s at temperature
temperature = 20
speedSound = 34326 + (0.6*temperature)
print("Ultrasonic Measurement")
print("Speed of sound is",speedSound/100,"m/s at ",temperature,"deg")
GPIO.setmode(GPIO.BCM)
GPIO.setup(TRIG,GPIO.OUT)
GPIO.setup(ECHO,GPIO.IN)
GPIO.output(TRIG, False)
GPIO.setup(ALARM,GPIO.OUT)
GPIO.output(ALARM, True)
print ("Waiting For Sensor To Settle")
time.sleep(1) #settling time
def get_distance():
dist_add = 0
k=0
for x in range(30):
try:
GPIO.output(TRIG, True)
time.sleep(0.00001)
GPIO.output(TRIG, False)
while GPIO.input(ECHO)==0:
pulse_start = time.time()
while GPIO.input(ECHO)==1:
pulse_end = time.time()
pulse_duration = pulse_end - pulse_start
distance = pulse_duration * speedSound
distance = round(distance, 1)
distance = distance/2
print (x, "distance: ", distance)
if(distance > 125):# ignore erroneous readings (max distance cannot be more than 125)
k=k+1
continue
dist_add = dist_add + distance
#print "dist_add: ", dist_add
time.sleep(.1) # 100ms interval between readings
except Exception as e:
pass
print ("x: ", x+1)
print ("k: ", k)
avg_dist=dist_add/(x+1 -k)
dist=round(avg_dist,2)
#print ("dist: ", dist)
return dist
def sendData_to_remoteServer(url,dist):
url_full=url+str(dist)
urlopen(url_full)
print("sent to url: ",url_full)
def low_level_warning(dist):
level=114-dist
if(level<40):
print("level low : ", level)
GPIO.output(ALARM, False)
else:
GPIO.output(ALARM, True)
print("level ok")
from urllib.request import urlopen
#local web server is running
url_remote="http://localhost/web_host/watertank/insert_data_baloon.php?level="
distance=get_distance()
print ("distance: ", distance ,"cm")
sendData_to_remoteServer(url_remote,distance)
low_level_warning(distance)
print ("---------------------")
答案1
从 crontab 运行的作业的输出不会显示在屏幕上。为了查看 cron 作业的输出,您需要将其捕获到文件中。您可以通过添加
>> /home/myuser/myscript.log 2>&1
在您的 crontab 中的命令之后(当然,请替换/home/myuser/myscript.log
为您要使用的实际日志文件)。它将2>&1
错误输出重定向到接收标准输出的第一个文件描述符,因此它将包含在文件中
传统上,此类输出将使用本地邮件守护程序邮寄给作业的所有者。