Python 使用systemctl复制文件

Python 使用systemctl复制文件,python,embedded-linux,systemctl,Python,Embedded Linux,Systemctl,我为嵌入式linux创建python脚本,该脚本监视USB存储的插入和删除,如果插入USB驱动器,则计算机会将文件复制到USB驱动器。该脚本需要在引导后启动,因此我在我的系统ctl中创建服务这里是myscript.service: [Unit] Description=My Script Service After=multi-user.target [Service] Type=idle ExecStart=/usr/bin/python2 /home/kiki/PycharmProjects

我为嵌入式linux创建python脚本,该脚本监视USB存储的插入和删除,如果插入USB驱动器,则计算机会将文件复制到USB驱动器。该脚本需要在引导后启动,因此我在我的系统ctl中创建服务这里是myscript.service:

[Unit]
Description=My Script Service
After=multi-user.target
[Service]
Type=idle
ExecStart=/usr/bin/python2 /home/kiki/PycharmProjects/copyfiletousb  /copyfile.py
[Install]
WantedBy=multi-user.target
这是我的python文件(copyfile.py)


我的问题是数据没有传输到USB存储器

,而且。。。你有什么问题?对不起,我忘了提我已经编辑了这个问题,
journalctl-e myscript
包含什么?有例外吗?调试消息?当您直接在控制台中运行脚本时,它工作吗?当我键入:python copyfile.pyjournalctl-e不包含任何内容时,脚本工作
from  __future__ import division
import pyudev
import serial
import time
from shutil import copyfile
import os.path
ser=serial.Serial('/dev/ttyUSB0')
ser.baudrate=19200
ser.write(bytearray(chr(27)))
ser.write(bytearray("                Standby"))
def copyfile(source, dest, buffer_size=1024 * 1024):
    if os.path.exists(dest):
        print "File %s already exists" % dest
        return

    if not os.path.exists(source):
        print "File %s doesnt exist" % source
        return

    if not hasattr(source, 'read'):
        source = open(source, 'rb')
    if not hasattr(dest, 'write'):
        dest = open(dest, 'wb')

    print "Start copy..."
    size=os.path.getsize('fedora.iso')
    size=size//buffer_size
    q=0
    old_propgress=0
    while 1:
        q=q+1
        copy_buffer = source.read(buffer_size)
        progress=((q*100.0)//size)

        if old_propgress != progress :
         ser.write(bytearray(chr(27)))
         ser.write(bytearray(" Copying         file :   \       "+str(int(progress))+" %"))
    old_propgress=progress

        if (progress==100):
            ser.write(bytearray(chr(27)))
            ser.write(bytearray("                  Done"))
        if copy_buffer:
            try:
              dest.write(copy_buffer)
            except:
              ser.write(bytearray(chr(27)))
              ser.write(bytearray("   Ruang        Tidak          Cukup"))
             break
        else:
            break
    print (q)
    source.close()
    dest.close()
    print "Finished!"


context = pyudev.Context()
monitor = pyudev.Monitor.from_netlink(context)
monitor.filter_by(subsystem='usb')
insert=0
remove=0
for device in iter(monitor.poll, None):


    if device.action == 'add' and insert==0:
        ser.write(bytearray(chr(27)))
        ser.write(bytearray("                USB Terh        ubung"))
        time.sleep(5)
        copyfile("fedora.iso", "/media/kiki/USBSTORAGENAME/vessel.iso")
        insert=1
        remove=0
        print(device.sequence_number)




    if device.action== 'remove' and remove==0:
        insert = 0
        remove = 1
        ser.write(bytearray(chr(27)))
        ser.write(bytearray('                USB terp        utus'))
        time.sleep(2)
        ser.write(bytearray(chr(27)))
        ser.write(bytearray('                standby'))