GithubHelp home page GithubHelp logo

python-daemon's Introduction

Build Status

Python daemonizer class

This is a Python class that will daemonize your Python script so it can continue running in the background. It works on Unix, Linux and OS X, creates a PID file and has standard commands (start, stop, restart) + a foreground mode.

Based on this original version from jejik.com.

Usage

Define a class which inherits from Daemon and has a run() method (which is what will be called once the daemonization is completed.

from daemon import Daemon

class pantalaimon(Daemon):
	def run(self):
		# Do stuff

Create a new object of your class, specifying where you want your PID file to exist:

pineMarten = pantalaimon('/path/to/pid.pid')
pineMarten.start()

Actions

  • start() - starts the daemon (creates PID and daemonizes).
  • stop() - stops the daemon (stops the child process and removes the PID).
  • restart() - does stop() then start().

Foreground

This is useful for debugging because you can start the code without making it a daemon. The running script then depends on the open shell like any normal Python script.

To do this, just call the run() method directly.

pineMarten.run()

Continuous execution

The run() method will be executed just once so if you want the daemon to be doing stuff continuously you may wish to use the sched module to execute code repeatedly (example).

python-daemon's People

Contributors

carlosperello avatar davidmytton avatar dimier avatar jtwaleson avatar observerss avatar reduxionist avatar reidransom avatar waawal avatar wwwjfy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

python-daemon's Issues

Change license to something software-compatible

Hi, I noticed that you're using the CC BY-SA 3.0 license here, which according to the Creative Commons website, is only compatible with other CC licenses.

Currently, no non-CC licenses have been designated as compatible with BY-SA 3.0.

The problem with CC licenses is that they aren't really intended for code, so it's unclear whether the project requires derivatives to be open source, for example. It would be great if you could change it to a more compatible license.

Calling Daemon.start() immediately terminates the main process.

Example:

    class ProxyD(Daemon):

        def run(self):
           ... do something ...

    singletonProxyP = ProxyD(expanduser("~"))
    singletonProxyP.start()

    print('finished')

execute the above code will yield the following result:

Starting...

Process finished with exit code 0

the last line was ignored and the daemon process is not created (failed silently), not good :-<

The above behaviour was observed on Ubuntu 16.04 with Python 2.7.12

Odd imports from pypi package

Hi,

The current version published on Pypi users odd imports due to the lack of proper imports on the package's __init__.py file or misconfigured setup.py

from py_daemon.py_daemon import Daemon

I also don't see any release tags on this repository. Is this still being maintained?

ModuleNotFoundError: No module named 'daemon'

Hello:

After installing the py_daemon from pip using

pip install py_daemon
or
pip install git+https://github.com/serverdensity/python-daemon.git

into an python3 virtualenv

I'm unable to load the module

it throws

Traceback (most recent call last):
  File "main.py", line 21, in <module>
    from daemon import Daemon
ModuleNotFoundError: No module named 'daemon'

maybe I'm doing something wrong

test

I have the same issue, when I delete a map, nagvis delete the map but not entry in var/maplist-full-global.cfg-1.9b10-cache.

When I change maplist content to : a:0:{} map is deleting from sidebar

Server not stopped

Hello!
Thank you for the script.
However I have an issue when trying to stop the daemon.
The script is locked in

while 1:
    os.kill(pid, signal.SIGTERM)
    time.sleep(0.1)

I think the error comes from this commit from @dimier
1bf68d4

Does that mean that I have to check for daemon_alive in my main loop to properly stop the daemon?
Thank you for your kind reply!

Close all file on restart

Please, add a routine to close all open files like socket etc. See like the daomon.py:

def close_all_open_files(exclude=None):
    """ Close all open file descriptors.

        :param exclude: Collection of file descriptors to skip when closing
            files.
        :return: ``None``.

        Closes every file descriptor (if open) of this process. If
        specified, `exclude` is a set of file descriptors to *not*
        close.

        """
    if exclude is None:
        exclude = set()
    maxfd = get_maximum_file_descriptors()

    for fd in reversed(range(maxfd)):
        if fd not in exclude:
            close_file_descriptor_if_open(fd)

If don't close this files, the socket return Address already in use and other problems on restart

Shouldn't signal registration be outside sigtermhandler?

def sigtermhandler(signum, frame):
    self.daemon_alive = False
    signal.signal(signal.SIGTERM, sigtermhandler)
    signal.signal(signal.SIGINT, sigtermhandler)

Shouldn't both signal.signal() calls be outside the handler? Unless I'm misunderstanding something, this is attempting to register the handler method within the handler itself.

detach_process_context inside daemon.py hangs

I am creating a context with just the pidfile argument as,
daemon.DaemonContext(pidfile="/home/user/pidfile")
And when i do context.open, the parent process dies as expected but the detached/forked child process is not getting executed as well.

I put more logs inside daemon.py and i figured out detach_process_context is causing the problem.

def detach_process_context():
   def fork_then_exit_parent(error_message):
        try:
            pid = os.fork()
            if pid > 0:
                #the control comes here
                os._exit(0)
            #the child process control never comes here

i am using python-daemon (2.0.5).

Restart inside the daemon called triple

Hello,

i want restart the daemon inside the python like:

subprocess.call(["python", "/mnt/mtd/master/test.py", "restart"])

but then the daemon start triple. If I restart from the terminal all works fine

Create a 'status' action

In addition to supporting start|stop|restart, most init.d scripts support a 'status' action which you can call to see if the daemon is running. Typically when this is run, it will return a 0 if the pid is running, or a 1 otherwise.

OS Errno9 due to os.closerange

With Python 3.10, python-daemon generates OS Errno 9 and maybe some other errors and doesn't leave many clues. Part of the problem is these errors are generated whilst forking and closing FDs which can lead to confusion.

Root caused this to the os.closerange(range[0], range[1]) generating a [Errno 9] Bad file descriptor range: (5,524288)

The 524288 seemed like a rather large number?

But, found this: http://0pointer.net/blog/file-descriptor-limits.html So apparently 524288 is legit.

Best guess is Python's os.closerange function doesn't know about the increased file descriptor range?? Probably a Python problem.

restart on a crash?

what is the expected design pattern for implementing restarts on crashing?

from the documentation, it isn't apparent how to handle errors

 from daemon import Daemon
 class App(Daemon):
   def run(self):     
     try:
       foo()
     except:
       print 'crashing, so restart ?'
       daemon_runner.run()

 daemon_runner = App('/tmp/app.pid')
 daemon_runner.run()

or are restarts supposed to be handled by another process/module?

if restarts are indeed handled, an example would be great.

thanks

'Started' message wont output

    if self.verbose >= 1:
            print "Started"

the output of print in above code in daemonize(self) method wont be seen at all, since stdout had been dup2-ed already. ( on Linux )

So user can not tell if process is on or not only checking it by ps

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.