views:

514

answers:

3

I have a simple web.py program to load data. In the server I don't want to install apache or any webserver.

I try to put it as a background service with http://www.jejik.com/articles/2007/02/a_simple_unix_linux_daemon_in_python/

And subclassing: (from http://www.jejik.com/files/examples/daemon.py)

class Daemon:
    def start(self):
     """
     Start the daemon
     """
     ... PID CHECKS....

     # Start the daemon
     self.daemonize()
     self.run()
#My code
class WebService(Daemon):
        def run(self):
            app.run()

if __name__ == "__main__":
    if DEBUG:
        app.run()
    else:
        service = WebService(os.path.join(DIR_ACTUAL,'ElAdministrador.pid'))
        if len(sys.argv) == 2:
         if 'start' == sys.argv[1]:
          service.start()
         elif 'stop' == sys.argv[1]:
          service.stop()
         elif 'restart' == sys.argv[1]:
          service.restart()
         else:
          print "Unknown command"
          sys.exit(2)
         sys.exit(0)
        else:
         print "usage: %s start|stop|restart" % sys.argv[0]
         sys.exit(2)

However, the web.py software not load (ie: The service no listen)

If I call it directly (ie: No using the daemon code) work fine.

A: 

I don't think you're telling the daemon to run. You need to instantiate a MyDaemon object and call o.run(). It looks like WebService only starts and stops the service interface to your web app, not the actual web app itself.

mcandre
I update the question to show that when call "start" is called the "run" method.
mamcx
I still can't tell how service (from WebService) connects to MyDaemon, unless it's implicit.
mcandre
Oh, my mistake :(. I forget to put the correct className (bad kittie copy/paste!)
mamcx
+2  A: 

I finally find the problem.

Web.py accept from command-line the optional port number:

python code.py 80

And the script also take input from the command-line:

python WebServer start

then web.py try to use "start" as port number and fail. I don't see the error because was in the bacground.

I fix this with a mini-hack:

if __name__ == "__main__":
    if DEBUG:
        app.run()
    else:
        service = WebService(os.path.join(DIR_ACTUAL,'ElAdministrador.pid'))
        if len(sys.argv) == 2:
            if 'start' == sys.argv[1]:
                sys.argv[1] = '8080'
                service.start()
mamcx
A: 

you can start the web.py by using this command

/usr/bin/python index.py > log.txt 2>&1 & 
maguschen