How to sleep for 1 second between each xargs command?

。_饼干妹妹 提交于 2019-12-03 11:15:09

问题


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?


回答1:


You can use following syntax:

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

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




回答2:


Replace echo by some shell script named sleepecho containing

 #!/bin/sh
 sleep 1
 echo $*



回答3:


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;
    echo $user
    sleep 1;
done


来源:https://stackoverflow.com/questions/15153240/how-to-sleep-for-1-second-between-each-xargs-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!