-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrun.sh
executable file
·164 lines (134 loc) · 3.69 KB
/
run.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env bash
set -eo pipefail
if [[ "$EUID" -ne 0 ]]
then echo "Please run as root"
exit
fi
promode=false
skillmode=false
numberofprocess=1
while (( "$#" )); do
case "$1" in
-p|--pro)
promode=true
shift
;;
-s|--skill)
skillmode=true
numberofprocess=2
shift
;;
--) # end argument parsing
shift
break
;;
-*|--*=) # unsupported flags
echo "Error: Unsupported flag $1"
exit 1
;;
*) # preserve positional arguments
shift
;;
esac
done
function intro() {
cat << "EOF"
________ __
| \ | \
| $$$$$$$$ _______ ____| $$ ______ ______ ______ ____ ______
| $$__ | \ / $$ / \ | \ | \ \ / \
| $$ \ | $$$$$$$\| $$$$$$$| $$$$$$\ \$$$$$$\| $$$$$$\$$$$\| $$$$$$\
| $$$$$ | $$ | $$| $$ | $$| $$ | $$ / $$| $$ | $$ | $$| $$ $$
| $$_____ | $$ | $$| $$__| $$| $$__| $$| $$$$$$$| $$ | $$ | $$| $$$$$$$$
| $$ \| $$ | $$ \$$ $$ \$$ $$ \$$ $$| $$ | $$ | $$ \$$ \
\$$$$$$$$ \$$ \$$ \$$$$$$$ _\$$$$$$$ \$$$$$$$ \$$ \$$ \$$ \$$$$$$$
| \__| $$
\$$ $$
\$$$$$$
v0.2
EOF
if [[ "$promode" = true ]]; then
echo "with pro-mode enabled"
else
echo "you can enable pro-mode (incl. kernel threads) by starting with -p or --pro"
fi
echo
if [[ "$skillmode" = true ]]; then
echo "with skill-mode enabled"
else
echo "you can enable skill-mode (select one out of 3) by starting with -s or --skill"
fi
echo
read -p "Press Enter"
# clear screen
printf "\033c"
}
function rules() {
cat << "EOF"
THE RULES
1. Prepare a drink
2. The goal is to kill a process without killing the machine.
EOF
read -p "Press Enter to start."
# clear screen
printf "\033c"
}
intro
rules
while true; do
# clear screen
printf "\033c"
if [[ "$promode" = true ]]; then
processes=$(ps -AF --no-header)
else
processes=$(ps --ppid 2 --deselect -F --no-header)
fi
readarray -t r <<<"$processes"
result=()
for (( i = 0; i < ${numberofprocess}; ++i )); do
rand=$(( $RANDOM % ${#r[@]} ))
pid=$(echo ${r[$rand]} | awk '{print $2}')
process=$(echo ${r[$rand]} | awk '{for (i=11; i<NF; i++) printf $i " "; print $NF}')
result[$i]="${pid} '${process}'"
done
if [[ "$skillmode" = false ]]; then
echo "The following process has been selected"
echo "$pid $process"
read -p "Kill it with -9? (Y/n)" answer
case ${answer:0:1} in
n|N )
echo "You skipped. Drink!"
;;
* )
if ! kill -9 ${pid}; then
echo "Process was already dead. Drink!"
fi
;;
esac
else
echo "Select one of the following processes:"
for i in "${!result[@]}"; do
printf "%s\t%s\n" "$i" "${result[$i]}"
done
read -p "Which process to kill? (0-$(( $numberofprocess - 1 ))) " answer
case ${answer} in
0|1 )
pid=$(echo ${result[${answer}]} | awk '{print $1}')
if ! kill -9 ${pid}; then
echo "Process was already dead. Drink!"
fi
;;
* )
echo "Wrong input. Drink!"
;;
esac
fi
echo "Waiting 5s for the process to react".
sleep 5
if kill -0 ${pid} > /dev/null 2>&1; then
echo "Process did not die. Drink!"
else
echo "SUCCESS you killed ${pid}."
fi
read -p "Press enter to continue with next person."
done