In: , ,
On: 2004 / 03 / 20
Shorter URL for this post: http://ozh.in/r

This script logs CPU load for a given process, every n seconds into a file
Requires : bash, ps, awk
Examples :
psmon <process name> [delay [logfile]]
psmon sshd logs %CPU load from process sshd, every 5 seconds, to file psmon.log.[day]
psmon q3ded 10 q3ded.log logs %CPU load from process q3ded, every 10 seconds, to file q3ded.log

  1. #!/bin/bash
  2.  
  3. if [ -z $1 ] ; then
  4.     echo "Monitor a cpu load for a given process, every n seconds"
  5.     echo "Usage : `basename $0` <process name> [delay [logfile]]"
  6.     echo "   [delay] : defaults to 5 seconds"
  7.     echo "   [logfile] : defaults to psmon.log.yymmdd.hhmmss"
  8.     echo "   if you specify a logfile, you MUST specify a delay"
  9.     echo "Example : psmon q3ded 10 q3dedmonitor.log"
  10.     echo "Example : psmon q3ded"
  11.     exit 0
  12. fi
  13.  
  14. PROCESS=$1
  15.  
  16. if $(ps -C "$PROCESS">/dev/null) ; then
  17.     # process exists
  18.     WARNING=""
  19. else
  20.     # process doesnt exist
  21.     WARNING="Warning : $PROCESS NOT running at the moment !"
  22. fi
  23.  
  24.  
  25. LOGFILE=$3
  26. if [ -z $3 ] ; then
  27.     LOGFILE=psmon.log.$(date +%Y%m%d.%H%M%S)
  28. fi
  29.  
  30. DELAY=$2
  31. if [ -z $2 ] ; then
  32.     DELAY=5
  33. fi
  34.  
  35. C="\033[1m"
  36. N="\033[0m"
  37.  
  38. echo -e "${N}Monitoring $C$PROCESS$N cpu load, every $C$DELAY$N s, to file $C$LOGFILE"
  39. echo -e $WARNING
  40.  
  41. echo "# psmon logfile" > $LOGFILE
  42. echo "# Process name : $PROCESS" >> $LOGFILE
  43. echo "# Started : $(date +%D\ \@\ %X)" >> $LOGFILE
  44. echo "" >> $LOGFILE
  45.  
  46. # Now logging the stuff
  47.  
  48. while true; do
  49.     ps -C "$PROCESS" -o "pcpu" | awk '{a+=$1} END {print a}' >> $LOGFILE
  50.     sleep $DELAY
  51. done

Shorter URL

Want to share or tweet this post? Please use this short URL: http://ozh.in/r

Metastuff

This entry "ps monitor" was posted on 20/03/2004 at 10:32 pm and is tagged with , ,
Watch this discussion : Comments RSS 2.0.

No Comment yet

  1. TomBoss says:

    FirstPost
    (au cas ou)

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>
Gravatars: Curious about the little images next to each commenter's name ? Go to Gravatar and sign for a free account
Spam: Various spam plugins may be activated. I'll put pins in a Voodoo doll if you spam me.

Read more ?