-
Notifications
You must be signed in to change notification settings - Fork 3
/
sandman.sh
executable file
·91 lines (77 loc) · 1.56 KB
/
sandman.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/bin/bash
# This came about when I was tyring to do maintenance on a production
# PostgreSQL database server and the IO generated by the maintenance was
# hurting performance. ionice didn't help, so I wrote a one-liner that would
# make the process sleep 4 out of every 5 seconds.
function usage {
cat << EOF
usage: $0 options
Make processes sleepy.
OPTIONS:
-h Help
-p Process ID
-r Ratio of sleep:awake (0-1, 1 being a coma)
-s Seconds of a sleep cycle
-v Verbose
Make process 5678 sleep for 5 seconds out of every 10:
$0 -p 5678 -r 0.5 -s 10 -v
EOF
}
# If verbose is enabled echo the given message
function v {
if [ $verbose -ne 0 ]
then
echo $*
fi
}
# Make sure the process stays awake after quitting
function ctrlc {
v Waking $pid before quitting
kill -sigcont $pid
exit
}
pid=
ratio=0.5
span=5
verbose=0
while getopts "hp:r:s:v" OPTION
do
case $OPTION in
h)
usage
exit
;;
p)
pid=$OPTARG
;;
r)
ratio=$OPTARG
;;
s)
span=$OPTARG
;;
v)
verbose=1
;;
esac
done
if [ -z $pid ]
then
echo "A process ID is required"
usage
exit 1
fi
sleep_span=$(awk "BEGIN { print $span * $ratio }")
wake_span=$(awk "BEGIN { print $span - $sleep_span }")
# handle control-c and make sure the script ends with the process awake
trap ctrlc SIGINT
# Sleep/Wake cycle
while true
do
v Sleeping \#$pid for $sleep_span seconds
kill -sigstop $pid
sleep $sleep_span
v WAKE UP \#$pid for $wake_span seconds
kill -sigcont $pid
sleep $wake_span
done