After much searching, I found a cool way to launch a daemon thread using exec:
$start_cmd = /path/to/script.php > /path/to/output.txt & echo $!';
$output = NULL; // will contain an array of output lines from the exec command
$result = NULL; // will contain the success/failure code returned by the OS. Will be zero for a valid command, non-zero otherwise
$return = exec($start_cmd, $output, $result); // $return will contain the last line from the result of the command which should be the PID of the process we$
$start_results = array(
'pid' => intval($return),
'return' => $return,
'start_cmd' => $start_cmd,
'output' => $output,
'result' => $result
);
The above code will return immediately and script.php will be started as a background process and can run indefinitely.
The problem I am having is that the script I want to launch is a socket server and, since it must bind to port 843, it must run as root. At least, I think it needs to run as root. It certainly gives me an error when I don't and it's my understanding that you can't bind to any ports under 1024 unless you are running as root.
At any rate, it seems to me that I could always to 'sudo' before the command. HOWEVER I don't know how I might go about supplying the password to a sudo command. Is there a flag you can use to supply the password in that single command? If not, I am not sure how to supply the password.
Also, this sounds a bit unsafe to me. The path to the script I want to run is basically hard wired but if someone were to edit the script it could run rampant with root permission.
Any advice about how to handle this situation would be much appreciated.