-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfast-memgrep.sh
42 lines (33 loc) · 863 Bytes
/
fast-memgrep.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/bin/bash
green='\033[0;32m'
clear='\033[0m'
string="$1"
search_core() {
pid="$1"
echo "$(ps -p $pid -o comm= 2>/dev/null)($pid)"
gcore "$pid" > /dev/null 2>&1
if grep -q "$string" core."$pid" > /dev/null 2>&1; then
echo -e "${green}$(ps -p $pid -o comm=) $pid CONTAINS STRING '$string'${clear}"
fi
rm core."$pid" > /dev/null 2>&1
}
chunk() {
local list=("$@")
local chunk_size=$1
shift
for i in $(seq 0 $chunk_size $((${#list[@]} - 1))); do
echo "${list[@]:$i:$chunk_size}"
done
}
pids=($(ps axo pid))
num_cores=$(nproc)
chunk_size=$(( (${#pids[@]} + num_cores - 1) / num_cores ))
IFS=$'\n' read -rd '' -a chunks <<< "$(chunk $chunk_size "${pids[@]}")"
for pid_chunk in "${chunks[@]}"; do
{
for pid in $pid_chunk; do
search_core "$pid"
done
} &
done
wait