Start background process/daemon from CGI script

后端 未结 10 1158
北恋
北恋 2020-12-10 02:02

I\'m trying to launch a background process from a CGI scripts. Basically, when a form is submitted the CGI script will indicate to the user that his or her request is being

10条回答
  •  借酒劲吻你
    2020-12-10 02:59

    As other answers have noted, it is tricky to start a persistent process from your CGI script because the process must cleanly dissociate itself from the CGI program. I have found that a great general-purpose program for this is daemon. It takes care of the messy details involving open file handles, process groups, root directory, etc etc for you. So the pattern of such a CGI program is:

    #!/bin/sh
    foo-service-ping || daemon --restart foo-service
    
    # ... followed below by some CGI handler that uses the "foo" service
    

    The original post describes the case where you want your CGI program to return quickly, while spawning off a background process to finish handling that one request. But there is also the case where your web application depends on a running service which must be kept alive. (Other people have talked about using beanstalkd to handle jobs. But how do you ensure that beanstalkd itself is alive?) One way to do this is to restart the service (if it's down) from within the CGI script. This approach makes sense in an environment where you have limited control over the server and can't rely on things like cron or an init.d mechanism.

提交回复
热议问题