0

I wanted to simulate execution time of certain scripts for which I found sleep NUMBER does exactly what I want.
In my scenario I needed something like

sleep 5 | command | sleep 5 ...

But it behaved strangely so I've tested sleeps alone, and I was surprised that
This takes 10 seconds sleep 10 | sleep 5
and this also takes 10 seconds sleep 5 | sleep 10

I even tried sleep 1 | sleep in case sleep was listening to standard input stdin

Only thing I got working is when I was looking on how to force stdout as argument (with xargs)

sleep 3; echo 3 | xargs sleep; echo "finished"

But since I need to time the whole execution I had to do

time -p (sleep 3; echo 3) | (xargs sleep; echo "finished")

Hoe to pipe sleeps? If there is a better way, I'd still ike to know why sleep 1 | sleep 1 isn't working in the first place?

10
  • 1
    Does this answer your question? In what order do piped commands run?
    – muru
    Commented Feb 2, 2021 at 18:53
  • And also pretty much the same thing in unix.stackexchange.com/questions/594070/…
    – muru
    Commented Feb 2, 2021 at 18:53
  • @muru it only explains why isn't it working or it works the way it does, but does not answer how to properly pipe sleeps so they'd run in serial mode,
    – jave.web
    Commented Feb 2, 2021 at 19:21
  • 3
    There is no serial mode in pipes. They run in parallel. That is the point. If you want them to run serially, run them serially and use temp files or something for passing around output.
    – muru
    Commented Feb 2, 2021 at 19:22
  • Also, it's unclear why you do something like (sleep 3; echo 3 | (xargs sleep; echo finished) - what is the sleep 3 doing in the left side, and why echo | xargs? If you want to delay the second echo, | (sleep 3; echo finished) is enough.
    – muru
    Commented Feb 2, 2021 at 19:28

1 Answer 1

3

I think you want to separate with ; instead of |

sleep 10; echo hi; sleep 5

This will wait 10 seconds, echo hi, wait 5 seconds, then exit.

I think many of us are confused why you want to pipe the output of sleep (which doesn’t really have meaningful output) at all. To tell the truth, the idea of piping sleep, while possible, had never occurred to me.

; is the syntax for running commands serially.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .