【发布时间】:2021-05-29 03:46:22
【问题描述】:
我编写了一个 pythonscript,它使每一分钟都成为 RTSP-Stream 的快照。它运作良好,但 24-30 小时后,它会冻结。
所以我写了一个 Shell 脚本来检查定义文件夹中的图片数量是否增加。但同样,我使用睡眠。我想那并不理想,但我不知道如何使用 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, 0)
imgNr += 1
end = timer()
waitXsec(interval, (end - start), shift)
def main():
captureIt()
if __name__ == '__main__':
main()
【问题讨论】:
-
如果不每隔一段时间发出信号并等待其响应,您将无法看到“冻结”线程。手动创建一个线程来调用该函数并监控该线程。
-
目前我可以控制pythonscript是否没有被冻结,因为我检查文件夹中有多少图像。一旦值在一分钟后保持不变,pythonscript 就会被冻结,因为没有写入新图像。但我想用一个 cronjob 来完成它,以避免我的 shellscript 中的 sleep-command。
-
然后你自己解决了你的问题。你说它每 X 小时冻结一次,所以睡 Y 分钟并检查是否冻结。如果结冰了,就杀了它,如果没有结冰,就再睡。
标签: python python-3.x bash cron rtsp