thesharp / daemonize

daemonize is a library for writing system daemons in Python.
MIT License
445 stars 64 forks source link

Daemonized script doesn't seem to be running #71

Closed zacharyabresch closed 5 years ago

zacharyabresch commented 5 years ago

So, I've tried a few times to get this to work to no avail. Does this work?? Here's the script I'm running ... pretty much based off the example provided. (contents of spade/__main__.py)

System Details

import logging
from sys import argv
from daemonize import Daemonize

PID = "/tmp/spade.pid"
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
logger.propagate = False
fh = logging.FileHandler("/tmp/test.log", "w")
fh.setLevel(logging.DEBUG)
logger.addHandler(fh)
keep_fds = [fh.stream.fileno()]

def main():
    logger.debug("daemonized")

if __name__ == "__main__":
    daemon = Daemonize(app="spade", pid=PID, action=main, keep_fds=keep_fds)
    if argv[1] == "start":
        daemon.start()
    if argv[1] == "stop":
        daemon.exit()

I run this like so: python -u spade/ start (to stop run the same but replace start with stop)

The script runs without error but does nothing. Can't find it in process list. No pid file is created. The log file is created but contains nothing. Start & stop seem to do nothing as well. I've scanned my filesystem for results ... nothing. Perhaps I'm missing something but it seems like daemonizing Python scripts is a lost art because all the libraries I've tried just don't seem to work.

knsd commented 5 years ago

argv[0] in your example contains string spade/, try argv[1]. Also Daemonize.exit method doesn't stop existing instance, it's internally used and just removes pid file before exit current process.

zacharyabresch commented 5 years ago

Ha, I had updated my post. I'm still getting used to Python and totally forgot argv[0] is the script name. I ended up using some code I found to make my own Daemon class. Thanks!