5
votes

The following command line call of timeout (which makes no sense, just for testing reason) does not work as expected. It waits 10 seconds and does not stop the command from working after 3 seconds. Why ?

timeout 3 ls | sleep 10
4
What, exactly, did you expect?FrankieTheKneeMan
@FrankieTheKneeMan I expected, timeout executes ls | sleep 10. Since ls | sleep 10 will take at least 10 seconds, timeout should stop it. But timeout does not.John Threepwood

4 Answers

18
votes

What your command is doing is running timeout 3 ls and piping its output to sleep 10. The sleep command is therefore not under the control of timeout and will always sleep for 10s.

Something like this would give the desired effect.

timeout 3 bash -c "ls | sleep 10"
3
votes

The 'ls' command shouldn't be taking 3 seconds to run. What I think is happening is you are saying (1) timeout on ls after 3 seconds (again this isn't happening since ls shouldn't take anywhere near 3 seconds to run), then (2) pipe the results into sleep 10 which does not need further arguments than the number you are giving it. Thus ls happens, timeout doesn't matter, and bash sleeps for 10 seconds.

2
votes

The only way I know how to get the effect you're after, is to put the piped commands into a separate file:

cat > script
ls | sleep 10
^D

timeout 3 sh script
1
votes

It is enough to set the timeout on the last command of the pipeline:

# Exits after 3 seconds with code 124
ls | timeout 3 sleep 10

# Exits after 1 second with code 0
ls | timeout 3 sleep 1