Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sleep for 1 second between each xargs command?

Tags:

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?

like image 533
WDan Avatar asked Mar 01 '13 08:03

WDan


People also ask

How do you sleep 10 seconds in shell script?

/bin/sleep is Linux or Unix command to delay for a specified amount of time. You can suspend the calling shell script for a specified time. For example, pause for 10 seconds or stop execution for 2 mintues. In other words, the sleep command pauses the execution on the next shell command for a given time.

What is sleep 1 in shell script?

Conclusion. The sleep command is one of the simplest Linux commands. It is used to pause the execution of the next command for a given amount of time.

How do you use sleep command?

sleep command is used to create a dummy job. A dummy job helps in delaying the execution. It takes time in seconds by default but a small suffix(s, m, h, d) can be added at the end to convert it into any other format. This command pauses the execution for an amount of time which is defined by NUMBER.

What xargs command does?

The xargs command builds and executes commands provided through the standard input. It takes the input and converts it into a command argument for another command. This feature is particularly useful in file management, where xargs is used in combination with rm , cp , mkdir , and other similar commands.


1 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 though. After every command in between brackets, semicolon is required (even after the last one).

like image 107
kamituel Avatar answered Sep 18 '22 09:09

kamituel