我写了一个pythonscript,它每分钟都从一个RTSP流生成一个快照。它工作得很好,但24-30小时后就会结冰
因此,我编写了一个Shell脚本来检查定义的文件夹中的图片数量是否增长。不过,我还是用睡眠。我想这并不理想,但我不知道如何使用crontab
有人有更好的方法来控制我的python脚本吗。下周我有假期,我需要一种方法,使我的python脚本在一周内不会冻结。或者如果它冻结,我的shell脚本应该杀死并重新启动它
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()
您可以稍微改变一下这个问题,而不是创建一个带有无限循环的python脚本,而是创建一个每分钟运行一次并拍摄快照的cronjob。这样,您就不会有一个可能挂起的长时间运行的进程。首先更新程序,使其仅拍摄一个快照,然后运行
在打开的编辑器中,只需添加:
crontab将每分钟运行一次脚本,它只会捕获一个快照,然后消失
相关问题 更多 >
编程相关推荐