Can I run a Python script as a service?

前端 未结 6 928
旧巷少年郎
旧巷少年郎 2020-12-01 10:05

Is it possible to run a Python script as a background service on a webserver? I want to do this for socket communication.

相关标签:
6条回答
  • 2020-12-01 10:09

    on XP and later you can use the sc.exe program to use any .exe as service:

    >sc create
    Creates a service entry in the registry and Service Database.
    SYNTAX:
    sc create [service name] [binPath= ] <option1> <option2>...
    CREATE OPTIONS:
    NOTE: The option name includes the equal sign.
     type= <own|share|interact|kernel|filesys|rec>
           (default = own)
     start= <boot|system|auto|demand|disabled>
           (default = demand)
     error= <normal|severe|critical|ignore>
           (default = normal)
     binPath= <BinaryPathName>
     group= <LoadOrderGroup>
     tag= <yes|no>
     depend= <Dependencies(separated by / (forward slash))>
     obj= <AccountName|ObjectName>
           (default = LocalSystem)
     DisplayName= <display name>
     password= <password>
    

    You can start your pythonscript by starting the python interpreter with your script as argument:

    python.exe myscript.py
    
    0 讨论(0)
  • 2020-12-01 10:23

    You can make it a daemon. There is a PEP for a more complete solution, but I have found that this works well.

    import os, sys
    
    def become_daemon(our_home_dir='.', out_log='/dev/null', err_log='/dev/null', pidfile='/var/tmp/daemon.pid'):
        """ Make the current process a daemon.  """
    
        try:
            # First fork
            try:
                if os.fork() > 0:
                    sys.exit(0)
            except OSError, e:
                sys.stderr.write('fork #1 failed" (%d) %s\n' % (e.errno, e.strerror))
                sys.exit(1)
    
            os.setsid()
            os.chdir(our_home_dir)
            os.umask(0)
    
            # Second fork
            try:
                pid = os.fork()
                if pid > 0:
                    # You must write the pid file here.  After the exit()
                    # the pid variable is gone.
                    fpid = open(pidfile, 'wb')
                    fpid.write(str(pid))
                    fpid.close()
                    sys.exit(0)
            except OSError, e:
                sys.stderr.write('fork #2 failed" (%d) %s\n' % (e.errno, e.strerror))
                sys.exit(1)
    
            si = open('/dev/null', 'r')
            so = open(out_log, 'a+', 0)
            se = open(err_log, 'a+', 0)
            os.dup2(si.fileno(), sys.stdin.fileno())
            os.dup2(so.fileno(), sys.stdout.fileno())
            os.dup2(se.fileno(), sys.stderr.fileno())
        except Exception, e:
            sys.stderr.write(str(e))
    
    0 讨论(0)
  • 2020-12-01 10:23

    There is the very helpful Pypi package which is the basis for my daemons written in Python.

    0 讨论(0)
  • 2020-12-01 10:23

    If you are talking about linux, it is as easy as doing something like ./myscript.py &

    0 讨论(0)
  • 2020-12-01 10:24

    Assuming this is for Windows, see this recipe based on srvany

    0 讨论(0)
  • 2020-12-01 10:25

    You might want to check out Twisted.

    0 讨论(0)
提交回复
热议问题