Skip to content

Instantly share code, notes, and snippets.

@timmc
Created December 27, 2019 16:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timmc/77517914b8c981d44d06e7ce3a676d25 to your computer and use it in GitHub Desktop.
Save timmc/77517914b8c981d44d06e7ce3a676d25 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Call a script in a tight loop from multiple processes.
# Run as `$0 master <#seconds> <#children> script-and-args...`.
# seconds and children control run length and concurrency
# Modes:
# master <seconds> <children>: Fork and run n child processes for m seconds
# child: Call on tight loop, indefinitely
# test: Call just once
myself="$0"
mode="$1"
shift
if [[ "$mode" = 'master' ]]; then
# Volume properties
seconds="$1"
shift
children="$1"
shift
fi
scriptAndArgs=( "$@" )
function do_call {
"${scriptAndArgs[@]}"
}
function emit_call {
echo `date -u +"%Y%m%d.%H%M%SZ"`$'\t'"`do_call`"
}
function run_indefinitely {
while true; do
emit_call
done
}
if [[ "$mode" = 'master' ]]; then
for (( child_id=0; child_id < $children; child_id++ )); do
timeout "${seconds}s" "$myself" child "${scriptAndArgs[@]}" &
done
wait
elif [[ "$mode" = 'child' ]]; then
run_indefinitely
elif [[ "$mode" = 'test' ]]; then
emit_call # just run once
else
echo "Bad mode"
exit 2
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment