How to sleep for 1 second between each xargs command?
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).
Bash Xargs Sleep (Multiple Command Line Arguments)
The simplest solution would be to simply put the cli53
and sleep
calls in a script and use xargs
to execute the script.
If you don't want to do that you should be able to do what you were trying to do with this:
... | xargs ... /bin/sh -c '{ /usr/local/bin/cli53 ... "$@"; sleep 10; }' -
Make xargs execute the command once for each line of input
The following will only work if you do not have spaces in your input:
xargs -L 1
xargs --max-lines=1 # synonym for the -L option
from the man page:
-L max-lines
Use at most max-lines nonblank input lines per command line.
Trailing blanks cause an input line to be logically continued on
the next input line. Implies -x.
XARGS Long Arguments and Pause Between Commands
You can have xargs run any command you like; that certainly can be a command that pauses or runs another trigger you define between invocations.
So, if you're currently running:
xargs -0 doSomethingWithFiles
...you can replace it with:
xargs -0 bash -c '"$@"; sleep 5' _ doSomethingWithFiles
Add wait between parallel processes in bash
If the upload takes a random amount of time you just need the first 5 to start with a 1-5 second delay:
cat list | parallel -j5 [ {#} -lt 6 ] \&\& sleep {#}\; upload {}
print out a separator in between different outputs for a piped xargs command
You have various options. To add a separator as you describe, simply add it into your shell command: ; printf '---\n'
cat /tmp/ip_addresses | xargs -I {} sh -c "aws ec2 describe-instances --filter Name=private-ip-address,Values={} | jq '.Reservations[].Instances[].SecurityGroups[]'; printf '---\n'"
Or you could output a more structured object from the jq query: .Reservations[].Instances[]|{InstanceId,SecurityGroups}
cat /tmp/ip_addresses | xargs -I {} sh -c "aws ec2 describe-instances --filter Name=private-ip-address,Values={} | jq '.Reservations[].Instances[]|{InstanceId,SecurityGroups}'"
I don't have access to AWS to check but that should get you one object for each instance with only two fields - the InstanceId and the array of SecurityGroups.
Lastly if you want to output one object per security group but still label them with InstanceId you can do: .Reservations[].Instances[]|{InstanceId,SecurityGroup:.SecurityGroups[]}
cat /tmp/ip_addresses | xargs -I {} sh -c "aws ec2 describe-instances --filter Name=private-ip-address,Values={} | jq '.Reservations[].Instances[]|{InstanceId,SecurityGroup:.SecurityGroups[]}'"
(rm/xargs) purposefully slowing down the number of arguments passed to a command per second?
Pipe it to a while read ...
loop instead of xargs
, then add a 0.1-second sleep before each file, equaling ~10 files per second.
find 'xxxxxxx' -path "*xxxx/*xxxx" \
| sed -e 's/.*/"&"/' \
| while read file; do
echo "Removing: $file"
sleep 0.1
rm -r "$file"
done
Potentially better option: since you're interested in specific directories, you could make find
only match directories (using -type d
), then pause before each one of them for a bit longer, then delete all files without delay if not interrupted.
# though you'd probably have to adjust the -path pattern
find 'xxxxxxx' -path "*xxxx/*xxxx" -type d \
| sed -e 's/.*/"&"/' \
| while read directory; do
echo "About to remove directory: $directory"
sleep 2
rm -r "$directory"
done
xargs sh -c skipping the first argument
First argument to sh -c
or bash -c
is the name of the script i.e. $0
which is not printed when you use $@
:
Examples:
echo a b c d e f| xargs -n 3 bash -c 'echo "$0 $@"'
a b c
d e f
To fix this you can pass _
as dummy name of the script and then it should work:
echo a b c d e f| xargs -n 3 bash -c 'echo "$@"' _
a b c
d e f
It will work fine even with your sleep
example:
echo a b c d e f| xargs -n 3 bash -c 'echo "$@"; sleep 1' _
a b c
d e f
Related Topics
Error: Ld.So: Object Ld_Preload Cannot Be Preloaded: Ignored
Identify Other End of a Unix Domain Socket Connection
Importing Shapefiles in Postgresql in Linux Using Pgadmin 4
Recursive Copy of a Specific File Type Maintaining the File Structure in Unix/Linux
How to Clear the Line Number in Vim When Copying
Detecting Keyboard, Mouse Activity in Linux
Finding Process Count in Linux via Command Line
Meaning of Exit Status 1 Returned by Linux Command
Sys_Execve System Call from Assembly
How to Reinstall the Latest Cmake Version
Weird Sigsegv Segmentation Fault in Std::String::Assign() Method from Libstdc++.So.6
Changing Permissions of Files in a Directory Recursively
Moving Multiple Files Having Spaces in Name (Linux)
How to Delete Many 0 Byte Files in Linux
How to Automatically Start a Node.Js Application in Amazon Linux Ami on Aws
How to Run a Command in a Chroot Jail Not as Root and Without Sudo