问题描述
我写了一个 PythonScript,它每分钟都从 RTSP-Stream 生成一个快照。它运行良好,但 24-30 小时后,它会冻结。
所以我写了一个 Shell-Script 来检查定义的文件夹中的图片数量是否增长。但是,我再次使用睡眠。我想那并不理想,但我不知道如何使用 crontab 来实现它。
有没有人有更好的方法来控制我的 python 脚本。下周我放假了,我需要一种方法让我的 python 脚本不会因为 weej 而冻结。或者如果它冻结,我的 Shellscript 应该杀死并重新启动它。
watcher.sh
#!/bin/bash
cd /home/pi/Pictures/TimeLapse
clear
/usr/bin/python3 /home/pi/mu_code/RTSPCapture/captureIt.py > /dev/null 2>&1
while true
do
before=$(ls -l | wc -l)
sleep 60
after=$(ls -l | wc -l)
echo "Before: $before"
echo "After : $after"
if (("$before" < "$after"))
then
echo 'Ok'
else
echo 'Panic'
kill -9 $(ps -aux | grep "captureIt.py" | grep -v grep | awk '{print $2}')
/usr/bin/python3 /home/pi/mu_code/RTSPCapture/captureIt.py > /dev/null 2>&1
fi
done
captureIt.py
from timeit import default_timer as timer
import time,os,re,vlc
def waitXsec(second,executionTime,shiftTime):
time.sleep(second - (executionTime + shiftTime))
def getPicture(imgDir):
newName = ''
for entry in os.scandir(imgDir):
if entry.is_file():
newName = entry.name
output = re.search(r'(\d{5})\.\w*',str(newName))
if output.group(1) is not None:
val = int(output.group(1))
else:
val = 0
return val + 1
def captureIt():
shift = 0.0015
interval = 60
imgNr = getPicture('/home/pi/Pictures/TimeLapse')
stream = 'rtsp://192.168.1.118/live/ch00_1'
os.environ['VLC_VERBOSE'] = str('-1')
player = vlc.MediaPlayer(stream)
player.play()
time.sleep(10)
while True:
start = timer()
player.video_take_snapshot(0,'/home/pi/Pictures/TimeLapse/' + str(imgNr).zfill(5) + '.jpg',0)
imgNr += 1
end = timer()
waitXsec(interval,(end - start),shift)
def main():
captureIt()
if __name__ == '__main__':
main()
解决方法
您可以稍微翻转一下问题,而不是创建一个无限循环的 python 脚本,而是创建一个每分钟运行一次并拍摄快照的 cronjob。这样,您将不会有一个可能挂起的长时间运行的进程。首先更新你的程序,只拍一张快照,然后运行
highest = max(sum(num % denom == 0 for denom in a) for num in set(a))
在打开的编辑器中添加:
crontab -e
并且 crontab 将每分钟运行您的脚本,该脚本恰好拍摄一个快照,然后死亡。