How to monitor CPU/memory usage of a single process?
up vote
128
down vote
favorite
I would like to monitor one process's memory / cpu usage in real time. Similar to top
but targeted at only one process, preferably with a history graph of some sort.
shell process monitoring top
add a comment |
up vote
128
down vote
favorite
I would like to monitor one process's memory / cpu usage in real time. Similar to top
but targeted at only one process, preferably with a history graph of some sort.
shell process monitoring top
What memory statistics do you want? There are lots of them.
– vwduder
Jan 13 '11 at 11:33
Memory usage over a given time frame, current usage, maximum usage, average.
– Josh K
Jan 13 '11 at 11:33
add a comment |
up vote
128
down vote
favorite
up vote
128
down vote
favorite
I would like to monitor one process's memory / cpu usage in real time. Similar to top
but targeted at only one process, preferably with a history graph of some sort.
shell process monitoring top
I would like to monitor one process's memory / cpu usage in real time. Similar to top
but targeted at only one process, preferably with a history graph of some sort.
shell process monitoring top
shell process monitoring top
edited Feb 4 '11 at 17:06
Tshepang
25.4k71182262
25.4k71182262
asked Aug 17 '10 at 3:21
Josh K
1,00631314
1,00631314
What memory statistics do you want? There are lots of them.
– vwduder
Jan 13 '11 at 11:33
Memory usage over a given time frame, current usage, maximum usage, average.
– Josh K
Jan 13 '11 at 11:33
add a comment |
What memory statistics do you want? There are lots of them.
– vwduder
Jan 13 '11 at 11:33
Memory usage over a given time frame, current usage, maximum usage, average.
– Josh K
Jan 13 '11 at 11:33
What memory statistics do you want? There are lots of them.
– vwduder
Jan 13 '11 at 11:33
What memory statistics do you want? There are lots of them.
– vwduder
Jan 13 '11 at 11:33
Memory usage over a given time frame, current usage, maximum usage, average.
– Josh K
Jan 13 '11 at 11:33
Memory usage over a given time frame, current usage, maximum usage, average.
– Josh K
Jan 13 '11 at 11:33
add a comment |
8 Answers
8
active
oldest
votes
up vote
114
down vote
accepted
On Linux, top
actually supports focusing on a single process, although it naturally doesn't have a history graph:
top -p PID
This is also available on Mac OS X with a different syntax:
top -pid PID
8
And since you may not want to look up the PID every time, try something liketop -p `pgrep -f /usr/bin/kvm`
.
– Stefan Lasiewski
Aug 17 '10 at 3:33
I use Cacti to monitor some individual processes, but installing a full blown Cacti installation sounds too complex for the simple situation asked here.
– Stefan Lasiewski
Aug 17 '10 at 3:34
@Stefan: I'm assuming I would have to run that remotely?
– Josh K
Aug 17 '10 at 4:00
@Josh : Yes you would need to run Cacti (Which requires MySQL, Apache and few other packages) on another server. On most distros, it's pretty simple to install using Yum or apt-get.
– Stefan Lasiewski
Aug 17 '10 at 20:48
@Stefan if you want to to check remotly you can do ssh@remotehost 'top -p PID > ~hostname
_pid.txt; exit'and
– klerk
May 13 '14 at 20:02
add a comment |
up vote
54
down vote
htop
is a great replacement to top
. It has... Colors! Simple keyboard shortcuts! Scroll the list using the arrow keys! Kill a process without leaving and without taking note of the PID! Mark multiple processes and kill them all!
Among all of the features, the manpage says you can press F to follow a process.
Really, you should try htop
. I never started top
again, after the first time I used htop
.
Display a single process:
htop -p PID
7
+1 for htop. This is one of the first program I install on a new system. It makes my life much easier. The tree view is also very handy.
– Barthelemy
Nov 24 '10 at 12:22
7
top
also has colors. Pressz
.
– Tshepang
Jan 12 '11 at 1:41
2
You're right!top
has colors! Too bad its colors are quite useless, specially when compared tohtop
(which fades other users processes and highlights the program basename).
– Denilson Sá Maia
Jan 12 '11 at 18:17
1
Andhtop -p PID
will work too, just like the example given by @Michael Mrozek.
– noisebleed
Nov 25 '14 at 12:05
1
Then only reason to use top, is because htop is not available or can't be installed. That is why htop was created, to provide much more features.
– lepe
Apr 13 '15 at 2:55
|
show 1 more comment
up vote
25
down vote
psrecord
The following addresses history graph of some sort. Python psrecord
package does exactly this.
pip install psrecord # local user install
sudo apt-get install python-matplotlib python-tk # for plotting; or via pip
For single process it's the following (stopped by Ctrl+C):
psrecord $(pgrep proc-name1) --interval 1 --plot plot1.png
For several processes the following script is helpful to synchronise the charts:
#!/bin/bash
psrecord $(pgrep proc-name1) --interval 1 --duration 60 --plot plot1.png &
P1=$!
psrecord $(pgrep proc-name2) --interval 1 --duration 60 --plot plot2.png &
P2=$!
wait $P1 $P2
echo 'Done'
Charts look like:
memory_profiler
The package provides RSS-only sampling (plus some Python-specific options). It can also record process with its children processes (see mprof --help
).
pip install memory_profiler
mprof run /path/to/executable
mprof plot
By default this pops up a Tkinter-based (python-tk
may be needed) chart explorer which can be exported:
graphite-stack & statsd
It may seem an overkill for a simple one-off test, but for something like a several-day debugging it's, for sure, reasonable. A handy all-in-one raintank/graphite-stack
(from Grafana's authors) image and psutil
and statsd
client. procmon.py
provides an implementation.
$ docker run --rm -p 8080:3000 -p 8125:8125/udp raintank/graphite-stack
Then in another terminal, after starting target process:
$ sudo apt-get install python-statsd python-psutil # or via pip
$ python procmon.py -s localhost -f chromium -r 'chromium.*'
Then opening Grafana at http://localhost:8080, authentication as admin:admin
, setting up datasource https://localhost, you can plot a chart like:
graphite-stack & telegraf
Instead of Python script sending the metrics to Statsd, telegraf
(and procstat
input plugin) can be used to send the metrics to Graphite directly.
Minimal telegraf
configuration looks like:
[agent]
interval = "1s"
[[outputs.graphite]]
servers = ["localhost:2003"]
prefix = "testprfx"
[[inputs.procstat]]
pid_file = "/path/to/file/with.pid"
Then run line telegraf --config minconf.conf
. Grafana part is the same, except metrics names.
sysdig
sysdig
(available in Debian and Ubuntu's repos) with sysdig-inspect UI look very promising, providing extremely fine-grained details along with CPU utilisation and RSS, but unfortunately the UI is unable to render them, and sysdig
can't filter procinfo
event by process at the time of writing. Though, this should be possible with a custom chisel (an sysdig
extension written in Lua).
pgrep systemd is giving multiple lines of output, and thus bugs the psrecord, what should be done? I just want to test with any process.
– EralpB
Apr 11 at 7:09
1
@EralpBpgrep --help
to the rescue. There's at least--newest
and--oldest
.
– saaj
Apr 11 at 9:03
add a comment |
up vote
8
down vote
To use that information on a script you can do this:
calcPercCpu.sh
#!/bin/bash
nPid=$1;
nTimes=10; # customize it
delay=0.1; # customize it
strCalc=`top -d $delay -b -n $nTimes -p $nPid
|grep $nPid
|sed -r -e "s;ss*; ;g" -e "s;^ *;;"
|cut -d' ' -f9
|tr 'n' '+'
|sed -r -e "s;(.*)[+]$;1;" -e "s/.*/scale=2;(&)/$nTimes/"`;
nPercCpu=`echo "$strCalc" |bc -l`
echo $nPercCpu
use like: calcPercCpu.sh 1234
where 1234 is the pid
For the specified $nPid, it will measure the average of 10 snapshots of the cpu usage in a whole of 1 second (delay of 0.1s each * nTimes=10); that provides a good and fast accurate result of what is happening in the very moment.
Tweak the variables to your needs.
add a comment |
up vote
5
down vote
I normally use following two :
HP caliper : its very good tool for monitoring processes it you can check call graph and other low level information also. But please note its free only for personal use.
daemontools : a collection of tools for managing UNIX services
5
I used daemontools for years. It's great as a supervisor/watchdog for other processes. How does it help you monitor CPU/memory usage for one process?
– Stefan Lasiewski
Aug 19 '10 at 4:05
add a comment |
up vote
2
down vote
If you know process name you can use
top -p $(pidof <process_name>)
7
That's pretty much what the accepted answer, from years ago, and its first comment say.
– dhag
Apr 29 '15 at 15:26
add a comment |
up vote
0
down vote
If you need the averages for a period of time of a specific process, try the accumulative -c option of top:
top -c a -pid PID
"-c a" found in top for Mac 10.8.5.
For Scientific Linux, the option is -S, that can be set interactively.
You'll likely want to add further details around which version(s) oftop
actually provide this feature. My version on Fedora 19 does not. Same too on Ubuntu 13.04.
– slm♦
May 12 '14 at 1:22
You're right!, I was so happy of having found something useful, I forgot I was in my mac at home.
– Kieleth
May 13 '14 at 19:34
add a comment |
up vote
0
down vote
If you have a cut-down Linux distribution where top does not have per process (-p) option or related options, you can parse the output of the top command for your process name to get the CPU usage information per process.
while true; do top -bn1 | awk '/your_process_name/ {print $8}' ; sleep 1; done
8 represents the CPU usage per process in the output of the top command in my embedded Linux distribution
add a comment |
8 Answers
8
active
oldest
votes
8 Answers
8
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
114
down vote
accepted
On Linux, top
actually supports focusing on a single process, although it naturally doesn't have a history graph:
top -p PID
This is also available on Mac OS X with a different syntax:
top -pid PID
8
And since you may not want to look up the PID every time, try something liketop -p `pgrep -f /usr/bin/kvm`
.
– Stefan Lasiewski
Aug 17 '10 at 3:33
I use Cacti to monitor some individual processes, but installing a full blown Cacti installation sounds too complex for the simple situation asked here.
– Stefan Lasiewski
Aug 17 '10 at 3:34
@Stefan: I'm assuming I would have to run that remotely?
– Josh K
Aug 17 '10 at 4:00
@Josh : Yes you would need to run Cacti (Which requires MySQL, Apache and few other packages) on another server. On most distros, it's pretty simple to install using Yum or apt-get.
– Stefan Lasiewski
Aug 17 '10 at 20:48
@Stefan if you want to to check remotly you can do ssh@remotehost 'top -p PID > ~hostname
_pid.txt; exit'and
– klerk
May 13 '14 at 20:02
add a comment |
up vote
114
down vote
accepted
On Linux, top
actually supports focusing on a single process, although it naturally doesn't have a history graph:
top -p PID
This is also available on Mac OS X with a different syntax:
top -pid PID
8
And since you may not want to look up the PID every time, try something liketop -p `pgrep -f /usr/bin/kvm`
.
– Stefan Lasiewski
Aug 17 '10 at 3:33
I use Cacti to monitor some individual processes, but installing a full blown Cacti installation sounds too complex for the simple situation asked here.
– Stefan Lasiewski
Aug 17 '10 at 3:34
@Stefan: I'm assuming I would have to run that remotely?
– Josh K
Aug 17 '10 at 4:00
@Josh : Yes you would need to run Cacti (Which requires MySQL, Apache and few other packages) on another server. On most distros, it's pretty simple to install using Yum or apt-get.
– Stefan Lasiewski
Aug 17 '10 at 20:48
@Stefan if you want to to check remotly you can do ssh@remotehost 'top -p PID > ~hostname
_pid.txt; exit'and
– klerk
May 13 '14 at 20:02
add a comment |
up vote
114
down vote
accepted
up vote
114
down vote
accepted
On Linux, top
actually supports focusing on a single process, although it naturally doesn't have a history graph:
top -p PID
This is also available on Mac OS X with a different syntax:
top -pid PID
On Linux, top
actually supports focusing on a single process, although it naturally doesn't have a history graph:
top -p PID
This is also available on Mac OS X with a different syntax:
top -pid PID
edited Apr 10 '14 at 21:23
Gilles
523k12610411575
523k12610411575
answered Aug 17 '10 at 3:27
Michael Mrozek♦
60k28187208
60k28187208
8
And since you may not want to look up the PID every time, try something liketop -p `pgrep -f /usr/bin/kvm`
.
– Stefan Lasiewski
Aug 17 '10 at 3:33
I use Cacti to monitor some individual processes, but installing a full blown Cacti installation sounds too complex for the simple situation asked here.
– Stefan Lasiewski
Aug 17 '10 at 3:34
@Stefan: I'm assuming I would have to run that remotely?
– Josh K
Aug 17 '10 at 4:00
@Josh : Yes you would need to run Cacti (Which requires MySQL, Apache and few other packages) on another server. On most distros, it's pretty simple to install using Yum or apt-get.
– Stefan Lasiewski
Aug 17 '10 at 20:48
@Stefan if you want to to check remotly you can do ssh@remotehost 'top -p PID > ~hostname
_pid.txt; exit'and
– klerk
May 13 '14 at 20:02
add a comment |
8
And since you may not want to look up the PID every time, try something liketop -p `pgrep -f /usr/bin/kvm`
.
– Stefan Lasiewski
Aug 17 '10 at 3:33
I use Cacti to monitor some individual processes, but installing a full blown Cacti installation sounds too complex for the simple situation asked here.
– Stefan Lasiewski
Aug 17 '10 at 3:34
@Stefan: I'm assuming I would have to run that remotely?
– Josh K
Aug 17 '10 at 4:00
@Josh : Yes you would need to run Cacti (Which requires MySQL, Apache and few other packages) on another server. On most distros, it's pretty simple to install using Yum or apt-get.
– Stefan Lasiewski
Aug 17 '10 at 20:48
@Stefan if you want to to check remotly you can do ssh@remotehost 'top -p PID > ~hostname
_pid.txt; exit'and
– klerk
May 13 '14 at 20:02
8
8
And since you may not want to look up the PID every time, try something like
top -p `pgrep -f /usr/bin/kvm`
.– Stefan Lasiewski
Aug 17 '10 at 3:33
And since you may not want to look up the PID every time, try something like
top -p `pgrep -f /usr/bin/kvm`
.– Stefan Lasiewski
Aug 17 '10 at 3:33
I use Cacti to monitor some individual processes, but installing a full blown Cacti installation sounds too complex for the simple situation asked here.
– Stefan Lasiewski
Aug 17 '10 at 3:34
I use Cacti to monitor some individual processes, but installing a full blown Cacti installation sounds too complex for the simple situation asked here.
– Stefan Lasiewski
Aug 17 '10 at 3:34
@Stefan: I'm assuming I would have to run that remotely?
– Josh K
Aug 17 '10 at 4:00
@Stefan: I'm assuming I would have to run that remotely?
– Josh K
Aug 17 '10 at 4:00
@Josh : Yes you would need to run Cacti (Which requires MySQL, Apache and few other packages) on another server. On most distros, it's pretty simple to install using Yum or apt-get.
– Stefan Lasiewski
Aug 17 '10 at 20:48
@Josh : Yes you would need to run Cacti (Which requires MySQL, Apache and few other packages) on another server. On most distros, it's pretty simple to install using Yum or apt-get.
– Stefan Lasiewski
Aug 17 '10 at 20:48
@Stefan if you want to to check remotly you can do ssh@remotehost 'top -p PID > ~
hostname
_pid.txt; exit'and– klerk
May 13 '14 at 20:02
@Stefan if you want to to check remotly you can do ssh@remotehost 'top -p PID > ~
hostname
_pid.txt; exit'and– klerk
May 13 '14 at 20:02
add a comment |
up vote
54
down vote
htop
is a great replacement to top
. It has... Colors! Simple keyboard shortcuts! Scroll the list using the arrow keys! Kill a process without leaving and without taking note of the PID! Mark multiple processes and kill them all!
Among all of the features, the manpage says you can press F to follow a process.
Really, you should try htop
. I never started top
again, after the first time I used htop
.
Display a single process:
htop -p PID
7
+1 for htop. This is one of the first program I install on a new system. It makes my life much easier. The tree view is also very handy.
– Barthelemy
Nov 24 '10 at 12:22
7
top
also has colors. Pressz
.
– Tshepang
Jan 12 '11 at 1:41
2
You're right!top
has colors! Too bad its colors are quite useless, specially when compared tohtop
(which fades other users processes and highlights the program basename).
– Denilson Sá Maia
Jan 12 '11 at 18:17
1
Andhtop -p PID
will work too, just like the example given by @Michael Mrozek.
– noisebleed
Nov 25 '14 at 12:05
1
Then only reason to use top, is because htop is not available or can't be installed. That is why htop was created, to provide much more features.
– lepe
Apr 13 '15 at 2:55
|
show 1 more comment
up vote
54
down vote
htop
is a great replacement to top
. It has... Colors! Simple keyboard shortcuts! Scroll the list using the arrow keys! Kill a process without leaving and without taking note of the PID! Mark multiple processes and kill them all!
Among all of the features, the manpage says you can press F to follow a process.
Really, you should try htop
. I never started top
again, after the first time I used htop
.
Display a single process:
htop -p PID
7
+1 for htop. This is one of the first program I install on a new system. It makes my life much easier. The tree view is also very handy.
– Barthelemy
Nov 24 '10 at 12:22
7
top
also has colors. Pressz
.
– Tshepang
Jan 12 '11 at 1:41
2
You're right!top
has colors! Too bad its colors are quite useless, specially when compared tohtop
(which fades other users processes and highlights the program basename).
– Denilson Sá Maia
Jan 12 '11 at 18:17
1
Andhtop -p PID
will work too, just like the example given by @Michael Mrozek.
– noisebleed
Nov 25 '14 at 12:05
1
Then only reason to use top, is because htop is not available or can't be installed. That is why htop was created, to provide much more features.
– lepe
Apr 13 '15 at 2:55
|
show 1 more comment
up vote
54
down vote
up vote
54
down vote
htop
is a great replacement to top
. It has... Colors! Simple keyboard shortcuts! Scroll the list using the arrow keys! Kill a process without leaving and without taking note of the PID! Mark multiple processes and kill them all!
Among all of the features, the manpage says you can press F to follow a process.
Really, you should try htop
. I never started top
again, after the first time I used htop
.
Display a single process:
htop -p PID
htop
is a great replacement to top
. It has... Colors! Simple keyboard shortcuts! Scroll the list using the arrow keys! Kill a process without leaving and without taking note of the PID! Mark multiple processes and kill them all!
Among all of the features, the manpage says you can press F to follow a process.
Really, you should try htop
. I never started top
again, after the first time I used htop
.
Display a single process:
htop -p PID
edited Apr 13 '15 at 4:49
lepe
29527
29527
answered Aug 17 '10 at 22:47
Denilson Sá Maia
9441814
9441814
7
+1 for htop. This is one of the first program I install on a new system. It makes my life much easier. The tree view is also very handy.
– Barthelemy
Nov 24 '10 at 12:22
7
top
also has colors. Pressz
.
– Tshepang
Jan 12 '11 at 1:41
2
You're right!top
has colors! Too bad its colors are quite useless, specially when compared tohtop
(which fades other users processes and highlights the program basename).
– Denilson Sá Maia
Jan 12 '11 at 18:17
1
Andhtop -p PID
will work too, just like the example given by @Michael Mrozek.
– noisebleed
Nov 25 '14 at 12:05
1
Then only reason to use top, is because htop is not available or can't be installed. That is why htop was created, to provide much more features.
– lepe
Apr 13 '15 at 2:55
|
show 1 more comment
7
+1 for htop. This is one of the first program I install on a new system. It makes my life much easier. The tree view is also very handy.
– Barthelemy
Nov 24 '10 at 12:22
7
top
also has colors. Pressz
.
– Tshepang
Jan 12 '11 at 1:41
2
You're right!top
has colors! Too bad its colors are quite useless, specially when compared tohtop
(which fades other users processes and highlights the program basename).
– Denilson Sá Maia
Jan 12 '11 at 18:17
1
Andhtop -p PID
will work too, just like the example given by @Michael Mrozek.
– noisebleed
Nov 25 '14 at 12:05
1
Then only reason to use top, is because htop is not available or can't be installed. That is why htop was created, to provide much more features.
– lepe
Apr 13 '15 at 2:55
7
7
+1 for htop. This is one of the first program I install on a new system. It makes my life much easier. The tree view is also very handy.
– Barthelemy
Nov 24 '10 at 12:22
+1 for htop. This is one of the first program I install on a new system. It makes my life much easier. The tree view is also very handy.
– Barthelemy
Nov 24 '10 at 12:22
7
7
top
also has colors. Press z
.– Tshepang
Jan 12 '11 at 1:41
top
also has colors. Press z
.– Tshepang
Jan 12 '11 at 1:41
2
2
You're right!
top
has colors! Too bad its colors are quite useless, specially when compared to htop
(which fades other users processes and highlights the program basename).– Denilson Sá Maia
Jan 12 '11 at 18:17
You're right!
top
has colors! Too bad its colors are quite useless, specially when compared to htop
(which fades other users processes and highlights the program basename).– Denilson Sá Maia
Jan 12 '11 at 18:17
1
1
And
htop -p PID
will work too, just like the example given by @Michael Mrozek.– noisebleed
Nov 25 '14 at 12:05
And
htop -p PID
will work too, just like the example given by @Michael Mrozek.– noisebleed
Nov 25 '14 at 12:05
1
1
Then only reason to use top, is because htop is not available or can't be installed. That is why htop was created, to provide much more features.
– lepe
Apr 13 '15 at 2:55
Then only reason to use top, is because htop is not available or can't be installed. That is why htop was created, to provide much more features.
– lepe
Apr 13 '15 at 2:55
|
show 1 more comment
up vote
25
down vote
psrecord
The following addresses history graph of some sort. Python psrecord
package does exactly this.
pip install psrecord # local user install
sudo apt-get install python-matplotlib python-tk # for plotting; or via pip
For single process it's the following (stopped by Ctrl+C):
psrecord $(pgrep proc-name1) --interval 1 --plot plot1.png
For several processes the following script is helpful to synchronise the charts:
#!/bin/bash
psrecord $(pgrep proc-name1) --interval 1 --duration 60 --plot plot1.png &
P1=$!
psrecord $(pgrep proc-name2) --interval 1 --duration 60 --plot plot2.png &
P2=$!
wait $P1 $P2
echo 'Done'
Charts look like:
memory_profiler
The package provides RSS-only sampling (plus some Python-specific options). It can also record process with its children processes (see mprof --help
).
pip install memory_profiler
mprof run /path/to/executable
mprof plot
By default this pops up a Tkinter-based (python-tk
may be needed) chart explorer which can be exported:
graphite-stack & statsd
It may seem an overkill for a simple one-off test, but for something like a several-day debugging it's, for sure, reasonable. A handy all-in-one raintank/graphite-stack
(from Grafana's authors) image and psutil
and statsd
client. procmon.py
provides an implementation.
$ docker run --rm -p 8080:3000 -p 8125:8125/udp raintank/graphite-stack
Then in another terminal, after starting target process:
$ sudo apt-get install python-statsd python-psutil # or via pip
$ python procmon.py -s localhost -f chromium -r 'chromium.*'
Then opening Grafana at http://localhost:8080, authentication as admin:admin
, setting up datasource https://localhost, you can plot a chart like:
graphite-stack & telegraf
Instead of Python script sending the metrics to Statsd, telegraf
(and procstat
input plugin) can be used to send the metrics to Graphite directly.
Minimal telegraf
configuration looks like:
[agent]
interval = "1s"
[[outputs.graphite]]
servers = ["localhost:2003"]
prefix = "testprfx"
[[inputs.procstat]]
pid_file = "/path/to/file/with.pid"
Then run line telegraf --config minconf.conf
. Grafana part is the same, except metrics names.
sysdig
sysdig
(available in Debian and Ubuntu's repos) with sysdig-inspect UI look very promising, providing extremely fine-grained details along with CPU utilisation and RSS, but unfortunately the UI is unable to render them, and sysdig
can't filter procinfo
event by process at the time of writing. Though, this should be possible with a custom chisel (an sysdig
extension written in Lua).
pgrep systemd is giving multiple lines of output, and thus bugs the psrecord, what should be done? I just want to test with any process.
– EralpB
Apr 11 at 7:09
1
@EralpBpgrep --help
to the rescue. There's at least--newest
and--oldest
.
– saaj
Apr 11 at 9:03
add a comment |
up vote
25
down vote
psrecord
The following addresses history graph of some sort. Python psrecord
package does exactly this.
pip install psrecord # local user install
sudo apt-get install python-matplotlib python-tk # for plotting; or via pip
For single process it's the following (stopped by Ctrl+C):
psrecord $(pgrep proc-name1) --interval 1 --plot plot1.png
For several processes the following script is helpful to synchronise the charts:
#!/bin/bash
psrecord $(pgrep proc-name1) --interval 1 --duration 60 --plot plot1.png &
P1=$!
psrecord $(pgrep proc-name2) --interval 1 --duration 60 --plot plot2.png &
P2=$!
wait $P1 $P2
echo 'Done'
Charts look like:
memory_profiler
The package provides RSS-only sampling (plus some Python-specific options). It can also record process with its children processes (see mprof --help
).
pip install memory_profiler
mprof run /path/to/executable
mprof plot
By default this pops up a Tkinter-based (python-tk
may be needed) chart explorer which can be exported:
graphite-stack & statsd
It may seem an overkill for a simple one-off test, but for something like a several-day debugging it's, for sure, reasonable. A handy all-in-one raintank/graphite-stack
(from Grafana's authors) image and psutil
and statsd
client. procmon.py
provides an implementation.
$ docker run --rm -p 8080:3000 -p 8125:8125/udp raintank/graphite-stack
Then in another terminal, after starting target process:
$ sudo apt-get install python-statsd python-psutil # or via pip
$ python procmon.py -s localhost -f chromium -r 'chromium.*'
Then opening Grafana at http://localhost:8080, authentication as admin:admin
, setting up datasource https://localhost, you can plot a chart like:
graphite-stack & telegraf
Instead of Python script sending the metrics to Statsd, telegraf
(and procstat
input plugin) can be used to send the metrics to Graphite directly.
Minimal telegraf
configuration looks like:
[agent]
interval = "1s"
[[outputs.graphite]]
servers = ["localhost:2003"]
prefix = "testprfx"
[[inputs.procstat]]
pid_file = "/path/to/file/with.pid"
Then run line telegraf --config minconf.conf
. Grafana part is the same, except metrics names.
sysdig
sysdig
(available in Debian and Ubuntu's repos) with sysdig-inspect UI look very promising, providing extremely fine-grained details along with CPU utilisation and RSS, but unfortunately the UI is unable to render them, and sysdig
can't filter procinfo
event by process at the time of writing. Though, this should be possible with a custom chisel (an sysdig
extension written in Lua).
pgrep systemd is giving multiple lines of output, and thus bugs the psrecord, what should be done? I just want to test with any process.
– EralpB
Apr 11 at 7:09
1
@EralpBpgrep --help
to the rescue. There's at least--newest
and--oldest
.
– saaj
Apr 11 at 9:03
add a comment |
up vote
25
down vote
up vote
25
down vote
psrecord
The following addresses history graph of some sort. Python psrecord
package does exactly this.
pip install psrecord # local user install
sudo apt-get install python-matplotlib python-tk # for plotting; or via pip
For single process it's the following (stopped by Ctrl+C):
psrecord $(pgrep proc-name1) --interval 1 --plot plot1.png
For several processes the following script is helpful to synchronise the charts:
#!/bin/bash
psrecord $(pgrep proc-name1) --interval 1 --duration 60 --plot plot1.png &
P1=$!
psrecord $(pgrep proc-name2) --interval 1 --duration 60 --plot plot2.png &
P2=$!
wait $P1 $P2
echo 'Done'
Charts look like:
memory_profiler
The package provides RSS-only sampling (plus some Python-specific options). It can also record process with its children processes (see mprof --help
).
pip install memory_profiler
mprof run /path/to/executable
mprof plot
By default this pops up a Tkinter-based (python-tk
may be needed) chart explorer which can be exported:
graphite-stack & statsd
It may seem an overkill for a simple one-off test, but for something like a several-day debugging it's, for sure, reasonable. A handy all-in-one raintank/graphite-stack
(from Grafana's authors) image and psutil
and statsd
client. procmon.py
provides an implementation.
$ docker run --rm -p 8080:3000 -p 8125:8125/udp raintank/graphite-stack
Then in another terminal, after starting target process:
$ sudo apt-get install python-statsd python-psutil # or via pip
$ python procmon.py -s localhost -f chromium -r 'chromium.*'
Then opening Grafana at http://localhost:8080, authentication as admin:admin
, setting up datasource https://localhost, you can plot a chart like:
graphite-stack & telegraf
Instead of Python script sending the metrics to Statsd, telegraf
(and procstat
input plugin) can be used to send the metrics to Graphite directly.
Minimal telegraf
configuration looks like:
[agent]
interval = "1s"
[[outputs.graphite]]
servers = ["localhost:2003"]
prefix = "testprfx"
[[inputs.procstat]]
pid_file = "/path/to/file/with.pid"
Then run line telegraf --config minconf.conf
. Grafana part is the same, except metrics names.
sysdig
sysdig
(available in Debian and Ubuntu's repos) with sysdig-inspect UI look very promising, providing extremely fine-grained details along with CPU utilisation and RSS, but unfortunately the UI is unable to render them, and sysdig
can't filter procinfo
event by process at the time of writing. Though, this should be possible with a custom chisel (an sysdig
extension written in Lua).
psrecord
The following addresses history graph of some sort. Python psrecord
package does exactly this.
pip install psrecord # local user install
sudo apt-get install python-matplotlib python-tk # for plotting; or via pip
For single process it's the following (stopped by Ctrl+C):
psrecord $(pgrep proc-name1) --interval 1 --plot plot1.png
For several processes the following script is helpful to synchronise the charts:
#!/bin/bash
psrecord $(pgrep proc-name1) --interval 1 --duration 60 --plot plot1.png &
P1=$!
psrecord $(pgrep proc-name2) --interval 1 --duration 60 --plot plot2.png &
P2=$!
wait $P1 $P2
echo 'Done'
Charts look like:
memory_profiler
The package provides RSS-only sampling (plus some Python-specific options). It can also record process with its children processes (see mprof --help
).
pip install memory_profiler
mprof run /path/to/executable
mprof plot
By default this pops up a Tkinter-based (python-tk
may be needed) chart explorer which can be exported:
graphite-stack & statsd
It may seem an overkill for a simple one-off test, but for something like a several-day debugging it's, for sure, reasonable. A handy all-in-one raintank/graphite-stack
(from Grafana's authors) image and psutil
and statsd
client. procmon.py
provides an implementation.
$ docker run --rm -p 8080:3000 -p 8125:8125/udp raintank/graphite-stack
Then in another terminal, after starting target process:
$ sudo apt-get install python-statsd python-psutil # or via pip
$ python procmon.py -s localhost -f chromium -r 'chromium.*'
Then opening Grafana at http://localhost:8080, authentication as admin:admin
, setting up datasource https://localhost, you can plot a chart like:
graphite-stack & telegraf
Instead of Python script sending the metrics to Statsd, telegraf
(and procstat
input plugin) can be used to send the metrics to Graphite directly.
Minimal telegraf
configuration looks like:
[agent]
interval = "1s"
[[outputs.graphite]]
servers = ["localhost:2003"]
prefix = "testprfx"
[[inputs.procstat]]
pid_file = "/path/to/file/with.pid"
Then run line telegraf --config minconf.conf
. Grafana part is the same, except metrics names.
sysdig
sysdig
(available in Debian and Ubuntu's repos) with sysdig-inspect UI look very promising, providing extremely fine-grained details along with CPU utilisation and RSS, but unfortunately the UI is unable to render them, and sysdig
can't filter procinfo
event by process at the time of writing. Though, this should be possible with a custom chisel (an sysdig
extension written in Lua).
edited Jun 7 at 20:52
answered Jan 4 at 13:38
saaj
38134
38134
pgrep systemd is giving multiple lines of output, and thus bugs the psrecord, what should be done? I just want to test with any process.
– EralpB
Apr 11 at 7:09
1
@EralpBpgrep --help
to the rescue. There's at least--newest
and--oldest
.
– saaj
Apr 11 at 9:03
add a comment |
pgrep systemd is giving multiple lines of output, and thus bugs the psrecord, what should be done? I just want to test with any process.
– EralpB
Apr 11 at 7:09
1
@EralpBpgrep --help
to the rescue. There's at least--newest
and--oldest
.
– saaj
Apr 11 at 9:03
pgrep systemd is giving multiple lines of output, and thus bugs the psrecord, what should be done? I just want to test with any process.
– EralpB
Apr 11 at 7:09
pgrep systemd is giving multiple lines of output, and thus bugs the psrecord, what should be done? I just want to test with any process.
– EralpB
Apr 11 at 7:09
1
1
@EralpB
pgrep --help
to the rescue. There's at least --newest
and --oldest
.– saaj
Apr 11 at 9:03
@EralpB
pgrep --help
to the rescue. There's at least --newest
and --oldest
.– saaj
Apr 11 at 9:03
add a comment |
up vote
8
down vote
To use that information on a script you can do this:
calcPercCpu.sh
#!/bin/bash
nPid=$1;
nTimes=10; # customize it
delay=0.1; # customize it
strCalc=`top -d $delay -b -n $nTimes -p $nPid
|grep $nPid
|sed -r -e "s;ss*; ;g" -e "s;^ *;;"
|cut -d' ' -f9
|tr 'n' '+'
|sed -r -e "s;(.*)[+]$;1;" -e "s/.*/scale=2;(&)/$nTimes/"`;
nPercCpu=`echo "$strCalc" |bc -l`
echo $nPercCpu
use like: calcPercCpu.sh 1234
where 1234 is the pid
For the specified $nPid, it will measure the average of 10 snapshots of the cpu usage in a whole of 1 second (delay of 0.1s each * nTimes=10); that provides a good and fast accurate result of what is happening in the very moment.
Tweak the variables to your needs.
add a comment |
up vote
8
down vote
To use that information on a script you can do this:
calcPercCpu.sh
#!/bin/bash
nPid=$1;
nTimes=10; # customize it
delay=0.1; # customize it
strCalc=`top -d $delay -b -n $nTimes -p $nPid
|grep $nPid
|sed -r -e "s;ss*; ;g" -e "s;^ *;;"
|cut -d' ' -f9
|tr 'n' '+'
|sed -r -e "s;(.*)[+]$;1;" -e "s/.*/scale=2;(&)/$nTimes/"`;
nPercCpu=`echo "$strCalc" |bc -l`
echo $nPercCpu
use like: calcPercCpu.sh 1234
where 1234 is the pid
For the specified $nPid, it will measure the average of 10 snapshots of the cpu usage in a whole of 1 second (delay of 0.1s each * nTimes=10); that provides a good and fast accurate result of what is happening in the very moment.
Tweak the variables to your needs.
add a comment |
up vote
8
down vote
up vote
8
down vote
To use that information on a script you can do this:
calcPercCpu.sh
#!/bin/bash
nPid=$1;
nTimes=10; # customize it
delay=0.1; # customize it
strCalc=`top -d $delay -b -n $nTimes -p $nPid
|grep $nPid
|sed -r -e "s;ss*; ;g" -e "s;^ *;;"
|cut -d' ' -f9
|tr 'n' '+'
|sed -r -e "s;(.*)[+]$;1;" -e "s/.*/scale=2;(&)/$nTimes/"`;
nPercCpu=`echo "$strCalc" |bc -l`
echo $nPercCpu
use like: calcPercCpu.sh 1234
where 1234 is the pid
For the specified $nPid, it will measure the average of 10 snapshots of the cpu usage in a whole of 1 second (delay of 0.1s each * nTimes=10); that provides a good and fast accurate result of what is happening in the very moment.
Tweak the variables to your needs.
To use that information on a script you can do this:
calcPercCpu.sh
#!/bin/bash
nPid=$1;
nTimes=10; # customize it
delay=0.1; # customize it
strCalc=`top -d $delay -b -n $nTimes -p $nPid
|grep $nPid
|sed -r -e "s;ss*; ;g" -e "s;^ *;;"
|cut -d' ' -f9
|tr 'n' '+'
|sed -r -e "s;(.*)[+]$;1;" -e "s/.*/scale=2;(&)/$nTimes/"`;
nPercCpu=`echo "$strCalc" |bc -l`
echo $nPercCpu
use like: calcPercCpu.sh 1234
where 1234 is the pid
For the specified $nPid, it will measure the average of 10 snapshots of the cpu usage in a whole of 1 second (delay of 0.1s each * nTimes=10); that provides a good and fast accurate result of what is happening in the very moment.
Tweak the variables to your needs.
answered Jun 30 '13 at 4:31
Aquarius Power
1,63532035
1,63532035
add a comment |
add a comment |
up vote
5
down vote
I normally use following two :
HP caliper : its very good tool for monitoring processes it you can check call graph and other low level information also. But please note its free only for personal use.
daemontools : a collection of tools for managing UNIX services
5
I used daemontools for years. It's great as a supervisor/watchdog for other processes. How does it help you monitor CPU/memory usage for one process?
– Stefan Lasiewski
Aug 19 '10 at 4:05
add a comment |
up vote
5
down vote
I normally use following two :
HP caliper : its very good tool for monitoring processes it you can check call graph and other low level information also. But please note its free only for personal use.
daemontools : a collection of tools for managing UNIX services
5
I used daemontools for years. It's great as a supervisor/watchdog for other processes. How does it help you monitor CPU/memory usage for one process?
– Stefan Lasiewski
Aug 19 '10 at 4:05
add a comment |
up vote
5
down vote
up vote
5
down vote
I normally use following two :
HP caliper : its very good tool for monitoring processes it you can check call graph and other low level information also. But please note its free only for personal use.
daemontools : a collection of tools for managing UNIX services
I normally use following two :
HP caliper : its very good tool for monitoring processes it you can check call graph and other low level information also. But please note its free only for personal use.
daemontools : a collection of tools for managing UNIX services
edited Jan 12 '11 at 11:12
Tshepang
25.4k71182262
25.4k71182262
answered Aug 17 '10 at 8:59
Hemant
4,12123138
4,12123138
5
I used daemontools for years. It's great as a supervisor/watchdog for other processes. How does it help you monitor CPU/memory usage for one process?
– Stefan Lasiewski
Aug 19 '10 at 4:05
add a comment |
5
I used daemontools for years. It's great as a supervisor/watchdog for other processes. How does it help you monitor CPU/memory usage for one process?
– Stefan Lasiewski
Aug 19 '10 at 4:05
5
5
I used daemontools for years. It's great as a supervisor/watchdog for other processes. How does it help you monitor CPU/memory usage for one process?
– Stefan Lasiewski
Aug 19 '10 at 4:05
I used daemontools for years. It's great as a supervisor/watchdog for other processes. How does it help you monitor CPU/memory usage for one process?
– Stefan Lasiewski
Aug 19 '10 at 4:05
add a comment |
up vote
2
down vote
If you know process name you can use
top -p $(pidof <process_name>)
7
That's pretty much what the accepted answer, from years ago, and its first comment say.
– dhag
Apr 29 '15 at 15:26
add a comment |
up vote
2
down vote
If you know process name you can use
top -p $(pidof <process_name>)
7
That's pretty much what the accepted answer, from years ago, and its first comment say.
– dhag
Apr 29 '15 at 15:26
add a comment |
up vote
2
down vote
up vote
2
down vote
If you know process name you can use
top -p $(pidof <process_name>)
If you know process name you can use
top -p $(pidof <process_name>)
answered Apr 29 '15 at 15:03
user4757345
2002210
2002210
7
That's pretty much what the accepted answer, from years ago, and its first comment say.
– dhag
Apr 29 '15 at 15:26
add a comment |
7
That's pretty much what the accepted answer, from years ago, and its first comment say.
– dhag
Apr 29 '15 at 15:26
7
7
That's pretty much what the accepted answer, from years ago, and its first comment say.
– dhag
Apr 29 '15 at 15:26
That's pretty much what the accepted answer, from years ago, and its first comment say.
– dhag
Apr 29 '15 at 15:26
add a comment |
up vote
0
down vote
If you need the averages for a period of time of a specific process, try the accumulative -c option of top:
top -c a -pid PID
"-c a" found in top for Mac 10.8.5.
For Scientific Linux, the option is -S, that can be set interactively.
You'll likely want to add further details around which version(s) oftop
actually provide this feature. My version on Fedora 19 does not. Same too on Ubuntu 13.04.
– slm♦
May 12 '14 at 1:22
You're right!, I was so happy of having found something useful, I forgot I was in my mac at home.
– Kieleth
May 13 '14 at 19:34
add a comment |
up vote
0
down vote
If you need the averages for a period of time of a specific process, try the accumulative -c option of top:
top -c a -pid PID
"-c a" found in top for Mac 10.8.5.
For Scientific Linux, the option is -S, that can be set interactively.
You'll likely want to add further details around which version(s) oftop
actually provide this feature. My version on Fedora 19 does not. Same too on Ubuntu 13.04.
– slm♦
May 12 '14 at 1:22
You're right!, I was so happy of having found something useful, I forgot I was in my mac at home.
– Kieleth
May 13 '14 at 19:34
add a comment |
up vote
0
down vote
up vote
0
down vote
If you need the averages for a period of time of a specific process, try the accumulative -c option of top:
top -c a -pid PID
"-c a" found in top for Mac 10.8.5.
For Scientific Linux, the option is -S, that can be set interactively.
If you need the averages for a period of time of a specific process, try the accumulative -c option of top:
top -c a -pid PID
"-c a" found in top for Mac 10.8.5.
For Scientific Linux, the option is -S, that can be set interactively.
edited May 13 '14 at 19:32
answered May 11 '14 at 22:09
Kieleth
111
111
You'll likely want to add further details around which version(s) oftop
actually provide this feature. My version on Fedora 19 does not. Same too on Ubuntu 13.04.
– slm♦
May 12 '14 at 1:22
You're right!, I was so happy of having found something useful, I forgot I was in my mac at home.
– Kieleth
May 13 '14 at 19:34
add a comment |
You'll likely want to add further details around which version(s) oftop
actually provide this feature. My version on Fedora 19 does not. Same too on Ubuntu 13.04.
– slm♦
May 12 '14 at 1:22
You're right!, I was so happy of having found something useful, I forgot I was in my mac at home.
– Kieleth
May 13 '14 at 19:34
You'll likely want to add further details around which version(s) of
top
actually provide this feature. My version on Fedora 19 does not. Same too on Ubuntu 13.04.– slm♦
May 12 '14 at 1:22
You'll likely want to add further details around which version(s) of
top
actually provide this feature. My version on Fedora 19 does not. Same too on Ubuntu 13.04.– slm♦
May 12 '14 at 1:22
You're right!, I was so happy of having found something useful, I forgot I was in my mac at home.
– Kieleth
May 13 '14 at 19:34
You're right!, I was so happy of having found something useful, I forgot I was in my mac at home.
– Kieleth
May 13 '14 at 19:34
add a comment |
up vote
0
down vote
If you have a cut-down Linux distribution where top does not have per process (-p) option or related options, you can parse the output of the top command for your process name to get the CPU usage information per process.
while true; do top -bn1 | awk '/your_process_name/ {print $8}' ; sleep 1; done
8 represents the CPU usage per process in the output of the top command in my embedded Linux distribution
add a comment |
up vote
0
down vote
If you have a cut-down Linux distribution where top does not have per process (-p) option or related options, you can parse the output of the top command for your process name to get the CPU usage information per process.
while true; do top -bn1 | awk '/your_process_name/ {print $8}' ; sleep 1; done
8 represents the CPU usage per process in the output of the top command in my embedded Linux distribution
add a comment |
up vote
0
down vote
up vote
0
down vote
If you have a cut-down Linux distribution where top does not have per process (-p) option or related options, you can parse the output of the top command for your process name to get the CPU usage information per process.
while true; do top -bn1 | awk '/your_process_name/ {print $8}' ; sleep 1; done
8 represents the CPU usage per process in the output of the top command in my embedded Linux distribution
If you have a cut-down Linux distribution where top does not have per process (-p) option or related options, you can parse the output of the top command for your process name to get the CPU usage information per process.
while true; do top -bn1 | awk '/your_process_name/ {print $8}' ; sleep 1; done
8 represents the CPU usage per process in the output of the top command in my embedded Linux distribution
edited Mar 3 '17 at 5:45
answered Mar 3 '17 at 5:37
Razan Paul
1114
1114
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f554%2fhow-to-monitor-cpu-memory-usage-of-a-single-process%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What memory statistics do you want? There are lots of them.
– vwduder
Jan 13 '11 at 11:33
Memory usage over a given time frame, current usage, maximum usage, average.
– Josh K
Jan 13 '11 at 11:33