Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem when executing with sleep in a script #7

Open
yogeshjawale opened this issue Jun 30, 2015 · 1 comment
Open

Problem when executing with sleep in a script #7

yogeshjawale opened this issue Jun 30, 2015 · 1 comment

Comments

@yogeshjawale
Copy link

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.

@descrip
Copy link

descrip commented Sep 2, 2016

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants