How to determine whether a command is running or not from bash












0















I want to re-run a file php every time it ends...



Then I have created i file SH like this



VAR_1=1;

while [[ $VAR_1 != "2" ]]; do

for filename in *.php; do

if [ -- check if command is NOT running -- ]; then
sleep .5
php $filename
fi

done
done


What kind of IF i must use ?










share|improve this question























  • What is the output of the php process? Use that to determine when to re-run that file!

    – George Udosen
    8 hours ago
















0















I want to re-run a file php every time it ends...



Then I have created i file SH like this



VAR_1=1;

while [[ $VAR_1 != "2" ]]; do

for filename in *.php; do

if [ -- check if command is NOT running -- ]; then
sleep .5
php $filename
fi

done
done


What kind of IF i must use ?










share|improve this question























  • What is the output of the php process? Use that to determine when to re-run that file!

    – George Udosen
    8 hours ago














0












0








0








I want to re-run a file php every time it ends...



Then I have created i file SH like this



VAR_1=1;

while [[ $VAR_1 != "2" ]]; do

for filename in *.php; do

if [ -- check if command is NOT running -- ]; then
sleep .5
php $filename
fi

done
done


What kind of IF i must use ?










share|improve this question














I want to re-run a file php every time it ends...



Then I have created i file SH like this



VAR_1=1;

while [[ $VAR_1 != "2" ]]; do

for filename in *.php; do

if [ -- check if command is NOT running -- ]; then
sleep .5
php $filename
fi

done
done


What kind of IF i must use ?







command-line bash






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 8 hours ago









FireFoxIIFireFoxII

1083




1083













  • What is the output of the php process? Use that to determine when to re-run that file!

    – George Udosen
    8 hours ago



















  • What is the output of the php process? Use that to determine when to re-run that file!

    – George Udosen
    8 hours ago

















What is the output of the php process? Use that to determine when to re-run that file!

– George Udosen
8 hours ago





What is the output of the php process? Use that to determine when to re-run that file!

– George Udosen
8 hours ago










2 Answers
2






active

oldest

votes


















0














( readlink /proc/*/exe | grep -q '/path/to/the/command' )


Will return 0 if a process is running /path/to/the/command.



However, the canonical way to do this is a bit different:




  • On startup, the process checks if some known file (typically /run/$commandname.pid) exists

  • If so, it reads the PID in it, and check if said process is still running (for instance, there is a /proc/$pid/ directory)

  • If yes, it exits

  • Otherwise (no PID file, or process no longer running), it writes its own PID in the file and starts.






share|improve this answer































    0














    use pgrep



    if ! pgrep -f "php $filename"; then echo "command not running"; fi


    However, "I want to re-run a file php every time it ends", so why don't you just do this:



    while true; do
    php "$filename"
    done


    Then you don't have to search for it, when it ends it will just run again.






    share|improve this answer























      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "89"
      };
      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: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      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/3.0/"u003ecc by-sa 3.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%2faskubuntu.com%2fquestions%2f1122525%2fhow-to-determine-whether-a-command-is-running-or-not-from-bash%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      ( readlink /proc/*/exe | grep -q '/path/to/the/command' )


      Will return 0 if a process is running /path/to/the/command.



      However, the canonical way to do this is a bit different:




      • On startup, the process checks if some known file (typically /run/$commandname.pid) exists

      • If so, it reads the PID in it, and check if said process is still running (for instance, there is a /proc/$pid/ directory)

      • If yes, it exits

      • Otherwise (no PID file, or process no longer running), it writes its own PID in the file and starts.






      share|improve this answer




























        0














        ( readlink /proc/*/exe | grep -q '/path/to/the/command' )


        Will return 0 if a process is running /path/to/the/command.



        However, the canonical way to do this is a bit different:




        • On startup, the process checks if some known file (typically /run/$commandname.pid) exists

        • If so, it reads the PID in it, and check if said process is still running (for instance, there is a /proc/$pid/ directory)

        • If yes, it exits

        • Otherwise (no PID file, or process no longer running), it writes its own PID in the file and starts.






        share|improve this answer


























          0












          0








          0







          ( readlink /proc/*/exe | grep -q '/path/to/the/command' )


          Will return 0 if a process is running /path/to/the/command.



          However, the canonical way to do this is a bit different:




          • On startup, the process checks if some known file (typically /run/$commandname.pid) exists

          • If so, it reads the PID in it, and check if said process is still running (for instance, there is a /proc/$pid/ directory)

          • If yes, it exits

          • Otherwise (no PID file, or process no longer running), it writes its own PID in the file and starts.






          share|improve this answer













          ( readlink /proc/*/exe | grep -q '/path/to/the/command' )


          Will return 0 if a process is running /path/to/the/command.



          However, the canonical way to do this is a bit different:




          • On startup, the process checks if some known file (typically /run/$commandname.pid) exists

          • If so, it reads the PID in it, and check if said process is still running (for instance, there is a /proc/$pid/ directory)

          • If yes, it exits

          • Otherwise (no PID file, or process no longer running), it writes its own PID in the file and starts.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 7 hours ago









          xenoidxenoid

          1,7531416




          1,7531416

























              0














              use pgrep



              if ! pgrep -f "php $filename"; then echo "command not running"; fi


              However, "I want to re-run a file php every time it ends", so why don't you just do this:



              while true; do
              php "$filename"
              done


              Then you don't have to search for it, when it ends it will just run again.






              share|improve this answer




























                0














                use pgrep



                if ! pgrep -f "php $filename"; then echo "command not running"; fi


                However, "I want to re-run a file php every time it ends", so why don't you just do this:



                while true; do
                php "$filename"
                done


                Then you don't have to search for it, when it ends it will just run again.






                share|improve this answer


























                  0












                  0








                  0







                  use pgrep



                  if ! pgrep -f "php $filename"; then echo "command not running"; fi


                  However, "I want to re-run a file php every time it ends", so why don't you just do this:



                  while true; do
                  php "$filename"
                  done


                  Then you don't have to search for it, when it ends it will just run again.






                  share|improve this answer













                  use pgrep



                  if ! pgrep -f "php $filename"; then echo "command not running"; fi


                  However, "I want to re-run a file php every time it ends", so why don't you just do this:



                  while true; do
                  php "$filename"
                  done


                  Then you don't have to search for it, when it ends it will just run again.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 6 hours ago









                  glenn jackmanglenn jackman

                  12.5k2545




                  12.5k2545






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Ask Ubuntu!


                      • 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%2faskubuntu.com%2fquestions%2f1122525%2fhow-to-determine-whether-a-command-is-running-or-not-from-bash%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

                      Category:香港粉麵

                      List *all* the tuples!

                      Channel [V]