How to Sleep for 1 Second Between Each Xargs Command

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



Leave a reply



Submit