Biruntha
Posts: 9
Joined: Sun Oct 18, 2015 5:34 am
Location: Jaffna, Srilanka
Contact: Website

continuouslly run python script

Sun Nov 15, 2015 10:58 am

hi,

I want to run python script continuouslly all the time. for that i add a line in sudo nano /etc/rc.local (before the exit 0 line) with
python /usr/bin/python /home/pi/udpsendblock.py &.

here udpsendblock.py contain

Code: Select all

import sys, struct
from socket import *

SIZE = 1024      # packet size


hostName = gethostbyname('0.0.0.0')

mySocket  = socket( AF_INET, SOCK_DGRAM )
mySocket.bind((hostName,18728))

repeat = True
while repeat:
   (data,addr) = mySocket.recvfrom(SIZE)
   data = struct.unpack('d',data)
   data=int(data[0])
   file = open("output.txt", "w")
   file.write(data)
   file.close()
i run this script in raspberry pi.
i get udp data and then i want to print udp data all the time when i send without manually run udpsendblock.py.
but it print only one time. how can i do this?

sprinkmeier
Posts: 410
Joined: Mon Feb 04, 2013 10:48 am
Contact: Website

Re: continuouslly run python script

Mon Nov 16, 2015 11:28 am

any error message?

open(...,"w") will keep overwriting the same file. open(...,"wa") would append which might be more useful for logging.
If you really only want the latest data then consider writing to /dev/shm/ (a memory filesystem)

BTW,

Code: Select all

    with ("output.txt", "w") as w:
        w.write(data)
will close/flush automagically.

Biruntha
Posts: 9
Joined: Sun Oct 18, 2015 5:34 am
Location: Jaffna, Srilanka
Contact: Website

Re: continuouslly run python script

Mon Nov 16, 2015 12:55 pm

there are no error message. it print only one time. while 2nd udp data receive it will not print. I want to print whenever i get udp data.

Return to “Beginners”