Condition sometimes failing to test if a specific process is running or not












0














I can't understand why my script, sometimes, won't recognize if my Firefox is running. Analyzing the stdout I could state that this condition (if ps cax | grep firefox > /dev/null) sometimes is skipped even when my Firefox is running.



Currently using Xubuntu 18.10 Livecd



The script called at startup.



#!/bin/bash 

[[ -f /home/xubuntu/controle_memoria.lock ]] && exit 1
> /home/xubuntu/controle_memoria.lock

while true ; do
free=`free -m | grep Mem | awk '{print $4}'`
if [ "$free" -gt 0 ]
then
if [ $free -le 120 ]; #When my memory consuptiom goes below 120MB do the commands below.
then

if ps cax | grep firefox > /dev/null
then
sudo killall -9 firefox-bin
firefox &> /dev/null &

else
echo "Stopped"
fi

sudo sysctl -w vm.drop_caches=3
sudo sync && echo 3 | sudo tee /proc/sys/vm/drop_caches



fi
fi & sleep 1; done









share|improve this question





























    0














    I can't understand why my script, sometimes, won't recognize if my Firefox is running. Analyzing the stdout I could state that this condition (if ps cax | grep firefox > /dev/null) sometimes is skipped even when my Firefox is running.



    Currently using Xubuntu 18.10 Livecd



    The script called at startup.



    #!/bin/bash 

    [[ -f /home/xubuntu/controle_memoria.lock ]] && exit 1
    > /home/xubuntu/controle_memoria.lock

    while true ; do
    free=`free -m | grep Mem | awk '{print $4}'`
    if [ "$free" -gt 0 ]
    then
    if [ $free -le 120 ]; #When my memory consuptiom goes below 120MB do the commands below.
    then

    if ps cax | grep firefox > /dev/null
    then
    sudo killall -9 firefox-bin
    firefox &> /dev/null &

    else
    echo "Stopped"
    fi

    sudo sysctl -w vm.drop_caches=3
    sudo sync && echo 3 | sudo tee /proc/sys/vm/drop_caches



    fi
    fi & sleep 1; done









    share|improve this question



























      0












      0








      0







      I can't understand why my script, sometimes, won't recognize if my Firefox is running. Analyzing the stdout I could state that this condition (if ps cax | grep firefox > /dev/null) sometimes is skipped even when my Firefox is running.



      Currently using Xubuntu 18.10 Livecd



      The script called at startup.



      #!/bin/bash 

      [[ -f /home/xubuntu/controle_memoria.lock ]] && exit 1
      > /home/xubuntu/controle_memoria.lock

      while true ; do
      free=`free -m | grep Mem | awk '{print $4}'`
      if [ "$free" -gt 0 ]
      then
      if [ $free -le 120 ]; #When my memory consuptiom goes below 120MB do the commands below.
      then

      if ps cax | grep firefox > /dev/null
      then
      sudo killall -9 firefox-bin
      firefox &> /dev/null &

      else
      echo "Stopped"
      fi

      sudo sysctl -w vm.drop_caches=3
      sudo sync && echo 3 | sudo tee /proc/sys/vm/drop_caches



      fi
      fi & sleep 1; done









      share|improve this question















      I can't understand why my script, sometimes, won't recognize if my Firefox is running. Analyzing the stdout I could state that this condition (if ps cax | grep firefox > /dev/null) sometimes is skipped even when my Firefox is running.



      Currently using Xubuntu 18.10 Livecd



      The script called at startup.



      #!/bin/bash 

      [[ -f /home/xubuntu/controle_memoria.lock ]] && exit 1
      > /home/xubuntu/controle_memoria.lock

      while true ; do
      free=`free -m | grep Mem | awk '{print $4}'`
      if [ "$free" -gt 0 ]
      then
      if [ $free -le 120 ]; #When my memory consuptiom goes below 120MB do the commands below.
      then

      if ps cax | grep firefox > /dev/null
      then
      sudo killall -9 firefox-bin
      firefox &> /dev/null &

      else
      echo "Stopped"
      fi

      sudo sysctl -w vm.drop_caches=3
      sudo sync && echo 3 | sudo tee /proc/sys/vm/drop_caches



      fi
      fi & sleep 1; done






      shell-script process livecd xubuntu






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 18 '18 at 23:15

























      asked Dec 18 '18 at 19:25









      Lucas Rizzini

      267




      267






















          2 Answers
          2






          active

          oldest

          votes


















          1















          1. Ps has -C option without need to grep


          2. killall -0 firefox-bin will tell you if firefox-bin process exist by exit code. Without need of pipe and redirection in a raw.

          3. Just remove your if ps and leave killall -9 firefox-bin && firefox-bin &> /dev/null &. So if process will not be killed, it wouldn't be started.






          share|improve this answer





























            0














            What happens if you replace your if ps block with



            for proc in /proc/*
            do
            if [[ $(readlink -f ${proc}/exe) = "/usr/bin/firefox-bin" ]]
            sudo killall -9 firefox-bin
            firefox &> /dev/null &
            else
            echo "Stopped"
            fi
            done


            This removes trying to parse ps output.






            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/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%2funix.stackexchange.com%2fquestions%2f489754%2fcondition-sometimes-failing-to-test-if-a-specific-process-is-running-or-not%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









              1















              1. Ps has -C option without need to grep


              2. killall -0 firefox-bin will tell you if firefox-bin process exist by exit code. Without need of pipe and redirection in a raw.

              3. Just remove your if ps and leave killall -9 firefox-bin && firefox-bin &> /dev/null &. So if process will not be killed, it wouldn't be started.






              share|improve this answer


























                1















                1. Ps has -C option without need to grep


                2. killall -0 firefox-bin will tell you if firefox-bin process exist by exit code. Without need of pipe and redirection in a raw.

                3. Just remove your if ps and leave killall -9 firefox-bin && firefox-bin &> /dev/null &. So if process will not be killed, it wouldn't be started.






                share|improve this answer
























                  1












                  1








                  1







                  1. Ps has -C option without need to grep


                  2. killall -0 firefox-bin will tell you if firefox-bin process exist by exit code. Without need of pipe and redirection in a raw.

                  3. Just remove your if ps and leave killall -9 firefox-bin && firefox-bin &> /dev/null &. So if process will not be killed, it wouldn't be started.






                  share|improve this answer













                  1. Ps has -C option without need to grep


                  2. killall -0 firefox-bin will tell you if firefox-bin process exist by exit code. Without need of pipe and redirection in a raw.

                  3. Just remove your if ps and leave killall -9 firefox-bin && firefox-bin &> /dev/null &. So if process will not be killed, it wouldn't be started.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 19 '18 at 1:25









                  Alexander

                  1,151213




                  1,151213

























                      0














                      What happens if you replace your if ps block with



                      for proc in /proc/*
                      do
                      if [[ $(readlink -f ${proc}/exe) = "/usr/bin/firefox-bin" ]]
                      sudo killall -9 firefox-bin
                      firefox &> /dev/null &
                      else
                      echo "Stopped"
                      fi
                      done


                      This removes trying to parse ps output.






                      share|improve this answer


























                        0














                        What happens if you replace your if ps block with



                        for proc in /proc/*
                        do
                        if [[ $(readlink -f ${proc}/exe) = "/usr/bin/firefox-bin" ]]
                        sudo killall -9 firefox-bin
                        firefox &> /dev/null &
                        else
                        echo "Stopped"
                        fi
                        done


                        This removes trying to parse ps output.






                        share|improve this answer
























                          0












                          0








                          0






                          What happens if you replace your if ps block with



                          for proc in /proc/*
                          do
                          if [[ $(readlink -f ${proc}/exe) = "/usr/bin/firefox-bin" ]]
                          sudo killall -9 firefox-bin
                          firefox &> /dev/null &
                          else
                          echo "Stopped"
                          fi
                          done


                          This removes trying to parse ps output.






                          share|improve this answer












                          What happens if you replace your if ps block with



                          for proc in /proc/*
                          do
                          if [[ $(readlink -f ${proc}/exe) = "/usr/bin/firefox-bin" ]]
                          sudo killall -9 firefox-bin
                          firefox &> /dev/null &
                          else
                          echo "Stopped"
                          fi
                          done


                          This removes trying to parse ps output.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Dec 18 '18 at 23:35









                          Doug O'Neal

                          2,8321817




                          2,8321817






























                              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.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • 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%2f489754%2fcondition-sometimes-failing-to-test-if-a-specific-process-is-running-or-not%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

                              Morgemoulin

                              Scott Moir

                              Souastre