ok i been tinkering with this a bit. you obviously found the 'Can't use function return value in write context' error when trying to run system in an if clause.
btw, system() echoes all the results regardless of whether you set a return value or whatever. try [man]shell_exec/man instead.
I am able to launch the process and it seems to survive closing all the browser windows. I'm using this script:
<?php
error_reporting(E_ALL);
$service_name = 'flashmog_server.php';
$server_path = '/www/html/flashmog/' . $service_name;
// check if the server is running
$check = 'ps ax | grep -v grep | grep ' . $service_name;
$check_result = shell_exec($check);
echo 'initial check result:' . $check_result . '<br>';
if (!empty($check_result)) {
// server is running
echo $service_name . ' is already running.';
} else {
// server not running, start
$start = "(php " . $server_path . " &) > /dev/null";
system($start);
// check that it started
$check_result = shell_exec($check);
echo 'post-launch check result:' . $check_result . '<br>';
if (!empty($check_result)) {
echo $service_name . ' was started successfully.';
}
else {
echo $service_name . ' did not start.';
}
}
?>
I wrote a little script to check if it's running (and hopefully report more than one instance?):
<?
$service_name = 'flashmog_server.php';
$check = 'ps ax | grep -v grep | grep ' . $service_name;
passthru($check);
?>
It returns this:
13092 ? S 0:00 php /www/html/flashmog/flashmog_server.php
I also wrote a script to do a ps:
<?
$result = shell_exec('ps');
echo nl2br($result);
?>
Interestingly, the ps lists flashmog_server as 'php' rather than 'flashmog_server':
PID TTY TIME CMD
19728 ? 00:00:07 apache2
19729 ? 00:00:13 apache2
19730 ? 00:00:11 apache2
19731 ? 00:00:06 apache2
19732 ? 00:00:10 apache2
20497 ? 00:00:02 apache2
21390 ? 00:00:13 apache2
8930 ? 00:00:00 apache2
8931 ? 00:00:00 apache2
8932 ? 00:00:00 apache2
13092 ? 00:00:00 php
13174 ? 00:00:00 ps
There are two problems I'm having really. The first is with the launcher script checking for the daemon so quickly. If for some reason I specify the wrong script path or there is a syntax error in the server, the check will still return true because PHP is running at the time of the check however the server will end as soon as php figures out the fatal error. I considered sleeping between the launch and the check but it seems more exact to get a success/failure result from the attempt at executing the file.
The second problem I'm having is terminating the process. As I pointed out with my ps.php script, the process executing is listed as php rather than flashmog_server so end_server.php doesn't seem to work:
<?
error_reporting(E_ALL);
$service_name = 'flashmog_server.php';
system('killall ' . $service_name);
// check if the server is running
$check = 'ps ax | grep -v grep | grep ' . $service_name;
$check_result = shell_exec($check);
echo 'check result:' . $check_result . '<br>';
if (!empty($check_result)) {
// server is running
echo 'Server end failed. ' . $service_name.' is still running.';
} else {
echo 'Success. No instances of ' . $service_name . ' are running.';
}
?>
the result:
check result:13092 ? S 0:00 php /www/html/flashmog/flashmog_server.php
Server end failed. flashmog_server.php is still running.
Yet another consideration seems to be the possibility of running more than one instance of this daemon on a given server. I suspect that each user running the process will not have visibility to any other user's instance, but if a single user wanted to run more than one game, we would need to be able to start/kill each one independently and would somehow need to track them separately in order to start/end.
my check.php script appears to return the process id of whatever instances it finds running, so i think we might be able to kill the process using 'kill -9 13092' or something. It would be nice if a given project could track the PID of the server from the time it launches and when end_server needs to be called, it would somehow know where to look for the pid.
Thoughts?