Python脚本在以用户身份运行时完成,但在以root用户身份运行时不完成

2024-06-26 12:32:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Linux新手,正在为一个脚本而挣扎

硬件:树莓皮零W

操作系统:树莓皮操作系统Lite

主脚本listen.py正在等待按钮按下或udp命令,然后触发函数take_image_ftp():

def take_image_ftp():
global scancounter
global isTakingImage
global denominator
global counter
global BUTTON

if  isTakingImage == False:
    isTakingImage = True
    
    GPIO.remove_event_detect(BUTTON)
    shutter = 1000000 / denominator #in microseconds 
    timea = datetime.utcnow()
    
    print("taking image...")
    subprocess.call(['raspistill -o /home/pi/startup/current.jpg --raw -md 3 -awb flash -q 100 -ex spotlight --timeout 2 --nopreview --shutter ' + str(shutter) +' --ISO 100'], shell=True)

    time.sleep(1)
    print("extracting dng...")
    subprocess.check_call(['python3.7 /home/pi/PyDNG/examples/utility.py current.jpg'], shell=True)
    
    timeb = datetime.utcnow()
    print("uploading image")
    from ftplib import FTP

    ftp = FTP(host="192.168.178.34",user='**', passwd='**')
    ftp.set_pasv(True)
    ftp.debug(0) 
    counter += 1
   # print("Shutter : " + str(shutter))
    #print ("logging in to ftp server ")
    try:
        ftp.login("Medusa", "Medusa")
    except ftplib.all_errors as e:
       # print('FTP error:', e)
        sys.exc_clear()


   # print("creating directory")
    try:
        ftp.cwd("images/")
    except ftplib.all_errors as e:
    #    print('FTP error:', e)
        sys.exc_clear()

    foldername = "img" + str(counter)
    if foldername not in ftp.nlst():
     #   print("folder does not exist. creating")
        try:
            ftp.mkd(foldername)

        except ftplib.all_errors as e:
         #   print('FTP error:', e)
            sys.exc_clear()
    try:
        ftp.cwd(foldername)
    except ftplib.all_errors as e:
        #print('FTP error:', e)
        sys.exc_clear()   

    if os.path.isfile('/home/pi/startup/current.dng'):
        file = open("/home/pi/startup/current.dng", "rb")
    else: 
        file = open("/home/pi/startup/listen.py","rb")

    ftp.storbinary("STOR " + str(get_ip_address('wlan0')) + ".dng", file)
    file.close()
    ftp.quit()
    timer = threading.Timer(5, resetCounter)
    timer.start()
    #print("\n")
    print("---------SCAN SENT--------")  
    #GPIO.add_event_detect(BUTTON, GPIO.RISING, callback=button_callback, bouncetime=200)
    print timeb - timea

它通过raspistill()获取图像,然后通过PyDNG从捕获的图像中提取原始数据,然后通过ftp上传dng文件

当以用户pi的身份通过ssh运行时,它就像一个符咒一样工作,但是当以root用户身份通过autostart运行时,它在提取原始数据时会耗尽(编辑:耗尽内存)。这将导致在上载以用户身份运行pi时转换的最后一个图像

助手脚本:

import socket
import struct
import fcntl
import subprocess
import sys
import ftplib
import time
import threading
import os

from time import sleep



print("updateing scripts")
try:
   ftp = ftplib.FTP("192.168.178.34", "**", "**")
   ftp.login("**", "**")
   lf = open("/home/pi/startup/listen.py", "wb")
   ftp.retrbinary("RETR " + "listen.py", lf.write, 8*1024)
   lf.close()
   lf = open("/home/pi/startup/focus.py", "wb")
   ftp.retrbinary("RETR " + "focus.py", lf.write, 8*1024)
   lf.close()
   ftp.quit()
except ftplib.all_errors as e:
print('FTP error:', e)

print("done")

print ("starting listen script")

cmd = ("python /home/pi/startup/listen.py")
subprocess.call(cmd, shell=True)

助手脚本通过/etc/rc.local sudo python/home/pi/startup/start.py&

如果你们能给我指出正确的方向,我将非常感激


Tags: pyimporttruehomepiftpgloballisten
1条回答
网友
1楼 · 发布于 2024-06-26 12:32:45

哦,孩子。。。 以下是实际问题: 当ssh插入pi时,我将工作目录更改为文件夹/startup。 从systemd运行脚本时。。。我没有

转换('/home/pi/startup/current.jpg'),而不是转换('current.jpg')

该死的时间,伙计,该死的时间

编辑:当使用subprocess.call将原始信息从raspistill提取到dng时,raspi确实耗尽了内存-因此所有的麻烦都是值得的

感谢所有回复的人

相关问题 更多 >