Thread: how to do batch processing with time out?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Me -=SoKrA=-'s Avatar
    Join Date
    Oct 2002
    Location
    Europe
    Posts
    448
    You could run the algorithm as a cronjob, and then send a killall an hour later to kill it. It's not the most elegant solution, but unless you can be interrupted because of a time limit you woulnd't be able to do it.

    You could also add the timeout in the actual algorithm. Every time you finish processing a set/piece you could check to see how long it has passed. If more than an hour has passed, abort() or exit() the program.
    SoKrA-BTS "Judge not the program I made, but the one I've yet to code"
    I say what I say, I mean what I mean.
    IDE: emacs + make + gcc and proud of it.

  2. #2
    Registered User
    Join Date
    Dec 2004
    Posts
    163
    but cronjob does not have a time out.

    i found a code for shellscript on the internet:
    Code:
    my_command & sleep 5 
       kill -0 $! && kill $!
    seems like I can run my algorithm, put it to sleep, and then kill it. but what does the parameters for kill mean? does anyone knows?

    Someone posted this code below and I seek help from other forum. You guys know what it means? Sorry, I'm a newbie in shell scipt... =)
    Code:
    #!/bin/bash
    
    # Signal handler for SIGALRM.
    #
    on_timeout()
    {
    	echo "Time out! Exit..."
    	exit 3
    }
    
    # Timer function.
    #  - First argument:  PID to send SIGALRM to.
    #  - Second argument: time-out in seconds.
    #
    timeout_timer()
    {
    	sleep $2
    	kill -s SIGALRM $1 >/dev/null 2>&1
    }
    
    # Set signal-handler function
    #
    trap 'on_timeout' ALRM
    
    
    # Start time-out timer in the background.
    # Set time-out to 4 seconds.
    #
    timeout_timer $$ 4 &
    
    # Main part of the script, which will time out.
    # Here: just looping forever, or until time-out
    #
    SEC=0
    while true ; do
            sleep 1
            SEC=$((SEC+1))
            echo "I'm PID# $$, and I'm alive for $SEC seconds now!"
    done
    
    # We never get here.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Weird Times I'm getting
    By afflictedd2 in forum Linux Programming
    Replies: 8
    Last Post: 07-23-2008, 07:18 AM
  2. Journey time prog 1 minute wrong
    By mike_g in forum C Programming
    Replies: 4
    Last Post: 10-12-2006, 03:41 AM
  3. can c++ do batch file processing?
    By franziss in forum C++ Programming
    Replies: 11
    Last Post: 10-02-2006, 06:44 AM
  4. processing more than one file at a time
    By Moony in forum C Programming
    Replies: 4
    Last Post: 06-24-2006, 12:52 AM
  5. calculating user time and time elapsed
    By Neildadon in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2003, 06:00 PM