Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python3 create Daemon

I tried to create a Daemon in Python to run on an Ubuntu Server. The code below is the code I have problem with.

import sys
import time
import threading
import logging
import logging.handlers

from daemon import runner

class Main(object):
    def run(self):
        my_logger = logging.getLogger('NameGeneratorDeamon')
        my_logger.setLevel(logging.DEBUG)
        handler = logging.handlers.SysLogHandler(address=('192.168.0.69', 514),facility=LOG_DAEMON)
        my_logger.addHandler(handler)
        try:
            my_logger.info('Started')
            while True:
                pass
        except Exception as inst:
            #Send error to syslog server
            my_logger.critical(inst)

class App():
    def __init__(self):
        self.stdin_path = '/dev/null'
        self.stdout_path = '/dev/null'
        self.stderr_path = '/dev/null'
        self.pidfile_path =  '/tmp/foo.pid'
        self.pidfile_timeout = 5
    def run(self):
        service = Main()
        service.run()

app = App()
daemon_runner = runner.DaemonRunner(app)
daemon_runner.do_action()

The error message I got when I run the code is the following:

   File "Main.py", line 35, in <module>
     daemon_runner = runner.DaemonRunner(app)
   File "/usr/local/lib/python3.4/dist-packages/daemon/runner.py", line 111, in __init__
     self.daemon_context.stdout = open(app.stdout_path, 'w+t')
io.UnsupportedOperation: File or stream is not seekable.

Does anyone know how to fix this, or do you have a better way to create a Daemon in Python?

like image 698
Emil Rowland Avatar asked May 30 '16 19:05

Emil Rowland


People also ask

How do I start a daemon program in Python?

daemon.start() To run a program as a daemon, you must define a main function which will run in background. The daemonize function takes 3 arguments: the application name, the process ID (defined in the header) and the method to execute. After making those changes, you can run it normally as an usual python script.

What are daemons in Python?

This article will describe what daemons do, how to create them in Python, and what you can use them for. A daemon is a process on UNIX that runs in the background and will typically keep running until it is explicitly told to stop.

How to convert a python script into daemon mode?

YapDi is a python package. It can be used to convert a python script into daemon mode from inside the script. Show activity on this post. since python-daemon has not yet supported python 3.x, and from what can be read on the mailing list, it may never will, i have written a new implementation of PEP 3143: pep3143daemon

What is daemon thread in Python?

The Daemon Thread does not block the main thread from exiting and continues to run in the background. This article is based on threading in python, here we discuss daemon thread with examples.


1 Answers

In my case this works with no errors:

# vi /usr/local/lib/python3.5/dist-packages/daemon/runner.py 
# 118 -120
        self.daemon_context = DaemonContext()
        self.daemon_context.stdin = open(app.stdin_path, 'wb+',buffering=0)
        self.daemon_context.stdout = open(app.stdout_path,  'wb+',buffering=0)
        self.daemon_context.stderr = open(
                app.stderr_path, 'wb+', buffering=0)
like image 152
user2106796 Avatar answered Oct 20 '22 23:10

user2106796