Skip to content

Instantly share code, notes, and snippets.

@drconopoima
Created March 24, 2021 23:45
Show Gist options
  • Save drconopoima/f5184277d793de7ee8e05d2e6895c7f4 to your computer and use it in GitHub Desktop.
Save drconopoima/f5184277d793de7ee8e05d2e6895c7f4 to your computer and use it in GitHub Desktop.
System loadtest bash shell script that recursively forks itself. Generate CPU/memory/swap stress.
#!/bin/bash
script_name=$(basename $0)
script_version='0.1.0'
printf "* %s (%s)\n" "${script_name}" "${script_version}"
printf "System loadtest script that recursively forks itself\n"
printf "For maximum load:\n"
printf "\t * Raise maximum number of open files fs.file-max\n"
printf "\t * Raise user number of files soft/hard limits in /etc/security/limits.conf\n"
printf "**** CTRL + C to stop ****\n"
parent_process=${1:-$$}
function pkill_forks {
set -o pipefail
current_process=$$;
if [[ ${current_process} == ${parent_process} ]]; then
timeout 3 kill -15 $(pgrep "${script_name}" | grep -v ${parent_process}) 2>/dev/null || kill -9 $(pgrep "${script_name}" | grep -v ${parent_process}) 2>/dev/null & PID=$!; wait "${PID}";
exit 0
else
{ set +x; } 2>/dev/null
exit 0
fi
}
trap pkill_forks SIGINT EXIT
$("$(CDPATH='' cd -- "$(dirname -- "$0")" && pwd -P)/${script_name}" "${parent_process}") 1>/dev/null 2>/dev/null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment