簡體   English   中英

Python 腳本在一段時間后停止在 crontab 中運行

[英]Python script stops running in crontab after sometime

我有一個運行的代碼,我在 Raspberry pi 啟動時在 crontab 中運行(代碼在 python“空閑”中運行良好)。 它有一個無限的 while 循環。 一段時間后,python 腳本停止運行。 我在網上搜索並得到以下可能導致它們的答案

  1. https://serverfault.com/questions/399029/python-script-stops-responding-when-run-for-a-long-time
  2. http://www.solutionoferror.com/python/cron-jobbin39-a-python-script-stops-halfway-through-20824.asp (沒有回復這個)
  3. Cron jobbin'一個python腳本:中途停止

在這一點上,我真的很困惑背后的原因是什么。 我需要幫助來理解這背后的原因。 以及在樹莓派啟動時運行此代碼的最佳方法是什么。 crontab 是否需要任何調整或是否有其他方法? 請詳細回復,因為我是EE專業,對linux、樹莓派(因為我剛剛開始使用它)和python不是很熟悉。 代碼如下:

import threading
import os
import time
import serial
import httplib
import math

#GPS data
os.chdir('/home/pi/Desktop')

#Distance function
def Distance(lat1, long1,lat2,long2):

    degree_to_rad = float(math.pi / 180.0)

    d_lat = (lat2 - lat1) * degree_to_rad
    d_long = (long2 - long1) * degree_to_rad

    a = pow(math.sin(d_lat / 2), 2) + math.cos(lat1 * degree_to_rad) * math.cos(lat2 * degree_to_rad) * pow(math.sin(d_long / 2), 2)
    c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
    km = 6367 * c
    mi = 3956 * c

    return km

#global variables
global ser
global recieveBuffer
global recieveFlag
global webAddress
global data

#Serial Communication Settings
baud = 9600;
#port = 'Com4'
port ='/dev/ttyAMA0'

#default WebAddress and Data
webAddress = "gpsmaster.eu5.org"
webData = "/post.php?"

#Lat and Lng Coordinates, Initialied at Gate
lat=31.470997
lng=74.411116
lumsLat=31.470997
lumsLng=74.411116


ser = serial.Serial(port, baud)
if not ser.isOpen():
    ser.open()


recieveBuffer="NULL"
recieveFlag=0

def shutDown():
    os.system("sudo shutdown -h now")

def readSerial():
    global recieveBuffer
    global recieveFlag
    global ser
    while True:
        recieveBuffer = ser.readline()
        recieveFlag=1

# Function to Post Data, Return Y for success, N for failure
def httpGet(webAddress,webData):
    try:
        #print(webAddress)
        #print(webData)
        conn = httplib.HTTPConnection(webAddress)
        conn.request("GET",webData)
        res = conn.getresponse()
        #print res.status, res.reason
        conn.close()
    except:
        print("Exceptoion!!")
        return ['N','Failed']
    else:
        return ['Y',res]


def replayMaster(ser,recieved):
    global lat
    global lng
    recieved=recieved.replace("\n", "")
    recieved=recieved.replace("\r", "")
    tokenized = recieved.split(',');
    command = tokenized[0]
    if command=='AT':
        ser.write('<OK>')
    elif command=='POST':
        if lat!=0 and lng != 0: # and Distance(lumsLat,lumsLng,lat,lng)<50
            lat = float(tokenized[1])
            lng = float(tokenized[2])
            ans = httpGet(webAddress,"%slat=%f&lng=%f" % (webData, lat,lng))
            #with open("logFile.txt","a") as fileStream:
             #   fileStream.write("%s,%f,%f\r\n" % (ans[0],lat,lng))
            #fileStream.close()
            if ans[0]=='N':
                ser.write('<ERROR>')
            else:
                ser.write('<'+`ans[1].status`+'>')
        else:
            ser.write('<Invalid Coordinates>')
            print ("Invalid Coordinates")
    elif command=='CLOSE':
        ser.close()
    elif command=='HALT':
        ser.write('<Shutting Down>');
        shutDown()
    else:
        ser.write('<Unknown Command>')


serialReadThread = threading.Thread(target=readSerial)
serialReadThread.start()


ser.write('<OK>')
while True:
    #p#rint Distance(31.470997,74.411116,31.469723,74.512244)
    if recieveFlag:
        replayMaster(ser,recieveBuffer)
        print(recieveBuffer)
        recieveFlag=0
    print("waiting: |%f|,|%f|"%(lat,lng))
    time.sleep(0.4)

我的 crontab 也有一些問題。 我在重新啟動后使用短時間延遲修復了它。

@reboot (sleep 10; python3 ABSOLUTE_PATH/example.py)

也許這會有所幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM