WDan
WDan

Reputation: 573

How to sleep for 1 second between each xargs command?

For example, if I execute

ps aux | awk '{print $1}' | xargs -I {} echo {}

I want to let the shell sleep for 1 second between each echo.

How can I change my shell command?

Upvotes: 29

Views: 17880

Answers (3)

chepner
chepner

Reputation: 532053

If your awk supports it:

ps aux | awk '{ system("sleep 1"); print $1 }' | xargs -I {} echo {}q

or skip awk and xargs altogether

ps aux | while read -r user rest; do
    echo ${user}
    sleep 1
done

Upvotes: 3

kamituel
kamituel

Reputation: 35960

You can use the following syntax:

ps aux | awk '{print $1}' | xargs -I % sh -c '{ echo %; sleep 1; }'

Be careful with spaces and semicolons though. After every command in between brackets, semicolon is required (even after the last one).

Upvotes: 53

Replace echo by some shell script named sleepecho containing

 #!/bin/sh
 sleep 1
 echo $*

Upvotes: 2

Related Questions