wait for predefined time before exit the script

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












I have an app server running in my unix box with process name as abc_test. I have a shell script in which I have to stop my server using /opt/abc/bin/stop. Now sometimes my server doesn't get stopped so I need to check whether my process is still running or not. If it is running then sleep for some seconds and then check again whether process is still running or not. If after 5 minutes process is still running, then I want to exit successfully from the shell script but if the process is not running then exit successfully as well from the shell script.



So I came up with below script but I am not able to understand how to add this 5 minute thing in my below shell script. Does my below script will do exactly as I am thinking? I guess this might be improved as well.



#!/bin/bash

/opt/abc/bin/stop

# Wait for 10 second
sleep 10

# Number of seconds to wait
WAIT_SECONDS=300
# Counter to keep count of how many seconds have passed
count=0

while pgrep abc_test > /dev/null
do
echo "server is still running."
# Wait for one second
sleep 1
# Increment the second counter
((count++))
# Has the process been killed? If so, exit the loop.
if ! pgrep abc_test > /dev/null ; then
break
fi
# Have we exceeded $WAIT_SECONDS? If so exit the loop
if [ $count -gt $WAIT_SECONDS ]; then
break
fi
done









share|improve this question



















  • 2




    Does my below script will do exactly as I am thinking?. Run it and see? (And then edit with your results, if not).
    – Michael Homer
    Dec 17 '15 at 0:51










  • You could use the SECONDS variable to see how many seconds have passed since the script began.
    – muru
    Dec 17 '15 at 1:01














up vote
1
down vote

favorite












I have an app server running in my unix box with process name as abc_test. I have a shell script in which I have to stop my server using /opt/abc/bin/stop. Now sometimes my server doesn't get stopped so I need to check whether my process is still running or not. If it is running then sleep for some seconds and then check again whether process is still running or not. If after 5 minutes process is still running, then I want to exit successfully from the shell script but if the process is not running then exit successfully as well from the shell script.



So I came up with below script but I am not able to understand how to add this 5 minute thing in my below shell script. Does my below script will do exactly as I am thinking? I guess this might be improved as well.



#!/bin/bash

/opt/abc/bin/stop

# Wait for 10 second
sleep 10

# Number of seconds to wait
WAIT_SECONDS=300
# Counter to keep count of how many seconds have passed
count=0

while pgrep abc_test > /dev/null
do
echo "server is still running."
# Wait for one second
sleep 1
# Increment the second counter
((count++))
# Has the process been killed? If so, exit the loop.
if ! pgrep abc_test > /dev/null ; then
break
fi
# Have we exceeded $WAIT_SECONDS? If so exit the loop
if [ $count -gt $WAIT_SECONDS ]; then
break
fi
done









share|improve this question



















  • 2




    Does my below script will do exactly as I am thinking?. Run it and see? (And then edit with your results, if not).
    – Michael Homer
    Dec 17 '15 at 0:51










  • You could use the SECONDS variable to see how many seconds have passed since the script began.
    – muru
    Dec 17 '15 at 1:01












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I have an app server running in my unix box with process name as abc_test. I have a shell script in which I have to stop my server using /opt/abc/bin/stop. Now sometimes my server doesn't get stopped so I need to check whether my process is still running or not. If it is running then sleep for some seconds and then check again whether process is still running or not. If after 5 minutes process is still running, then I want to exit successfully from the shell script but if the process is not running then exit successfully as well from the shell script.



So I came up with below script but I am not able to understand how to add this 5 minute thing in my below shell script. Does my below script will do exactly as I am thinking? I guess this might be improved as well.



#!/bin/bash

/opt/abc/bin/stop

# Wait for 10 second
sleep 10

# Number of seconds to wait
WAIT_SECONDS=300
# Counter to keep count of how many seconds have passed
count=0

while pgrep abc_test > /dev/null
do
echo "server is still running."
# Wait for one second
sleep 1
# Increment the second counter
((count++))
# Has the process been killed? If so, exit the loop.
if ! pgrep abc_test > /dev/null ; then
break
fi
# Have we exceeded $WAIT_SECONDS? If so exit the loop
if [ $count -gt $WAIT_SECONDS ]; then
break
fi
done









share|improve this question















I have an app server running in my unix box with process name as abc_test. I have a shell script in which I have to stop my server using /opt/abc/bin/stop. Now sometimes my server doesn't get stopped so I need to check whether my process is still running or not. If it is running then sleep for some seconds and then check again whether process is still running or not. If after 5 minutes process is still running, then I want to exit successfully from the shell script but if the process is not running then exit successfully as well from the shell script.



So I came up with below script but I am not able to understand how to add this 5 minute thing in my below shell script. Does my below script will do exactly as I am thinking? I guess this might be improved as well.



#!/bin/bash

/opt/abc/bin/stop

# Wait for 10 second
sleep 10

# Number of seconds to wait
WAIT_SECONDS=300
# Counter to keep count of how many seconds have passed
count=0

while pgrep abc_test > /dev/null
do
echo "server is still running."
# Wait for one second
sleep 1
# Increment the second counter
((count++))
# Has the process been killed? If so, exit the loop.
if ! pgrep abc_test > /dev/null ; then
break
fi
# Have we exceeded $WAIT_SECONDS? If so exit the loop
if [ $count -gt $WAIT_SECONDS ]; then
break
fi
done






shell-script kill






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 17 '15 at 22:36









Gilles

516k12210301557




516k12210301557










asked Dec 17 '15 at 0:48









user1950349

2511312




2511312







  • 2




    Does my below script will do exactly as I am thinking?. Run it and see? (And then edit with your results, if not).
    – Michael Homer
    Dec 17 '15 at 0:51










  • You could use the SECONDS variable to see how many seconds have passed since the script began.
    – muru
    Dec 17 '15 at 1:01












  • 2




    Does my below script will do exactly as I am thinking?. Run it and see? (And then edit with your results, if not).
    – Michael Homer
    Dec 17 '15 at 0:51










  • You could use the SECONDS variable to see how many seconds have passed since the script began.
    – muru
    Dec 17 '15 at 1:01







2




2




Does my below script will do exactly as I am thinking?. Run it and see? (And then edit with your results, if not).
– Michael Homer
Dec 17 '15 at 0:51




Does my below script will do exactly as I am thinking?. Run it and see? (And then edit with your results, if not).
– Michael Homer
Dec 17 '15 at 0:51












You could use the SECONDS variable to see how many seconds have passed since the script began.
– muru
Dec 17 '15 at 1:01




You could use the SECONDS variable to see how many seconds have passed since the script began.
– muru
Dec 17 '15 at 1:01










2 Answers
2






active

oldest

votes

















up vote
3
down vote



accepted










Assuming /opt/abc/bin/stop doesn't block your script seem to work.

As muru suggested you could skip the $count variable and use the builtin $SECONDS. This would lead to code like this:



/opt/abc/bin/stop
# Wait for 10 second
sleep 10
# Number of seconds to wait
WAIT_SECONDS=300
while pgrep abc_test > /dev/null
do
echo "server is still running. Seconds: $SECONDS"
# Wait for one second
sleep 1
# Have we exceeded $WAIT_SECONDS? If so exit the loop
if [ $SECONDS -gt $WAIT_SECONDS ]; then
break
fi
done


In case /opt/abc/bin/stop DOES block, just call it in the background like:



/opt/abc/bin/stop &





share|improve this answer





























    up vote
    0
    down vote













    Write the Unix command for the following: to wait for a specified number of seconds before exit.





    share








    New contributor




    Tara Kumari is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.

















      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',
      convertImagesToLinks: false,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      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%2f249877%2fwait-for-predefined-time-before-exit-the-script%23new-answer', 'question_page');

      );

      Post as a guest






























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      3
      down vote



      accepted










      Assuming /opt/abc/bin/stop doesn't block your script seem to work.

      As muru suggested you could skip the $count variable and use the builtin $SECONDS. This would lead to code like this:



      /opt/abc/bin/stop
      # Wait for 10 second
      sleep 10
      # Number of seconds to wait
      WAIT_SECONDS=300
      while pgrep abc_test > /dev/null
      do
      echo "server is still running. Seconds: $SECONDS"
      # Wait for one second
      sleep 1
      # Have we exceeded $WAIT_SECONDS? If so exit the loop
      if [ $SECONDS -gt $WAIT_SECONDS ]; then
      break
      fi
      done


      In case /opt/abc/bin/stop DOES block, just call it in the background like:



      /opt/abc/bin/stop &





      share|improve this answer


























        up vote
        3
        down vote



        accepted










        Assuming /opt/abc/bin/stop doesn't block your script seem to work.

        As muru suggested you could skip the $count variable and use the builtin $SECONDS. This would lead to code like this:



        /opt/abc/bin/stop
        # Wait for 10 second
        sleep 10
        # Number of seconds to wait
        WAIT_SECONDS=300
        while pgrep abc_test > /dev/null
        do
        echo "server is still running. Seconds: $SECONDS"
        # Wait for one second
        sleep 1
        # Have we exceeded $WAIT_SECONDS? If so exit the loop
        if [ $SECONDS -gt $WAIT_SECONDS ]; then
        break
        fi
        done


        In case /opt/abc/bin/stop DOES block, just call it in the background like:



        /opt/abc/bin/stop &





        share|improve this answer
























          up vote
          3
          down vote



          accepted







          up vote
          3
          down vote



          accepted






          Assuming /opt/abc/bin/stop doesn't block your script seem to work.

          As muru suggested you could skip the $count variable and use the builtin $SECONDS. This would lead to code like this:



          /opt/abc/bin/stop
          # Wait for 10 second
          sleep 10
          # Number of seconds to wait
          WAIT_SECONDS=300
          while pgrep abc_test > /dev/null
          do
          echo "server is still running. Seconds: $SECONDS"
          # Wait for one second
          sleep 1
          # Have we exceeded $WAIT_SECONDS? If so exit the loop
          if [ $SECONDS -gt $WAIT_SECONDS ]; then
          break
          fi
          done


          In case /opt/abc/bin/stop DOES block, just call it in the background like:



          /opt/abc/bin/stop &





          share|improve this answer














          Assuming /opt/abc/bin/stop doesn't block your script seem to work.

          As muru suggested you could skip the $count variable and use the builtin $SECONDS. This would lead to code like this:



          /opt/abc/bin/stop
          # Wait for 10 second
          sleep 10
          # Number of seconds to wait
          WAIT_SECONDS=300
          while pgrep abc_test > /dev/null
          do
          echo "server is still running. Seconds: $SECONDS"
          # Wait for one second
          sleep 1
          # Have we exceeded $WAIT_SECONDS? If so exit the loop
          if [ $SECONDS -gt $WAIT_SECONDS ]; then
          break
          fi
          done


          In case /opt/abc/bin/stop DOES block, just call it in the background like:



          /opt/abc/bin/stop &






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Dec 17 '15 at 2:35









          muru

          34.4k579149




          34.4k579149










          answered Dec 17 '15 at 1:09









          SleepProgger

          36518




          36518






















              up vote
              0
              down vote













              Write the Unix command for the following: to wait for a specified number of seconds before exit.





              share








              New contributor




              Tara Kumari is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.





















                up vote
                0
                down vote













                Write the Unix command for the following: to wait for a specified number of seconds before exit.





                share








                New contributor




                Tara Kumari is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.



















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  Write the Unix command for the following: to wait for a specified number of seconds before exit.





                  share








                  New contributor




                  Tara Kumari is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  Write the Unix command for the following: to wait for a specified number of seconds before exit.






                  share








                  New contributor




                  Tara Kumari is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.








                  share


                  share






                  New contributor




                  Tara Kumari is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  answered 7 mins ago









                  Tara Kumari

                  1




                  1




                  New contributor




                  Tara Kumari is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.





                  New contributor





                  Tara Kumari is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.






                  Tara Kumari is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f249877%2fwait-for-predefined-time-before-exit-the-script%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

                      How to check contact read email or not when send email to Individual?

                      Displaying single band from multi-band raster using QGIS

                      How many registers does an x86_64 CPU actually have?