timeout icon indicating copy to clipboard operation
timeout copied to clipboard

Problem when executing with sleep in a script

Open yogeshjawale opened this issue 9 years ago • 1 comments

Hi,

I don't know if this is an issue with this script, but when you execute a shell script with sleep {sleep_time} in it, then the timeout does not terminate the script/program.

I am attaching the code which i tried to run below,

!/bin/bash

i=1; while [ $i -lt 10000000 ] do echo "$i second" ; i=$[$i+1]; sleep 1; done

I also tried using it with java program using threads but same result with that as well.

Java code below, import java.util.; import java.text.; import java.math.; import java.io.; public class TestClass { public static void main(String[] args) { int i=1; try { while(true) { System.out.println(i++); //thread to sleep for the specified number of milliseconds Thread.sleep(1000); } } catch ( java.lang.InterruptedException ie) { System.out.println(ie); } } }

Please help me out here. Thanks in advance.

yogeshjawale avatar Jun 30 '15 09:06 yogeshjawale

I think this is because the script only measures cpu and sys time, and not real time. For example, running time sleep 5 gives me the following output (Ubuntu 16.04):

real    0m5.001s
user    0m0.000s
sys 0m0.000s

This means that sleep 5 took five seconds in real time, but to the sys and user, which is what the script measures, it seemed like it took no time at all.

If you need to limit the real time of a process, try the native bash timeout. For example, ./timeout -t 3 sleep 5 will not end the process and will take five seconds to complete, but ./timeout -t 3 timeout 3 sleep 5 will end the process within three seconds and return with status 124, from the middle timeout, signaling exceeded time.

./timeout calls the Perl script, and timeout calls the native bash utility.

descrip avatar Sep 02 '16 14:09 descrip