-
Notifications
You must be signed in to change notification settings - Fork 11
/
top-ps.sh
executable file
·103 lines (83 loc) · 2.71 KB
/
top-ps.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
#!/bin/bash
## Extract resources usage by processes from the ps command along with totals reported
SCRIPT_NAME=$0
RATE=5
PID=
######### Functions #########
errorExit () {
echo -e "\nERROR: $1\n"
exit 1
}
usage () {
cat << END_USAGE
${SCRIPT_NAME} - Show resources usage per processes ("simple" top)
Usage: ${SCRIPT_NAME} <options>
-p | --pid <process id> : Show data for provided process if only. Default: all processes
-o | --once : Output once. Default: infinite loop
-r | --rate : Refresh rate (seconds). Default: 5
-h | --help : Show this usage.
--no-headers : Don't print headers line.
Examples:
========
Show all: $ ${SCRIPT_NAME}
Show all (refresh rate 10 seconds): $ ${SCRIPT_NAME} --rate 10
Show once: $ ${SCRIPT_NAME} --once
Show for single pid: $ ${SCRIPT_NAME} --pid 1234
END_USAGE
exit 1
}
# Process command line options. See usage above for supported options
processOptions () {
while [[ $# -gt 0 ]]; do
case "$1" in
-p | --pid)
PID="$2"
shift 2
;;
-o | --once)
ONCE=true
shift 1
;;
-r | --rate)
RATE=$2
[ "${RATE}" -eq "${RATE}" ] 2> /dev/null || errorExit "Refresh rate must be an integer"
[ "${RATE}" -gt 0 ] 2> /dev/null || errorExit "Refresh rate must be an integer higher than 0"
shift 2
;;
-h | --help)
usage
exit 0
;;
*)
usage
;;
esac
done
}
# The main loop
main () {
# Check we have the ps executable
ps > /dev/null 2>&1 || errorExit "Missing the 'ps' command"
processOptions "$@"
local extra_args=
local input=
if [ -n "$PID" ]; then
extra_args="-p $PID"
fi
while true; do
[ -z "${ONCE}" ] && printf "\033c" # Don't clear screen if it's only once
echo "----------------------------------------------------"
ps -eo pid,ppid,rss:10,%mem,%cpu,cmd $extra_args | grep -v 'ps -eo'
echo "----------------------------------------------------"
echo -n "* CPU Cores: "; nproc
echo "* Memory: "; free -th
[ -n "${ONCE}" ] && break # Exit if asked for only once
read -r -s -n 1 -t "${RATE}" input
if [[ "${input}" = "q" ]] || [[ "${input}" = "Q" ]]; then
echo
break
fi
sleep "${RATE}"
done
}
main "$@"