Results 1 to 8 of 8

Thread: [SOLVED] How can I execute a program with a specified max. time (BASH)

  1. #1
    Join Date
    Jul 2005
    Beans
    1,535
    Distro
    Ubuntu 8.04 Hardy Heron

    [SOLVED] How can I execute a program with a specified max. time (BASH)

    Is there a way to provide a max execution time to a program when I launch it? Here is what I am doing now
    Code:
    ## c --> each students' name
    ## hw  --> name of executable
    MAX_NUM_HOURS="2" ## arbitrary upper bound
    
    ## time a detached program
    /usr/bin/time -f "%E real,%U user,%S sys"  --output="${c}_Time.txt" --append ./${hw} > {c}_output.txt & 
        
    #Kill program after running for specified hours
    DONE=0
    while [ $DONE -eq 0 ]; do
       /bin/sleep 1s # sleep for a second
       elapsedTime=`ps -e | grep ${hw} | awk '{print $3}'`
       if [ -z $elapsedTime ]; then # finished before time limit
          DONE=1             
       else # kill if running too long
          hours=`echo $elapsedTime | cut -d":" -f1`
          if (("$hours" >= "$MAX_NUM_HOURS")); then
             echo "Killing program, running too long"
             /bin/kill `pidof ${hw}` &> /dev/null
             DONE=1 
          fi
       fi
    done
    As you see, I run the students' code detached, and start a while loop monitoring the execution, killing their code if it is taking too long. Is there a better way?
    When I invented the Web, I didn't have to ask anyone's permission.
    ~Tim Berners-Lee on Net Neutrality
    -------------------------------------
    Visit the Ubuntu Programming IRC-channel at #ubuntu-programming (chat.freenode.net).

  2. #2
    Join Date
    Sep 2005
    Location
    Cedar Rapids, IA, USA
    Beans
    545
    Distro
    Xubuntu 12.04 Precise Pangolin

    Re: How can I execute a program with a specified max. time (BASH)

    My idea is similar to yours, but instead of using ps to get time, just keep calling /usr/bin/time to get the current time.

    I have no code for it and this was a quick think in a short break at work.
    #399784 | Ubuntu User #287
    *** If you're going to program, install the damn build-essential package ***
    There is no such thing as Ubuntu specific programming
    Save the electrons - if you quote, trim!

  3. #3
    Join Date
    Oct 2004
    Location
    Pennsylvania
    Beans
    1,698

    Re: How can I execute a program with a specified max. time (BASH)

    How about:

    Code:
    /program/to/run/in/background &
    CHILD_PID=$!
    #Sleep 2 hours (for example).
    sleep 2h 
    kill -HUP $CHILD_PID

  4. #4
    Join Date
    Jul 2005
    Beans
    1,535
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: How can I execute a program with a specified max. time (BASH)

    Quote Originally Posted by cwaldbieser View Post
    How about:<snip>
    This is a neat idea, but
    1) The program may very well end before the time limit is up, in which case your script will be sleeping for a long time (most likely not a problem).

    2) I did not show all the code in my script, but I have many student's submissions that I need to run. The while loop condition allows me to block the execution of subsequent students while another's submission is running.

    Catching the pid of the process is a really good idea though, and will clean up a lot of my while loop code. Thanks for that.
    When I invented the Web, I didn't have to ask anyone's permission.
    ~Tim Berners-Lee on Net Neutrality
    -------------------------------------
    Visit the Ubuntu Programming IRC-channel at #ubuntu-programming (chat.freenode.net).

  5. #5
    Join Date
    Oct 2004
    Location
    Pennsylvania
    Beans
    1,698

    Re: How can I execute a program with a specified max. time (BASH)

    Quote Originally Posted by hod139 View Post
    This is a neat idea, but
    1) The program may very well end before the time limit is up, in which case your script will be sleeping for a long time (most likely not a problem).

    2) I did not show all the code in my script, but I have many student's submissions that I need to run. The while loop condition allows me to block the execution of subsequent students while another's submission is running.

    Catching the pid of the process is a really good idea though, and will clean up a lot of my while loop code. Thanks for that.
    You could do something like:
    Code:
    #! /bin/bash
    
    MAX_TIME=1m
    for CHILD in list of progs to run; do
        $CHILD &
        CHILD_PID=$!
        bash -c "sleep $MAX_TIME; kill -HUP $CHILD_PID" &
        wait $CHILD_PID
    done
    Here you run the child in the background, but you'll wait for it (so you don't move onto the next program until it is done). You also spawn another background process that will wait for some amount of time and then signal the child to quit.

    You might end up with a bunch of processes waiting to kill non-existant other processes if all your student's programs run quickly, though. There might also be some problems if your students are trapping signals.

  6. #6
    Join Date
    Aug 2006
    Beans
    177
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Smile Re: How can I execute a program with a specified max. time (BASH)

    also you can pass kill -0 PID (for ask the state of the program) or some like that , go to #bash XD, they will send you to the FAQ taht they have (a very nice source of info on bash). O ya, I have the link here: http://wooledge.org/mywiki/BashFAQ
    Last edited by hecato; October 29th, 2007 at 11:41 PM.
    Las cosas que no se comparten se pierden. || Todo es lo que aparenta ser hasta que se descubre lo que en realidad es.
    How to become a hacker?

  7. #7
    Join Date
    May 2007
    Location
    Paris, France
    Beans
    927
    Distro
    Kubuntu 7.04 Feisty Fawn

    Re: How can I execute a program with a specified max. time (BASH)

    Quote Originally Posted by hecato View Post
    This FAQ is a killer. Thanks for the link!
    Not even tinfoil can save us now...

  8. #8
    Join Date
    Jul 2005
    Beans
    1,535
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: How can I execute a program with a specified max. time (BASH)

    Quote Originally Posted by hecato View Post
    also you can pass kill 0 or some like that, go to #bash XD, they will send you to the FAQ taht they have (a very nice source of info on bash). O ya, I have the link here: http://wooledge.org/mywiki/BashFAQ
    Awesome, thanks. FAQ 68 answers my question! That is a great link.
    When I invented the Web, I didn't have to ask anyone's permission.
    ~Tim Berners-Lee on Net Neutrality
    -------------------------------------
    Visit the Ubuntu Programming IRC-channel at #ubuntu-programming (chat.freenode.net).

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •