Example of python service running under systemd?

Ervin Hegedüs airween at gmail.com
Thu Sep 11 17:29:21 EDT 2014


Hi Travis,

On Thu, Sep 11, 2014 at 02:06:48PM -0700, Travis Griggs wrote:
> 
> On Sep 11, 2014, at 11:18 AM, Chris “Kwpolska” Warrick <kwpolska at gmail.com> wrote:
> 
> > Depends what you want. 
> 
> Mine is not a web service. My main.py looks like this:
> 
> #!/usr/bin/env python3
> 
> import cycle
> import pushTelemetry
> from threading import Thread
> 
> def main():
>     Thread(target=pushTelemetry.udpLoop).start()
>     Thread(target=cycle.cycleLoop).start()
> 
> if __name__ == '__main__':
>     main()
> 
> It basically creates two threads, one which does some local processing and control, the other which periodically does reporting via udp packets. I use the dual threads because they both work with a shared serial port at times, so I have to synchronize access through that.
> 
> What I want is to have this startup, after my board has it’s networking layer up and running (and hopefully a valid ip address by then), and to just keep running forever

may be you think about the fork(), eg:

if __name__ == "__main__":
    ...other codes, eg. drop root privileges, ...
    ...check arguments...
    try:
      pid = os.fork()
      if pid > 0:
          #print "Daemon started (pid: %d)" % (pid)
          sys.exit(0)
    except OSError, e:
      print >>sys.stderr, "fork #1 failed: %d (%s)" % (e.errno, e.strerror)
      sys.exit(1)
    
    os.chdir("/")
    os.setsid()
    os.umask(0)
    
    # do second fork
    try:
      pid = os.fork()
      if pid > 0:
          #print "Daemon started (pid: %d)" % (pid)
          sys.exit(0)
    except OSError, e:
      print >>sys.stderr, "fork #2 failed: %d (%s)" % (e.errno, e.strerror)
      sys.exit(1)

    main()




regards,


a.

-- 
I � UTF-8



More information about the Python-list mailing list