-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcpumemlog
executable file
·159 lines (129 loc) · 3.36 KB
/
cpumemlog
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
#!/bin/bash
### cpumemlog
### ----------------------------------------------------------------------------
NAME=$(basename $0)
usage()
{
cat <<EOF
NAME
$NAME - Monitor CPU and RAM usage for a given process (and its children)
SYNOPSIS
$NAME pid [string] [-t=1] [-o=<outputfilename>]
DESCRIPTION
pid
ID of a process that will be monitored; must be the FIRST argument
string
A string to be added to the output file name to help distinguishing
several output files; if used it must be the SECOND argument of a call
-t=x, --time=x
Collect information in time intervals equal to x; this is passed to the
sleep command so check its documentation on the format of x; default is
10 seconds.
-o=<path>, --out=<path>
Specify filename of output file. If not given, defaults to
cpumemlog_<pid>[_string].txt
-h, --help
Print this output
EXAMPLE
Monitor process with process ID 123 and put this script into background
to be able to work further in this terminal:
$NAME 123 &
... and add a string JOB to the output:
$NAME 123 JOB &
... and collect information every 2 seconds:
$NAME 123 JOB -t=2 &
AUTHOR
Gregor Gorjanc <gregor dot gorjanc at gmail dot com>
WEB
http://github.com/gregorgorjanc/cpumemlog
EOF
}
## Defaults
PID=
STR=
TIME=10
FOUT=
# argument parsing : http://mywiki.wooledge.org/BashFAQ/035
position=0
while :; do
case $1 in
-h | --help )
usage
exit 0
;;
-t=* | --time=*)
TIME=$(echo $1 | sed -e "s/--time=//" -e "s/-t=//")
;;
-o=* | --out=*)
FOUT=$(echo $1 | sed -e "s/--out=//" -e "s/-o=//")
;;
--) # End of all options.
printf 'End of all options?'
shift
break
;;
-?*)
printf 'WARNING: Unknown option (ignored): %s\n' "$1" >&2
;;
*) # Default case: If no more options then break out of the loop.
position=$((position+1))
if [ ${#1} -eq 0 ]; then
break
fi
case $position in
1)
PID=$1
;;
2)
STR=$1
;;
*)
printf 'WARNING: Unknown positional argument (ignored): %s\n' "$1" >&2
exit 1
esac
esac
shift
done
## Collect PID and STRING
if [ -z "$PID" ]; then
echo "Missing PID!"
usage
exit 1
fi
if [ -z $FOUT ]; then
if [ -z $STR ]; then
FOUT=cpumemlog_${PID}.txt
else
FOUT=cpumemlog_${PID}_${STR}.txt
fi
fi
rm -f $FOUT
echo "DATE TIME PID PCPU PMEM RSS VSZ ETIME COMMAND" >> $FOUT
printOut()
{
local PID
PID=$1
## Print everything except the header
## echo `date +"%Y-%m-%d %H:%M:%S"` `aps -p $PID -o pid= -o pcpu= -o pmem= -o rss= -o vsz= -o time= -o comm= --no-headers -w -w` >> $FOUT
## BSD always prints the header
echo `date +"%Y-%m-%d %H:%M:%S"` `ps -p $PID -o pid= -o pcpu= -o pmem= -o rss= -o vsz= -o time= -o comm= -w -w | tail -n 1` >> $FOUT
## Any children?
PIDC=$(pgrep -P $PID)
for child in $PIDC; do
printOut $child
done
}
DO="yes"
while [ "$DO" == "yes" ]; do
## Is process running?
WC=$(ps u -p $PID | wc -l | awk '{ print $1 }')
if [ "$WC" != "1" ]; then
printOut $PID
sleep $TIME
else
DO="no"
fi
done
echo -e "\n$0 for $STR finnished\n"
###----------------------------------------------------------------------------
### cpumemlog ends here