How to run a program for a fixed period of time?












6















I am looking for a method built-in to ubuntu that will allow me to run a script or program or whatever for a fixed period of time.



I found a program that does this in a way I like, but the package is unavailable for Ubuntu. In any case, I was hoping for something built-in.



The only thing i can think of is to get the current time and set a cron job 30 minutes from 'now' that will kill the program. I was hoping there was a way to do this without setting up a script, but if I need to - it wont be the end of the world. After the 30 minute interval I would like to put my laptop in a sleep mode, but this can be separate from the timer thing.



Thanks in advance.










share|improve this question





























    6















    I am looking for a method built-in to ubuntu that will allow me to run a script or program or whatever for a fixed period of time.



    I found a program that does this in a way I like, but the package is unavailable for Ubuntu. In any case, I was hoping for something built-in.



    The only thing i can think of is to get the current time and set a cron job 30 minutes from 'now' that will kill the program. I was hoping there was a way to do this without setting up a script, but if I need to - it wont be the end of the world. After the 30 minute interval I would like to put my laptop in a sleep mode, but this can be separate from the timer thing.



    Thanks in advance.










    share|improve this question



























      6












      6








      6


      1






      I am looking for a method built-in to ubuntu that will allow me to run a script or program or whatever for a fixed period of time.



      I found a program that does this in a way I like, but the package is unavailable for Ubuntu. In any case, I was hoping for something built-in.



      The only thing i can think of is to get the current time and set a cron job 30 minutes from 'now' that will kill the program. I was hoping there was a way to do this without setting up a script, but if I need to - it wont be the end of the world. After the 30 minute interval I would like to put my laptop in a sleep mode, but this can be separate from the timer thing.



      Thanks in advance.










      share|improve this question
















      I am looking for a method built-in to ubuntu that will allow me to run a script or program or whatever for a fixed period of time.



      I found a program that does this in a way I like, but the package is unavailable for Ubuntu. In any case, I was hoping for something built-in.



      The only thing i can think of is to get the current time and set a cron job 30 minutes from 'now' that will kill the program. I was hoping there was a way to do this without setting up a script, but if I need to - it wont be the end of the world. After the 30 minute interval I would like to put my laptop in a sleep mode, but this can be separate from the timer thing.



      Thanks in advance.







      command-line scripts timeout






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Sep 7 '13 at 20:48









      Braiam

      52.2k20137222




      52.2k20137222










      asked May 26 '11 at 16:50









      Teque5Teque5

      207139




      207139






















          4 Answers
          4






          active

          oldest

          votes


















          12














          I've just wrote the following and it seems to work:



          ping google.com& PID=$!; sleep 3; kill $PID


          Of course you should substitute ping with the command you want to run and 3 with a timeout in seconds. Let me know if you need a more detailed explanation on how it works.






          share|improve this answer


























          • This exits in a syntax error after '&;'. For example just running 'vlc &;' gets the same error; but I see what you are trying to do...

            – Teque5
            May 26 '11 at 18:54













          • Sorry about that, I tested it on Zsh instead of Bash. It works when you remote the semicolon, corrected.

            – Adam Byrtek
            May 26 '11 at 18:57













          • Well when i remove the semicolon it doesn't get the PID. I get: 'bash: kill: $!: arguments must be process or job IDs'. It does TRY to kill it after 10 seconds though...

            – Teque5
            May 26 '11 at 19:00













          • Just remove the backslash before !. Once more sorry for the confusion.

            – Adam Byrtek
            May 26 '11 at 19:09





















          14














          Why not use /usr/bin/timeout?



          $ timeout --help
          Usage: timeout [OPTION] DURATION COMMAND [ARG]...
          or: timeout [OPTION]
          Start COMMAND, and kill it if still running after DURATION.





          share|improve this answer































            3














            A simple (and not much tested) version of hatimerun:



            #!/bin/sh

            help(){
            echo "Usage" >&2
            echo " $0 -t TIME COMMAND" >&2
            exit 1
            }

            TEMP=`getopt -o t: -n "$0" -- "$@"`

            if [ $? != 0 ] ; then
            help
            fi

            eval set -- "$TEMP"

            while true ; do
            case "$1" in
            -t) timeout=$2; shift 2;;
            --) shift; break;;
            esac
            done

            if [ -z "$timeout" ]; then
            help
            fi

            cmd="$*"

            $cmd&
            echo "kill $!" | at now+$timeout


            See the manpage of at for how to specify the time. Like with at the minimum time resolution is 1 minute.



            Another way is to use upstart instead of this script.






            share|improve this answer

































              0














              Using Perl:



              perl -e "alarm 5; exec @ARGV" systemctl list-timers


              Using timelimit command:



              sudo apt install timelimit    
              timelimit -t5 node app.js


              Using timeout command:



              timeout -sHUP time command






              share|improve this answer





















              • 2





                That's already in askubuntu.com/a/102328/760903

                – Olorin
                Feb 7 at 5:27








              • 1





                @Olorin now the post is edited by OP including another solutions as well as a sample of the timeout command.

                – αғsнιη
                Feb 11 at 5:48













              • @αғsнιη Do you mind answering a short query about timeout and timelimit command? When I used timeout and timelimit command, it gave me error like this after it exited from that timeout period. myname@myname:~$ myname@myname:~$ myname@myname:~$ myname@myname:~$ For each enter, it kept making in the same line instead of going to another line. Then I had to exit from the session and login again. But, perl command worked fine without giving any error.

                – Rakib Fiha
                Feb 11 at 7:00













              • sorry, I have no idea what's happening there

                – αғsнιη
                Feb 12 at 5:22











              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%2f45363%2fhow-to-run-a-program-for-a-fixed-period-of-time%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              4 Answers
              4






              active

              oldest

              votes








              4 Answers
              4






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              12














              I've just wrote the following and it seems to work:



              ping google.com& PID=$!; sleep 3; kill $PID


              Of course you should substitute ping with the command you want to run and 3 with a timeout in seconds. Let me know if you need a more detailed explanation on how it works.






              share|improve this answer


























              • This exits in a syntax error after '&;'. For example just running 'vlc &;' gets the same error; but I see what you are trying to do...

                – Teque5
                May 26 '11 at 18:54













              • Sorry about that, I tested it on Zsh instead of Bash. It works when you remote the semicolon, corrected.

                – Adam Byrtek
                May 26 '11 at 18:57













              • Well when i remove the semicolon it doesn't get the PID. I get: 'bash: kill: $!: arguments must be process or job IDs'. It does TRY to kill it after 10 seconds though...

                – Teque5
                May 26 '11 at 19:00













              • Just remove the backslash before !. Once more sorry for the confusion.

                – Adam Byrtek
                May 26 '11 at 19:09


















              12














              I've just wrote the following and it seems to work:



              ping google.com& PID=$!; sleep 3; kill $PID


              Of course you should substitute ping with the command you want to run and 3 with a timeout in seconds. Let me know if you need a more detailed explanation on how it works.






              share|improve this answer


























              • This exits in a syntax error after '&;'. For example just running 'vlc &;' gets the same error; but I see what you are trying to do...

                – Teque5
                May 26 '11 at 18:54













              • Sorry about that, I tested it on Zsh instead of Bash. It works when you remote the semicolon, corrected.

                – Adam Byrtek
                May 26 '11 at 18:57













              • Well when i remove the semicolon it doesn't get the PID. I get: 'bash: kill: $!: arguments must be process or job IDs'. It does TRY to kill it after 10 seconds though...

                – Teque5
                May 26 '11 at 19:00













              • Just remove the backslash before !. Once more sorry for the confusion.

                – Adam Byrtek
                May 26 '11 at 19:09
















              12












              12








              12







              I've just wrote the following and it seems to work:



              ping google.com& PID=$!; sleep 3; kill $PID


              Of course you should substitute ping with the command you want to run and 3 with a timeout in seconds. Let me know if you need a more detailed explanation on how it works.






              share|improve this answer















              I've just wrote the following and it seems to work:



              ping google.com& PID=$!; sleep 3; kill $PID


              Of course you should substitute ping with the command you want to run and 3 with a timeout in seconds. Let me know if you need a more detailed explanation on how it works.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Sep 7 '13 at 21:09









              Eliah Kagan

              82.5k22227369




              82.5k22227369










              answered May 26 '11 at 17:18









              Adam ByrtekAdam Byrtek

              8,32712526




              8,32712526













              • This exits in a syntax error after '&;'. For example just running 'vlc &;' gets the same error; but I see what you are trying to do...

                – Teque5
                May 26 '11 at 18:54













              • Sorry about that, I tested it on Zsh instead of Bash. It works when you remote the semicolon, corrected.

                – Adam Byrtek
                May 26 '11 at 18:57













              • Well when i remove the semicolon it doesn't get the PID. I get: 'bash: kill: $!: arguments must be process or job IDs'. It does TRY to kill it after 10 seconds though...

                – Teque5
                May 26 '11 at 19:00













              • Just remove the backslash before !. Once more sorry for the confusion.

                – Adam Byrtek
                May 26 '11 at 19:09





















              • This exits in a syntax error after '&;'. For example just running 'vlc &;' gets the same error; but I see what you are trying to do...

                – Teque5
                May 26 '11 at 18:54













              • Sorry about that, I tested it on Zsh instead of Bash. It works when you remote the semicolon, corrected.

                – Adam Byrtek
                May 26 '11 at 18:57













              • Well when i remove the semicolon it doesn't get the PID. I get: 'bash: kill: $!: arguments must be process or job IDs'. It does TRY to kill it after 10 seconds though...

                – Teque5
                May 26 '11 at 19:00













              • Just remove the backslash before !. Once more sorry for the confusion.

                – Adam Byrtek
                May 26 '11 at 19:09



















              This exits in a syntax error after '&;'. For example just running 'vlc &;' gets the same error; but I see what you are trying to do...

              – Teque5
              May 26 '11 at 18:54







              This exits in a syntax error after '&;'. For example just running 'vlc &;' gets the same error; but I see what you are trying to do...

              – Teque5
              May 26 '11 at 18:54















              Sorry about that, I tested it on Zsh instead of Bash. It works when you remote the semicolon, corrected.

              – Adam Byrtek
              May 26 '11 at 18:57







              Sorry about that, I tested it on Zsh instead of Bash. It works when you remote the semicolon, corrected.

              – Adam Byrtek
              May 26 '11 at 18:57















              Well when i remove the semicolon it doesn't get the PID. I get: 'bash: kill: $!: arguments must be process or job IDs'. It does TRY to kill it after 10 seconds though...

              – Teque5
              May 26 '11 at 19:00







              Well when i remove the semicolon it doesn't get the PID. I get: 'bash: kill: $!: arguments must be process or job IDs'. It does TRY to kill it after 10 seconds though...

              – Teque5
              May 26 '11 at 19:00















              Just remove the backslash before !. Once more sorry for the confusion.

              – Adam Byrtek
              May 26 '11 at 19:09







              Just remove the backslash before !. Once more sorry for the confusion.

              – Adam Byrtek
              May 26 '11 at 19:09















              14














              Why not use /usr/bin/timeout?



              $ timeout --help
              Usage: timeout [OPTION] DURATION COMMAND [ARG]...
              or: timeout [OPTION]
              Start COMMAND, and kill it if still running after DURATION.





              share|improve this answer




























                14














                Why not use /usr/bin/timeout?



                $ timeout --help
                Usage: timeout [OPTION] DURATION COMMAND [ARG]...
                or: timeout [OPTION]
                Start COMMAND, and kill it if still running after DURATION.





                share|improve this answer


























                  14












                  14








                  14







                  Why not use /usr/bin/timeout?



                  $ timeout --help
                  Usage: timeout [OPTION] DURATION COMMAND [ARG]...
                  or: timeout [OPTION]
                  Start COMMAND, and kill it if still running after DURATION.





                  share|improve this answer













                  Why not use /usr/bin/timeout?



                  $ timeout --help
                  Usage: timeout [OPTION] DURATION COMMAND [ARG]...
                  or: timeout [OPTION]
                  Start COMMAND, and kill it if still running after DURATION.






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Feb 7 '12 at 21:24









                  waltinatorwaltinator

                  22.6k74169




                  22.6k74169























                      3














                      A simple (and not much tested) version of hatimerun:



                      #!/bin/sh

                      help(){
                      echo "Usage" >&2
                      echo " $0 -t TIME COMMAND" >&2
                      exit 1
                      }

                      TEMP=`getopt -o t: -n "$0" -- "$@"`

                      if [ $? != 0 ] ; then
                      help
                      fi

                      eval set -- "$TEMP"

                      while true ; do
                      case "$1" in
                      -t) timeout=$2; shift 2;;
                      --) shift; break;;
                      esac
                      done

                      if [ -z "$timeout" ]; then
                      help
                      fi

                      cmd="$*"

                      $cmd&
                      echo "kill $!" | at now+$timeout


                      See the manpage of at for how to specify the time. Like with at the minimum time resolution is 1 minute.



                      Another way is to use upstart instead of this script.






                      share|improve this answer






























                        3














                        A simple (and not much tested) version of hatimerun:



                        #!/bin/sh

                        help(){
                        echo "Usage" >&2
                        echo " $0 -t TIME COMMAND" >&2
                        exit 1
                        }

                        TEMP=`getopt -o t: -n "$0" -- "$@"`

                        if [ $? != 0 ] ; then
                        help
                        fi

                        eval set -- "$TEMP"

                        while true ; do
                        case "$1" in
                        -t) timeout=$2; shift 2;;
                        --) shift; break;;
                        esac
                        done

                        if [ -z "$timeout" ]; then
                        help
                        fi

                        cmd="$*"

                        $cmd&
                        echo "kill $!" | at now+$timeout


                        See the manpage of at for how to specify the time. Like with at the minimum time resolution is 1 minute.



                        Another way is to use upstart instead of this script.






                        share|improve this answer




























                          3












                          3








                          3







                          A simple (and not much tested) version of hatimerun:



                          #!/bin/sh

                          help(){
                          echo "Usage" >&2
                          echo " $0 -t TIME COMMAND" >&2
                          exit 1
                          }

                          TEMP=`getopt -o t: -n "$0" -- "$@"`

                          if [ $? != 0 ] ; then
                          help
                          fi

                          eval set -- "$TEMP"

                          while true ; do
                          case "$1" in
                          -t) timeout=$2; shift 2;;
                          --) shift; break;;
                          esac
                          done

                          if [ -z "$timeout" ]; then
                          help
                          fi

                          cmd="$*"

                          $cmd&
                          echo "kill $!" | at now+$timeout


                          See the manpage of at for how to specify the time. Like with at the minimum time resolution is 1 minute.



                          Another way is to use upstart instead of this script.






                          share|improve this answer















                          A simple (and not much tested) version of hatimerun:



                          #!/bin/sh

                          help(){
                          echo "Usage" >&2
                          echo " $0 -t TIME COMMAND" >&2
                          exit 1
                          }

                          TEMP=`getopt -o t: -n "$0" -- "$@"`

                          if [ $? != 0 ] ; then
                          help
                          fi

                          eval set -- "$TEMP"

                          while true ; do
                          case "$1" in
                          -t) timeout=$2; shift 2;;
                          --) shift; break;;
                          esac
                          done

                          if [ -z "$timeout" ]; then
                          help
                          fi

                          cmd="$*"

                          $cmd&
                          echo "kill $!" | at now+$timeout


                          See the manpage of at for how to specify the time. Like with at the minimum time resolution is 1 minute.



                          Another way is to use upstart instead of this script.







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Jul 20 '18 at 21:45









                          David Foerster

                          28.4k1366111




                          28.4k1366111










                          answered May 26 '11 at 17:50









                          Florian DieschFlorian Diesch

                          65.5k16164181




                          65.5k16164181























                              0














                              Using Perl:



                              perl -e "alarm 5; exec @ARGV" systemctl list-timers


                              Using timelimit command:



                              sudo apt install timelimit    
                              timelimit -t5 node app.js


                              Using timeout command:



                              timeout -sHUP time command






                              share|improve this answer





















                              • 2





                                That's already in askubuntu.com/a/102328/760903

                                – Olorin
                                Feb 7 at 5:27








                              • 1





                                @Olorin now the post is edited by OP including another solutions as well as a sample of the timeout command.

                                – αғsнιη
                                Feb 11 at 5:48













                              • @αғsнιη Do you mind answering a short query about timeout and timelimit command? When I used timeout and timelimit command, it gave me error like this after it exited from that timeout period. myname@myname:~$ myname@myname:~$ myname@myname:~$ myname@myname:~$ For each enter, it kept making in the same line instead of going to another line. Then I had to exit from the session and login again. But, perl command worked fine without giving any error.

                                – Rakib Fiha
                                Feb 11 at 7:00













                              • sorry, I have no idea what's happening there

                                – αғsнιη
                                Feb 12 at 5:22
















                              0














                              Using Perl:



                              perl -e "alarm 5; exec @ARGV" systemctl list-timers


                              Using timelimit command:



                              sudo apt install timelimit    
                              timelimit -t5 node app.js


                              Using timeout command:



                              timeout -sHUP time command






                              share|improve this answer





















                              • 2





                                That's already in askubuntu.com/a/102328/760903

                                – Olorin
                                Feb 7 at 5:27








                              • 1





                                @Olorin now the post is edited by OP including another solutions as well as a sample of the timeout command.

                                – αғsнιη
                                Feb 11 at 5:48













                              • @αғsнιη Do you mind answering a short query about timeout and timelimit command? When I used timeout and timelimit command, it gave me error like this after it exited from that timeout period. myname@myname:~$ myname@myname:~$ myname@myname:~$ myname@myname:~$ For each enter, it kept making in the same line instead of going to another line. Then I had to exit from the session and login again. But, perl command worked fine without giving any error.

                                – Rakib Fiha
                                Feb 11 at 7:00













                              • sorry, I have no idea what's happening there

                                – αғsнιη
                                Feb 12 at 5:22














                              0












                              0








                              0







                              Using Perl:



                              perl -e "alarm 5; exec @ARGV" systemctl list-timers


                              Using timelimit command:



                              sudo apt install timelimit    
                              timelimit -t5 node app.js


                              Using timeout command:



                              timeout -sHUP time command






                              share|improve this answer















                              Using Perl:



                              perl -e "alarm 5; exec @ARGV" systemctl list-timers


                              Using timelimit command:



                              sudo apt install timelimit    
                              timelimit -t5 node app.js


                              Using timeout command:



                              timeout -sHUP time command







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Feb 11 at 3:52

























                              answered Feb 7 at 5:27









                              Rakib FihaRakib Fiha

                              195




                              195








                              • 2





                                That's already in askubuntu.com/a/102328/760903

                                – Olorin
                                Feb 7 at 5:27








                              • 1





                                @Olorin now the post is edited by OP including another solutions as well as a sample of the timeout command.

                                – αғsнιη
                                Feb 11 at 5:48













                              • @αғsнιη Do you mind answering a short query about timeout and timelimit command? When I used timeout and timelimit command, it gave me error like this after it exited from that timeout period. myname@myname:~$ myname@myname:~$ myname@myname:~$ myname@myname:~$ For each enter, it kept making in the same line instead of going to another line. Then I had to exit from the session and login again. But, perl command worked fine without giving any error.

                                – Rakib Fiha
                                Feb 11 at 7:00













                              • sorry, I have no idea what's happening there

                                – αғsнιη
                                Feb 12 at 5:22














                              • 2





                                That's already in askubuntu.com/a/102328/760903

                                – Olorin
                                Feb 7 at 5:27








                              • 1





                                @Olorin now the post is edited by OP including another solutions as well as a sample of the timeout command.

                                – αғsнιη
                                Feb 11 at 5:48













                              • @αғsнιη Do you mind answering a short query about timeout and timelimit command? When I used timeout and timelimit command, it gave me error like this after it exited from that timeout period. myname@myname:~$ myname@myname:~$ myname@myname:~$ myname@myname:~$ For each enter, it kept making in the same line instead of going to another line. Then I had to exit from the session and login again. But, perl command worked fine without giving any error.

                                – Rakib Fiha
                                Feb 11 at 7:00













                              • sorry, I have no idea what's happening there

                                – αғsнιη
                                Feb 12 at 5:22








                              2




                              2





                              That's already in askubuntu.com/a/102328/760903

                              – Olorin
                              Feb 7 at 5:27







                              That's already in askubuntu.com/a/102328/760903

                              – Olorin
                              Feb 7 at 5:27






                              1




                              1





                              @Olorin now the post is edited by OP including another solutions as well as a sample of the timeout command.

                              – αғsнιη
                              Feb 11 at 5:48







                              @Olorin now the post is edited by OP including another solutions as well as a sample of the timeout command.

                              – αғsнιη
                              Feb 11 at 5:48















                              @αғsнιη Do you mind answering a short query about timeout and timelimit command? When I used timeout and timelimit command, it gave me error like this after it exited from that timeout period. myname@myname:~$ myname@myname:~$ myname@myname:~$ myname@myname:~$ For each enter, it kept making in the same line instead of going to another line. Then I had to exit from the session and login again. But, perl command worked fine without giving any error.

                              – Rakib Fiha
                              Feb 11 at 7:00







                              @αғsнιη Do you mind answering a short query about timeout and timelimit command? When I used timeout and timelimit command, it gave me error like this after it exited from that timeout period. myname@myname:~$ myname@myname:~$ myname@myname:~$ myname@myname:~$ For each enter, it kept making in the same line instead of going to another line. Then I had to exit from the session and login again. But, perl command worked fine without giving any error.

                              – Rakib Fiha
                              Feb 11 at 7:00















                              sorry, I have no idea what's happening there

                              – αғsнιη
                              Feb 12 at 5:22





                              sorry, I have no idea what's happening there

                              – αғsнιη
                              Feb 12 at 5:22


















                              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%2f45363%2fhow-to-run-a-program-for-a-fixed-period-of-time%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

                              How to make a Squid Proxy server?

                              Is this a new Fibonacci Identity?

                              19世紀