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?

jave.web
  • 152
  • 1
    Does this answer your question? In what order do piped commands run? – muru Feb 02 '21 at 18:53
  • And also pretty much the same thing in https://unix.stackexchange.com/questions/594070/how-much-time-will-this-sleep-time-take-with-a-pipe – muru Feb 02 '21 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 Feb 02 '21 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 Feb 02 '21 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 Feb 02 '21 at 19:28
  • @muru that was an example, first wait 3 seconds before doing command 1, then send 3 with echo to next sleep which will sleep for those 3 seconds too and then command 2 – jave.web Feb 02 '21 at 19:36
  • I guess my issue was with time sleep 3; sleep 3 in itself works thanks, but time sleep 3; sleep 3 prints time only of the first command, so I guess solution to that according to those answers would be time (sleep 3; sleep 3) ? – jave.web Feb 02 '21 at 19:41
  • I'm still not clear as to why you want those many sleeps. Say you have a pipeline cmd1 | cmd2 | ... | cmdn, and you're faking cmd5, then only cmd5 needs to be (sleep; echo). Why do the rest need sleep? And if there are multiple faked commands, and they are adjacent, only the last needs a sleep. – muru Feb 02 '21 at 19:51
  • @muru there are multiple commands but only some of them are simulated and some of them should wait before starting another etc. it's mainly for some visual confirmations – jave.web Feb 02 '21 at 20:34
  • I don't know what you mean by "visual confirmations" but maybe you will find this answer useful. – Kamil Maciorowski Feb 03 '21 at 06:52

1 Answers1

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.

berndbausch
  • 3,557