Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7820209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:12:23+00:00 2026-06-02T07:12:23+00:00

I’m currently trying to manually create a simple daemon process, I don’t want to

  • 0

I’m currently trying to manually create a simple daemon process, I don’t want to use the existing externals libraries to avoid overhead.

I’m currently checking when my process runs that it doesn’t have a PID file already created (meaning it’s running), like described in this post.

I also have a daemonizing module to detach the PID from current process and redirect stdout and stderr (so my daemon will keep running even if I end my session):

import os
import sys

def daemonize(stdin="/dev/null", stdout="/dev/null", stderr="/dev/null"):
    try: 
        pid = os.fork() 
        if pid > 0:
            sys.exit(0)
    except OSError, e: 
        sys.stderr.write ("fork #1 failed: (%d) %s\n" % (e.errno, e.strerror) )
        sys.exit(1)

    os.chdir("/") 
    os.umask(0) 
    os.setsid() 

    try: 
        pid = os.fork() 
        if pid > 0:
            sys.exit(0)
    except OSError, e: 
        sys.stderr.write ("fork #2 failed: (%d) %s\n" % (e.errno, e.strerror) )
        sys.exit(1)

    stdin_par = os.path.dirname(stdin)
    stdout_par = os.path.dirname(stdout)
    stderr_par = os.path.dirname(stderr)
    if not stdin_par:
        os.path.makedirs(stdin_par)
    if not stdout_par:
        os.path.makedirs(stdout_par)
    if not stderr_par:
        os.path.makedirs(stderr_par)

    si = open(stdin, 'r')
    so = open(stdout, 'a+')
    se = open(stderr, 'a+', 0)
    os.dup2(si.fileno(), sys.stdin.fileno())
    os.dup2(so.fileno(), sys.stdout.fileno())
    os.dup2(se.fileno(), sys.stderr.fileno())

So currently I can run my process like sample line below and it will run my daemon correctly:

>$ python myapp.py

But to stop it, I have to grep the PID (or take it from the lock file) and manually erase the PID after:

>$ ps -ef | grep myapp
xxxxx  11901     1  0 19:48 ?        00:00:00 python src/myapp.py
xxxxx  12282  7600  0 19:54 pts/7    00:00:00 grep myapp
>$ kill -9 11901
>$ rm -rf /path/to/lock.pid

I’d like to have a more Unix-like daemon where I can manage the daemon lifecycle with the following commands:

>$ python myapp.py start
>$ python myapp.py stop
>$ python myapp.py restart

I can certainly do it with the argparse module, but that seems a bit tedious and ugly.

Do you know a simple and elegant solution to have a Unix-style daemon process in Python?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T07:12:29+00:00Added an answer on June 2, 2026 at 7:12 am

    After looking a bit more I found a great example that does just this here.

    It uses a generic Daemon class, which can be subclassed afterwards:

    #!/usr/bin/env python
    
    import sys, os, time, atexit
    from signal import SIGTERM
    
    class Daemon:
            """
            A generic daemon class.
    
            Usage: subclass the Daemon class and override the run() method
            """
            def __init__(self, pidfile, stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'):
                    self.stdin = stdin
                    self.stdout = stdout
                    self.stderr = stderr
                    self.pidfile = pidfile
    
            def daemonize(self):
                    """
                    do the UNIX double-fork magic, see Stevens' "Advanced
                    Programming in the UNIX Environment" for details (ISBN 0201563177)
                    http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16
                    """
                    try:
                            pid = os.fork()
                            if pid > 0:
                                    # exit first parent
                                    sys.exit(0)
                    except OSError, e:
                            sys.stderr.write("fork #1 failed: %d (%s)\n" % (e.errno, e.strerror))
                            sys.exit(1)
    
                    # decouple from parent environment
                    os.chdir("/")
                    os.setsid()
                    os.umask(0)
    
                    # do second fork
                    try:
                            pid = os.fork()
                            if pid > 0:
                                    # exit from second parent
                                    sys.exit(0)
                    except OSError, e:
                            sys.stderr.write("fork #2 failed: %d (%s)\n" % (e.errno, e.strerror))
                            sys.exit(1)
    
                    # redirect standard file descriptors
                    sys.stdout.flush()
                    sys.stderr.flush()
                    si = file(self.stdin, 'r')
                    so = file(self.stdout, 'a+')
                    se = file(self.stderr, 'a+', 0)
                    os.dup2(si.fileno(), sys.stdin.fileno())
                    os.dup2(so.fileno(), sys.stdout.fileno())
                    os.dup2(se.fileno(), sys.stderr.fileno())
    
                    # write pidfile
                    atexit.register(self.delpid)
                    pid = str(os.getpid())
                    file(self.pidfile,'w+').write("%s\n" % pid)
    
            def delpid(self):
                    os.remove(self.pidfile)
    
            def start(self):
                    """
                    Start the daemon
                    """
                    # Check for a pidfile to see if the daemon already runs
                    try:
                            pf = file(self.pidfile,'r')
                            pid = int(pf.read().strip())
                            pf.close()
                    except IOError:
                            pid = None
    
                    if pid:
                            message = "pidfile %s already exist. Daemon already running?\n"
                            sys.stderr.write(message % self.pidfile)
                            sys.exit(1)
    
                    # Start the daemon
                    self.daemonize()
                    self.run()
    
            def stop(self):
                    """
                    Stop the daemon
                    """
                    # Get the pid from the pidfile
                    try:
                            pf = file(self.pidfile,'r')
                            pid = int(pf.read().strip())
                            pf.close()
                    except IOError:
                            pid = None
    
                    if not pid:
                            message = "pidfile %s does not exist. Daemon not running?\n"
                            sys.stderr.write(message % self.pidfile)
                            return # not an error in a restart
    
                    # Try killing the daemon process       
                    try:
                            while 1:
                                    os.kill(pid, SIGTERM)
                                    time.sleep(0.1)
                    except OSError, err:
                            err = str(err)
                            if err.find("No such process") > 0:
                                    if os.path.exists(self.pidfile):
                                            os.remove(self.pidfile)
                            else:
                                    print str(err)
                                    sys.exit(1)
    
            def restart(self):
                    """
                    Restart the daemon
                    """
                    self.stop()
                    self.start()
    
            def run(self):
                    """
                    You should override this method when you subclass Daemon. It will be called after the process has been
                daemonized by start() or restart().
                    """
    

    Once you have this module, you can do the following to have all different modes of your daemon:

    #!/usr/bin/env python
    
    import sys, time
    from daemon import Daemon
    
    class MyDaemon(Daemon):
            def run(self):
                    while True:
                            time.sleep(1)
    
    if __name__ == "__main__":
            daemon = MyDaemon('/tmp/daemon-example.pid')
            if len(sys.argv) == 2:
                    if 'start' == sys.argv[1]:
                            daemon.start()
                    elif 'stop' == sys.argv[1]:
                            daemon.stop()
                    elif 'restart' == sys.argv[1]:
                            daemon.restart()
                    else:
                            print "Unknown command"
                            sys.exit(2)
                    sys.exit(0)
            else:
                    print "usage: %s start|stop|restart" % sys.argv[0]
                    sys.exit(2)
    

    Hope that helps other people who get stuck on the same problem !

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want use html5's new tag to play a wav file (currently only supported
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.