Check if PHP script is running, and if not run itnohup is not working when called from root folderKsh Script...

In the example of guess a specified number between 1 and 20 (both inclusive), what is the sample space?

Why is 1.e4 c5 2.Nf3 b6 so unpopular?

Do any languages mark social distinctions other than gender and status?

Linear Or nonlinear Black Scholes Equation

Did it take 3 minutes to reload a musket when the second amendment to the US constitution was ratified?

Is it poor workplace etiquette to display signs of relative "wealth" at work when others are struggling financially?

出かけることにしました - What is the meaning of this?

Are there any Baryons that have quark-antiquark combinations?

How can a stock trade for a fraction of a cent?

What are these objects near the Cosmonaut's faces?

Who inspired the character Geordi La Forge?

Are my triangles similar?

Best ways to compress and store tons of CO2?

Stare long enough and you will have found the answer

Minimum number of turns to capture all pieces in Checkers

Was Hitler exclaiming "Heil Hitler!" himself when saluting?

Conveying the idea of "tricky"

Is it unusual that English uses possessive for past tense?

Does Turkey make the "structural steel frame" for the F-35 fighter?

If equal temperament divides octave into 12 equal parts, why hertz differences are not the same but element 12th of two?

In this day and age should the definition / categorisation of erotica be revised?

C# Toy Robot Simulator

Do neurons of a neural network model a linear relationship?

Compress .hex file for micro-controller



Check if PHP script is running, and if not run it


nohup is not working when called from root folderKsh Script to ftp multiple directories simultaneouslyCron job to check if PHP script is runningStart a background process from a script and manage it when the script endsRestart PHP-FPM from a PHP scriptHow can I reach an open & listening port on Linux?Sending a signal to a parent processOracle instant client module not found when PHP script run from a systemd service unitRun a script every minute without cron and allowing children






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{
margin-bottom:0;
}








1

















I am running a PHP file using



nohup php server.php &


This listens for a particular port and does actions according to the input.



The problem is that sometimes the script stops. I cannot use a cron job to fix this, because my server is listening to a port all the time and also is creating child processes.



Is there a way to check if server.php is running and (if not) start it?










share|improve this question





























  • Make server.php listen on an UNIX domain socket, and answer with its status when queried. Poll it from cron every few minutes, and restart it if it doesn't answer.

    – lcd047
    Jun 30 '15 at 13:41




















1

















I am running a PHP file using



nohup php server.php &


This listens for a particular port and does actions according to the input.



The problem is that sometimes the script stops. I cannot use a cron job to fix this, because my server is listening to a port all the time and also is creating child processes.



Is there a way to check if server.php is running and (if not) start it?










share|improve this question





























  • Make server.php listen on an UNIX domain socket, and answer with its status when queried. Poll it from cron every few minutes, and restart it if it doesn't answer.

    – lcd047
    Jun 30 '15 at 13:41
















1












1








1








I am running a PHP file using



nohup php server.php &


This listens for a particular port and does actions according to the input.



The problem is that sometimes the script stops. I cannot use a cron job to fix this, because my server is listening to a port all the time and also is creating child processes.



Is there a way to check if server.php is running and (if not) start it?










share|improve this question

















I am running a PHP file using



nohup php server.php &


This listens for a particular port and does actions according to the input.



The problem is that sometimes the script stops. I cannot use a cron job to fix this, because my server is listening to a port all the time and also is creating child processes.



Is there a way to check if server.php is running and (if not) start it?







php background-process






share|improve this question
















share|improve this question













share|improve this question




share|improve this question








edited Oct 26 '15 at 1:05









jasonwryan

53.5k14 gold badges141 silver badges195 bronze badges




53.5k14 gold badges141 silver badges195 bronze badges










asked Jun 30 '15 at 12:23









user1842264user1842264

131 silver badge3 bronze badges




131 silver badge3 bronze badges
















  • Make server.php listen on an UNIX domain socket, and answer with its status when queried. Poll it from cron every few minutes, and restart it if it doesn't answer.

    – lcd047
    Jun 30 '15 at 13:41





















  • Make server.php listen on an UNIX domain socket, and answer with its status when queried. Poll it from cron every few minutes, and restart it if it doesn't answer.

    – lcd047
    Jun 30 '15 at 13:41



















Make server.php listen on an UNIX domain socket, and answer with its status when queried. Poll it from cron every few minutes, and restart it if it doesn't answer.

– lcd047
Jun 30 '15 at 13:41







Make server.php listen on an UNIX domain socket, and answer with its status when queried. Poll it from cron every few minutes, and restart it if it doesn't answer.

– lcd047
Jun 30 '15 at 13:41












3 Answers
3






active

oldest

votes


















1


















You can check for the process using ps, but one drawback is that if you have multiple instances of this process running or if the script is hung up then this method can be less than conclusive.



I prefer to actually check if the server is listening on the port. Here are a couple of ways to do this. If your server is listening on port 2000 for example consider the following.



Using lsof



lsof -i :2000; echo $?;


lsof is checking for open file descriptors and should show whether or not a program is listening or actively communicating on this port. This will echo either a 0 if the server is accepting connections on port 2000 or a 1 if it is not.



Using nc



nc -z -w1 192.168.1.12 2000 &> /dev/null; echo $?;


This is my preferred method for checking on a socket server. Here nc is using the -z flag for zero I/O mode to quickly scan the port. You can use your IP address here and the correct port. If the server is accepting connections then life is good.



Again here return values will be either a 0 for good or a 1 for not good. We are discarding any output here because we are wanting just a quick boolean check. This method returns very fast if the network address is reachable. Run from the server itself you will not see hardly any latency as it is trying to talk to itself.



Automating



To run these tests via cron, create a bash script and execute one or both of these commands and run through a series of logical checks. If they fail restart your script and recheck. I have been using these methods for several years now and have had very good results of practical uptime.






share|improve this answer



























  • i am trying to automate the same but when i use nohup inside the bash its running and exiting at the same time ( executing the bash from root folder )

    – user1842264
    Jul 1 '15 at 10:31













  • Please check this link unix.stackexchange.com/questions/213283/…

    – user1842264
    Jul 1 '15 at 10:39





















0


















Little bit late to the party...



By using the restartd daemon you are able to run programs and bash scripts. Restartd will check every 20 seconds (configurable) if the programs in its list are running.



See the man page here






share|improve this answer


































    0


















    not sure this solve your XY problem



    if ps -C php -o args h | grep -q server.php
    then true ## php server.php running
    else php server.php ## not running
    fi


    ps





    • -C php will seach for php instance,


    • -o args will display full command line,


    • h without header


    grep





    • -q be quiet




    This will check whether php server.php is running, if programm is stop process will be here and will no relaunch.



    Real solution include polling server.php using whatever connection, expecting a know answer without timeout. if fail relaunch.






    share|improve this answer






























      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "106"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });















      draft saved

      draft discarded
















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f213073%2fcheck-if-php-script-is-running-and-if-not-run-it%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown


























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1


















      You can check for the process using ps, but one drawback is that if you have multiple instances of this process running or if the script is hung up then this method can be less than conclusive.



      I prefer to actually check if the server is listening on the port. Here are a couple of ways to do this. If your server is listening on port 2000 for example consider the following.



      Using lsof



      lsof -i :2000; echo $?;


      lsof is checking for open file descriptors and should show whether or not a program is listening or actively communicating on this port. This will echo either a 0 if the server is accepting connections on port 2000 or a 1 if it is not.



      Using nc



      nc -z -w1 192.168.1.12 2000 &> /dev/null; echo $?;


      This is my preferred method for checking on a socket server. Here nc is using the -z flag for zero I/O mode to quickly scan the port. You can use your IP address here and the correct port. If the server is accepting connections then life is good.



      Again here return values will be either a 0 for good or a 1 for not good. We are discarding any output here because we are wanting just a quick boolean check. This method returns very fast if the network address is reachable. Run from the server itself you will not see hardly any latency as it is trying to talk to itself.



      Automating



      To run these tests via cron, create a bash script and execute one or both of these commands and run through a series of logical checks. If they fail restart your script and recheck. I have been using these methods for several years now and have had very good results of practical uptime.






      share|improve this answer



























      • i am trying to automate the same but when i use nohup inside the bash its running and exiting at the same time ( executing the bash from root folder )

        – user1842264
        Jul 1 '15 at 10:31













      • Please check this link unix.stackexchange.com/questions/213283/…

        – user1842264
        Jul 1 '15 at 10:39


















      1


















      You can check for the process using ps, but one drawback is that if you have multiple instances of this process running or if the script is hung up then this method can be less than conclusive.



      I prefer to actually check if the server is listening on the port. Here are a couple of ways to do this. If your server is listening on port 2000 for example consider the following.



      Using lsof



      lsof -i :2000; echo $?;


      lsof is checking for open file descriptors and should show whether or not a program is listening or actively communicating on this port. This will echo either a 0 if the server is accepting connections on port 2000 or a 1 if it is not.



      Using nc



      nc -z -w1 192.168.1.12 2000 &> /dev/null; echo $?;


      This is my preferred method for checking on a socket server. Here nc is using the -z flag for zero I/O mode to quickly scan the port. You can use your IP address here and the correct port. If the server is accepting connections then life is good.



      Again here return values will be either a 0 for good or a 1 for not good. We are discarding any output here because we are wanting just a quick boolean check. This method returns very fast if the network address is reachable. Run from the server itself you will not see hardly any latency as it is trying to talk to itself.



      Automating



      To run these tests via cron, create a bash script and execute one or both of these commands and run through a series of logical checks. If they fail restart your script and recheck. I have been using these methods for several years now and have had very good results of practical uptime.






      share|improve this answer



























      • i am trying to automate the same but when i use nohup inside the bash its running and exiting at the same time ( executing the bash from root folder )

        – user1842264
        Jul 1 '15 at 10:31













      • Please check this link unix.stackexchange.com/questions/213283/…

        – user1842264
        Jul 1 '15 at 10:39
















      1














      1










      1









      You can check for the process using ps, but one drawback is that if you have multiple instances of this process running or if the script is hung up then this method can be less than conclusive.



      I prefer to actually check if the server is listening on the port. Here are a couple of ways to do this. If your server is listening on port 2000 for example consider the following.



      Using lsof



      lsof -i :2000; echo $?;


      lsof is checking for open file descriptors and should show whether or not a program is listening or actively communicating on this port. This will echo either a 0 if the server is accepting connections on port 2000 or a 1 if it is not.



      Using nc



      nc -z -w1 192.168.1.12 2000 &> /dev/null; echo $?;


      This is my preferred method for checking on a socket server. Here nc is using the -z flag for zero I/O mode to quickly scan the port. You can use your IP address here and the correct port. If the server is accepting connections then life is good.



      Again here return values will be either a 0 for good or a 1 for not good. We are discarding any output here because we are wanting just a quick boolean check. This method returns very fast if the network address is reachable. Run from the server itself you will not see hardly any latency as it is trying to talk to itself.



      Automating



      To run these tests via cron, create a bash script and execute one or both of these commands and run through a series of logical checks. If they fail restart your script and recheck. I have been using these methods for several years now and have had very good results of practical uptime.






      share|improve this answer














      You can check for the process using ps, but one drawback is that if you have multiple instances of this process running or if the script is hung up then this method can be less than conclusive.



      I prefer to actually check if the server is listening on the port. Here are a couple of ways to do this. If your server is listening on port 2000 for example consider the following.



      Using lsof



      lsof -i :2000; echo $?;


      lsof is checking for open file descriptors and should show whether or not a program is listening or actively communicating on this port. This will echo either a 0 if the server is accepting connections on port 2000 or a 1 if it is not.



      Using nc



      nc -z -w1 192.168.1.12 2000 &> /dev/null; echo $?;


      This is my preferred method for checking on a socket server. Here nc is using the -z flag for zero I/O mode to quickly scan the port. You can use your IP address here and the correct port. If the server is accepting connections then life is good.



      Again here return values will be either a 0 for good or a 1 for not good. We are discarding any output here because we are wanting just a quick boolean check. This method returns very fast if the network address is reachable. Run from the server itself you will not see hardly any latency as it is trying to talk to itself.



      Automating



      To run these tests via cron, create a bash script and execute one or both of these commands and run through a series of logical checks. If they fail restart your script and recheck. I have been using these methods for several years now and have had very good results of practical uptime.







      share|improve this answer













      share|improve this answer




      share|improve this answer










      answered Jun 30 '15 at 13:08









      111---111---

      3,00714 silver badges41 bronze badges




      3,00714 silver badges41 bronze badges
















      • i am trying to automate the same but when i use nohup inside the bash its running and exiting at the same time ( executing the bash from root folder )

        – user1842264
        Jul 1 '15 at 10:31













      • Please check this link unix.stackexchange.com/questions/213283/…

        – user1842264
        Jul 1 '15 at 10:39





















      • i am trying to automate the same but when i use nohup inside the bash its running and exiting at the same time ( executing the bash from root folder )

        – user1842264
        Jul 1 '15 at 10:31













      • Please check this link unix.stackexchange.com/questions/213283/…

        – user1842264
        Jul 1 '15 at 10:39



















      i am trying to automate the same but when i use nohup inside the bash its running and exiting at the same time ( executing the bash from root folder )

      – user1842264
      Jul 1 '15 at 10:31







      i am trying to automate the same but when i use nohup inside the bash its running and exiting at the same time ( executing the bash from root folder )

      – user1842264
      Jul 1 '15 at 10:31















      Please check this link unix.stackexchange.com/questions/213283/…

      – user1842264
      Jul 1 '15 at 10:39







      Please check this link unix.stackexchange.com/questions/213283/…

      – user1842264
      Jul 1 '15 at 10:39















      0


















      Little bit late to the party...



      By using the restartd daemon you are able to run programs and bash scripts. Restartd will check every 20 seconds (configurable) if the programs in its list are running.



      See the man page here






      share|improve this answer































        0


















        Little bit late to the party...



        By using the restartd daemon you are able to run programs and bash scripts. Restartd will check every 20 seconds (configurable) if the programs in its list are running.



        See the man page here






        share|improve this answer





























          0














          0










          0









          Little bit late to the party...



          By using the restartd daemon you are able to run programs and bash scripts. Restartd will check every 20 seconds (configurable) if the programs in its list are running.



          See the man page here






          share|improve this answer














          Little bit late to the party...



          By using the restartd daemon you are able to run programs and bash scripts. Restartd will check every 20 seconds (configurable) if the programs in its list are running.



          See the man page here







          share|improve this answer













          share|improve this answer




          share|improve this answer










          answered Jan 14 '17 at 22:03









          markmark

          1013 bronze badges




          1013 bronze badges


























              0


















              not sure this solve your XY problem



              if ps -C php -o args h | grep -q server.php
              then true ## php server.php running
              else php server.php ## not running
              fi


              ps





              • -C php will seach for php instance,


              • -o args will display full command line,


              • h without header


              grep





              • -q be quiet




              This will check whether php server.php is running, if programm is stop process will be here and will no relaunch.



              Real solution include polling server.php using whatever connection, expecting a know answer without timeout. if fail relaunch.






              share|improve this answer

































                0


















                not sure this solve your XY problem



                if ps -C php -o args h | grep -q server.php
                then true ## php server.php running
                else php server.php ## not running
                fi


                ps





                • -C php will seach for php instance,


                • -o args will display full command line,


                • h without header


                grep





                • -q be quiet




                This will check whether php server.php is running, if programm is stop process will be here and will no relaunch.



                Real solution include polling server.php using whatever connection, expecting a know answer without timeout. if fail relaunch.






                share|improve this answer































                  0














                  0










                  0









                  not sure this solve your XY problem



                  if ps -C php -o args h | grep -q server.php
                  then true ## php server.php running
                  else php server.php ## not running
                  fi


                  ps





                  • -C php will seach for php instance,


                  • -o args will display full command line,


                  • h without header


                  grep





                  • -q be quiet




                  This will check whether php server.php is running, if programm is stop process will be here and will no relaunch.



                  Real solution include polling server.php using whatever connection, expecting a know answer without timeout. if fail relaunch.






                  share|improve this answer
















                  not sure this solve your XY problem



                  if ps -C php -o args h | grep -q server.php
                  then true ## php server.php running
                  else php server.php ## not running
                  fi


                  ps





                  • -C php will seach for php instance,


                  • -o args will display full command line,


                  • h without header


                  grep





                  • -q be quiet




                  This will check whether php server.php is running, if programm is stop process will be here and will no relaunch.



                  Real solution include polling server.php using whatever connection, expecting a know answer without timeout. if fail relaunch.







                  share|improve this answer















                  share|improve this answer




                  share|improve this answer








                  edited Mar 17 '18 at 23:39









                  Rui F Ribeiro

                  41.8k16 gold badges97 silver badges158 bronze badges




                  41.8k16 gold badges97 silver badges158 bronze badges










                  answered Jun 30 '15 at 12:52









                  ArchemarArchemar

                  21.7k9 gold badges42 silver badges77 bronze badges




                  21.7k9 gold badges42 silver badges77 bronze badges


































                      draft saved

                      draft discarded



















































                      Thanks for contributing an answer to Unix & Linux Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f213073%2fcheck-if-php-script-is-running-and-if-not-run-it%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown









                      Popular posts from this blog

                      Taj Mahal Inhaltsverzeichnis Aufbau | Geschichte | 350-Jahr-Feier | Heutige Bedeutung | Siehe auch |...

                      Baia Sprie Cuprins Etimologie | Istorie | Demografie | Politică și administrație | Arii naturale...

                      Nicolae Petrescu-Găină Cuprins Biografie | Opera | In memoriam | Varia | Controverse, incertitudini...