{"id":20,"date":"2004-03-20T22:32:57","date_gmt":"2004-03-20T20:32:57","guid":{"rendered":"http:\/\/frenchfragfactory.net\/ozh\/archives\/2004\/03\/20\/ps-monitor\/"},"modified":"2007-05-09T23:31:56","modified_gmt":"2007-05-09T21:31:56","slug":"ps-monitor","status":"publish","type":"post","link":"https:\/\/planetozh.com\/blog\/2004\/03\/ps-monitor\/","title":{"rendered":"ps monitor"},"content":{"rendered":"<p>This script logs CPU load for a given process, every n seconds into a file<br \/>\n<strong>Requires <\/strong>: bash, ps, awk<br \/>\n<strong>Examples <\/strong>:<br \/>\n<em>psmon &lt;process name> [delay [logfile]]<\/em><br \/>\npsmon sshd<strong>&rarr;<\/strong> logs %CPU load from process sshd, every 5 seconds, to file psmon.log.[day]<br \/>\npsmon q3ded 10 q3ded.log <strong>&rarr;<\/strong> logs %CPU load from process q3ded, every 10 seconds, to file q3ded.log<br \/>\n<!--more--><\/p>\n<div class=\"igsh-code-box\" id=\"ig-sh-1\"><pre class=\"language-none line-numbers\" data-no-optimize=\"1\" data-cfasync=\"false\"><code class=\"language-none\">#!\/bin\/bash\r\n\r\nif [ -z $1 ] ; then\r\n\techo &quot;Monitor a cpu load for a given process, every n seconds&quot;\r\n\techo &quot;Usage : `basename $0` &lt;process name&gt; [delay [logfile]]&quot;\r\n\techo &quot;   [delay] : defaults to 5 seconds&quot;\r\n\techo &quot;   [logfile] : defaults to psmon.log.yymmdd.hhmmss&quot;\r\n\techo &quot;   if you specify a logfile, you MUST specify a delay&quot;\r\n\techo &quot;Example : psmon q3ded 10 q3dedmonitor.log&quot;\r\n\techo &quot;Example : psmon q3ded&quot;\r\n\texit 0\r\nfi\r\n\r\nPROCESS=$1\r\n\r\nif $(ps -C &quot;$PROCESS&quot;&gt;\/dev\/null) ; then\r\n\t# process exists\r\n\tWARNING=&quot;&quot;\r\nelse\r\n\t# process doesnt exist\r\n\tWARNING=&quot;Warning : $PROCESS NOT running at the moment !&quot;\r\nfi\r\n\r\n\r\nLOGFILE=$3\r\nif [ -z $3 ] ; then\r\n\tLOGFILE=psmon.log.$(date +%Y%m%d.%H%M%S)\r\nfi\r\n\r\nDELAY=$2\r\nif [ -z $2 ] ; then\r\n\tDELAY=5\r\nfi\r\n\r\nC=&quot;\\033[1m&quot;\r\nN=&quot;\\033[0m&quot;\r\n\r\necho -e &quot;${N}Monitoring $C$PROCESS$N cpu load, every $C$DELAY$N s, to file $C$LOGFILE&quot;\r\necho -e $WARNING\r\n\r\necho &quot;# psmon logfile&quot; &gt; $LOGFILE\r\necho &quot;# Process name : $PROCESS&quot; &gt;&gt; $LOGFILE\r\necho &quot;# Started : $(date +%D\\ \\@\\ %X)&quot; &gt;&gt; $LOGFILE\r\necho &quot;&quot; &gt;&gt; $LOGFILE\r\n\r\n# Now logging the stuff\r\n\r\nwhile true; do\r\n\tps -C &quot;$PROCESS&quot; -o &quot;pcpu&quot; | awk &#039;{a+=$1} END {print a}&#039; &gt;&gt; $LOGFILE\r\n\tsleep $DELAY\r\ndone<\/code><\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>PS monitoring script<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[5,2,3],"class_list":["post-20","post","type-post","status-publish","format-standard","hentry","tag-bash","tag-code","tag-linux"],"_links":{"self":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts\/20","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/comments?post=20"}],"version-history":[{"count":0,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts\/20\/revisions"}],"wp:attachment":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/media?parent=20"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/categories?post=20"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/tags?post=20"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}