How to sleep for 1 second between each xargs team?

For example, if I perform

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

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

How can I change the shell command?

+11
linux bash shell xargs
Mar 01 '13 at 8:11
source share
3 answers

You can use the following syntax:

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

Be careful with spaces and semicolons. After each command, a semicolon is required between the brackets (even after the last).

+30
Mar 01 '13 at 8:18
source share

Replace echo with some shell script named sleepecho containing

  #!/bin/sh sleep 1 echo $* 
0
Mar 01 '13 at 8:18
source share

If your awk supports it:

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

or skip awk and xargs in general

 ps aux | while read -r user rest; echo $user sleep 1; done 
0
Mar 01 '13 at 14:23
source share



All Articles