Home News WebDev Links About Garden Notes LAMP Stack Using Git Using SSH/SFTP Linux Commands

Linux ps

How to keep track of how heavily a process is loading the cpu on a server.

Resources

Workflow

  1. set up bash file to run ps and append data into file ~/monitor.process

	#!/bin/bash
	# filename monitor.process

	# run ps, append to monitorLogfile
	ps -f -U mysql -u mysql --no-headers >> mysql.monitor
  1. Modify to add datetime stamp
  2. 
    	#!/bin/bash
    	# monitorlogfile mysql.monitor
    	
    	# run ps, filter, append with datestamp to monitorLogfile
    	
    	DATESTAMP=`date +%y%m%d-%H%M%S`
    	MONITOR=`ps -f -U mysql -u mysql --no-headers`
    	echo $DATESTAMP $MONITOR >> mysql.monitor
    		
    
  3. Make file executable

	$ chmod 744 ~/monitor.process
	$
  1. Set up cron to run this at set interval

	# use 'crontab -e' to add to your crontab
	$ crontab -e

	# new crontab entry:
	# run monitor.process 4 times per hour
	*/15 * * * * /home/ubuntu/monitor.process
  1. Do something with log file, e.g.,
    1. Alert if getting too large
    2. Roll log file periodically, e.g.,
      1. Use cron to once a week rename log to dated log file (like mysql.monitor.210323).
      2. Next cron run will create a new log file.
      3. make sure not to run on 0,15,30,45 minute which could conflict with the 4 time per hour routing, above.