EDIT: Better solution.

When you run the command to start the server, use

./server.php &

The ampersand will tell it to kick it to the background, but it still runs and everything. Then it can still be checked on with ps -C and sent a kill sig based on the PID or the process name in the event of a failure. That should work, I'm testing it out now with a server that just sleep(100000);'s 😃

    New post cause I think I solved it!!

    I found some info on if/else structures, as well as checking if a service is running, here:
    http://www.anyexample.com/linux_bsd/bash/check_if_program_is_running_with_bash_shell_script.xml

    I've got a test server set up. Check this out.

    Files: server_start.sh and server.php

    server.php

    <?PHP
    sleep(10000);
    ?>
    

    server_start.sh

    ## attempt to start the server
    SERVICE='server.php'
    ./server.php & > /dev/null
    
    ## check if it is running
    if ps ax | grep -v grep | grep $SERVICE > /dev/null
    then
       echo "$SERVICE daemon has been started successfully."
    else
       echo "$SERVICE daemon failed to start."
    fi
    

    Here's a screen of me running it:

    🆒

      Ok so I adapted your scripting technique to start the daemon:

      ## attempts to start the server
      SERVICE_PROCESS='flashmog_server'
      
      
      ## check if it is running
      if ps ax | grep -v grep | grep $SERVICE_PROCESS > /dev/null
      then
        echo "$SERVICE_PROCESS daemon is already running."
      else
      
        ## launch the server
        ../flashmog_server.php & > /dev/null
      
        ## check if it running now
        if ps ax | grep -v grep | grep $SERVICE_PROCESS > /dev/null
        then
          echo "$SERVICE_PROCESS daemon launched successfully."
         else
          echo "$SERVICE_PROCESS daemon failed to start."
        fi
      fi
      

      It does launch the server successfully but all output from the server is still routed to stdout. If I run the script from the command line, output from the server script appears in my terminal window despite the '> /dev/null' part. I'm guessing that's supposed to route output to 'null' - meaning throw it away.

      I thought this was still workable because the success/failure messages do appear immediately rather then when the daemon finishes (which is never) so I created a php file to call start_server.sh like so:

      <?php
      // put in same directory as shell script to start your flash-php socket server
      system("./start_server.sh");
      echo 'shell script complete';
      ?>
      

      When I access this file in a browser, the server starts, i see the 'success' message in the browser - but i also get all the output from the server itself. More importantly, the browser has the hourglass icon on my mouse - meaning that php script is still running and there's still an output stream connection between my browser and apache. Most importantly of all, if I close all my browser windows the server will halt shortly after.

      We're close, but not finished 🙁

      I hope to get my server source up this weekend...it'll be GPL...site in progress here:
      http://jaith.net/flashmog

        Yeah, I'm playing with it right now too on my test machine. I know what you mean about it hanging in the browser, but for some reason when you run the exact command from bash it doesn't hang, but in PHP system() or exec() it does hang it for some reason. I don't get it. Playing with it now though.

          gah.

          This is so weird - no matter which system call i use from php, even if I throw in the & to kick the process to the background, PHP still hangs on it waiting for output, whether I call the scripts directly or not. I don't get it. :/ Working on it, will update with progress. :\

            EDIT: I got it!

            <?PHP
            $serviceName = 'flashmog_server.php';
            
            // check if the server is running
            $check = "ps ax | grep -v grep | grep $serviceName";
            $runCheck = system($check);
            
            if (!empty($runCheck)) {
               // server is running
               echo $serviceName.' is already running.';
            }
            else {
               // server not running, start
               $start = "(php ".$serviceName." &) > /dev/null";
               system($start);
            
               // check that it started
               $startCheck = system($check);
               if (!empty($startCheck)) {
                  echo $serviceName.' was started successfully.';
               }
               else {
                  echo $serviceName.' did not start.';
               }
            }
            ?>
            

            The only problem I can find with it right now is that if you call it without killing the server first if it's already running, it outputs this:
            25434 ? S 0:00 php flashmog_server.php flashmog_server.php is already running.

            I'm guessing that's because I don't set a retval in system on the first check, but I dunno. But this code works to start the server, and to check if the server is already running. 😃

            Edit: I set return values on all my system calls and it still outputs the results of that first grep check for some reason, no idea why. :\

              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?

                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.

                I thought about this earlier, but didn't get to coding it. My roommate made me watch Resident Evil Apocalypse with her. :/

                Anyways, the way you can do this is system() the grep command to see which one is running and grab the PID from the output of that. The PID is the first number that gets returned, but I can't say for sure if it will always be a 5-digit number, so what you'd want to do is use explode() by the space character, and the first bit returned would be the PID. You could use that immediately after starting the server, set it in a session, and use that session info to force kill it later, and also to keep track of multiple instances.

                That oughta work.

                  Yes but suppose you have two instances running (one for Game X and one for Game Y). We don't want the end_server script of Game X killing both instances.

                  Is there any way to obtain the specific PID resulting when we start the server?

                    If it's the first server instance, it could grab the PID from a grep flashmog_server, but it wouldn't work from then on. The only thing I can think of right now is maybe something like this?

                    $flashmog_rand = rand();
                    $serviceName = 'flashmog_server_'.$flashmog_rand.'.php';
                    

                    and then use that specific service name for the grep checks to grab the PID after you start it.

                      Hey. I dunno if you've worked something out here on your own or not, but I was scrolling through my apache configuration file and noticed this bit:

                      #
                      # PidFile: The file in which the server should record its process
                      # identification number when it starts.
                      #
                      PidFile run/httpd.pid
                      

                      I would assume that's for tracking it's PID for multiple instances. Maybe you could look into that?

                        There's an idea. I'm thinking there must be some way to launch a process (CLI or otherwise) and have the PID be returned.

                        I have put up a site to host my project...still very much beta
                        http://flashmog.net

                          I think I may have a solution...

                          When you run a command with the background & symbol, the output that gets returned is:

                          that task number [1]
                          and the PID.

                          So:

                          sleep 1000 &
                          

                          returns:

                          [1] 3152
                          

                          Now, if you were to call the server with that outputting to a variable instead of to /dev/null, and then explode() the variable into an array, seperated by spaces, you'd have:
                          $pidArray[0] = '[1]';
                          $pidArray[1] = '3152';

                          Then you would take $pidArray[1] and write it to a file with the date and time, and maybe a unique ID for that server. Then you could implement a web-based control panel for that file, if you wanted to get that in-depth with it.

                            i have tried to get that output routed to php but haven't had any luck. If I remove the parentheses or the > /dev/null then i get the script-never-finishes issue in my browser where the hourglass never goes away and i get no output.

                            if i leave them in, there is no output from the command at all so i can't get the pid.

                              Try writing it to a pid.log file instead of /dev/null, then use PHP to read the pid.log file.

                                Ok....i tried this script. The output from the socket server goes into the log but I never get the line with the PID:

                                <?php
                                error_reporting(E_ALL);
                                $service_name = 'flashmog_server.php';
                                $server_path = '/var/www/html/' . $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.<br>';
                                } else {
                                  // server not running, start
                                  $start = "(php " . $server_path . " &) > pid.log";
                                  echo 'command:' . $start . '<br>';
                                  $output = array();
                                  $result = exec($start, $output, $return_var);
                                  echo 'result:' . $result . '<br>';
                                  echo 'output:' . print_r($output, true) . '<br>';
                                  echo 'return_var:' . $return_var . '<br>';
                                  // 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 also tried this:

                                  $start = "php " . $server_path . " & > pid.log";
                                

                                But it results in the never-finish problem.

                                  That's so weird. Any help from that linux forum you hang out on?

                                    6 days later

                                    still no responses yet. 🙁

                                    I'm starting to think that the code you came up with is good enough for a temporary solution. I'm starting to think that I might have to write something in C or C++ to properly track PIDs and stuff.

                                    In the meantime, I've put up a pretty detailed quick start guide:
                                    http://flashmog.net/quick_start.php