Search code examples
phpprocessexecsli

SSI/PHP exec commands terminate when the response is send back, right?


Say I have a SSI script that uses exec, or a PHP script that uses exec or proc_open, to start a process when a users sends some data from their browser to my server. Am I correct that this spawned process will terminate when the server finishes processing the request and sends the response back to the server? Whether I use SSI or PHP the spawned process will terminate at this point, right?

And hence there is no way to 'keep the process alive' between separate requests so I would need to write a daemon program if I want to interact with the same process on subsequent requests?


Solution

  • Actually it is quite simple to keep a process alive, we do it all the time:

    Create a shellscript (wrapper.sh) like

    #!/bin/bash
    /path/to/some/process < /dev/zero > "$1" &
    echo "Blah"
    

    We found the echo "Blah" to be necessary on some systems.

    Spawn the process with wrapper.sh "/path/to/output/file", it will return almost immediately - on a later script call you can read /path/to/output/file to get a result.