3

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?

Jim_CS
  • 4,082
  • 13
  • 44
  • 80

1 Answers1

2

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.

Eugen Rieck
  • 64,175
  • 10
  • 70
  • 92
  • Im not quite sure what you are saying. Say I call your wrapper.sh from php and it spawns a process with an ID of '3'. Say this process is some kind of environment that lets me set variables and do computations, like a very powerful calculator. So I set some variable like "var answer = resultFromHugeComputation()'. I send the 'answer' variable back to the client. Now the client makes a new request to the server, are you saying that this process with ID 3 will still be running on this new request and I will be able to interact with it? – Jim_CS Jul 04 '12 at 18:11
  • First: Yes, the process will be still runing with the same process ID, **obviously only if** it is wriiten in a way to not end. Second: To interact with it, you need a lot of infrastructure: The output file method I mentioned is suitable only for related requests: The later one fetching the result of an earlier one, which started the process. If you want repeated interaction, use a daemon ans something like sockets. – Eugen Rieck Jul 04 '12 at 18:20