-
Notifications
You must be signed in to change notification settings - Fork 0
/
swap_guard.sh
executable file
·41 lines (31 loc) · 993 Bytes
/
swap_guard.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
#!/bin/bash
#
# Watch a command and terminate it if it starts to swap.
# Useful for memory intensive scientific calculations,
# which sometimes do not fit into RAM and cause the
# system to slow down until it is unusable.
#
# Written by Christian Zielinski <[email protected]>
#
# The command to watch, replace by "" to watch all commands
WATCH_COMMAND="python"
# Waiting period between consecutive checks
WATCH_WAIT="10s"
# Main loop
while true; do
echo
date
for PID in `ps -u "${USER}" | grep "${WATCH_COMMAND}" | awk '{print $1}'`; do
prod_dir="/proc/$PID/smaps"
total_swap=0
for swap in `grep "Swap" "${prod_dir}" 2>/dev/null | awk '{print $2}'`; do
let total_swap=$total_swap+$swap
done
echo "PID=${PID} | Swap used: ${total_swap}"
if [ "${total_swap}" -gt "0" ]; then
echo "WARNING: Swapping, killing process ..."
kill "${PID}"
fi
done
sleep "${WATCH_WAIT}"
done