0

i want to run python script as a service. for that i followed instructions here.

for init script(myservice.sh), i copied as it is.

for myservice.py ,

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,18736))

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(str(data))
   file.close()

When i start service "sudo /etc/init.d/myservice.sh start". it successfully started.

when i send udp data, but nothing is happend to "output.txt". what is the problem here?

Biruntha G
  • 27
  • 2
  • 10

1 Answers1

0

This process is formally referred to as daemonizing a Python script.

I am going to assume both your init script and code are working correctly, likely it is an issue there.

However, aside from this problem, use the logger class when daemonizing Python scripts. There are too many issues in attempting to implement logging in such a crude way yourself for a background process.

This is the same in the example link you provided, take a look here for why: Maintaining Logging and/or stdout/stderr in Python Daemon

Community
  • 1
  • 1
  • @ AAA i'm running this script in raspberry pi. i want to send this "data" to phone via SMS. for that do i need logger class ? what is the purpose of that class – Biruntha G Nov 22 '15 at 05:20
  • You are currently writing your data to a text file called output.txt. I assumed you wanted to log the data you received. The reason it works manually and not when running it as a service is because a lot more factors are in play such as working directories, file permissions, the service running under a specific user, other environmental variables, etc. The logger class takes care of all this when logging your output. –  Nov 22 '15 at 05:41