Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

On my linux machine there was a script which was accessing mysql withing a while(true) loop. It has caused my cpu usage to 99%-100%. But I couldn't find exactly what script was doing this. Following was the script code.

while (true) {
    $con = mysql_connect("localhost","root","root");
    mysql_select_db("test_db", $con);

    $result = mysql_query("SELECT * FROM hs_hr_employee");

I tried to identify the exact process through the top command. The result was as bellow. enter image description here

Even though this shows mysqld is taking 100% pf CPU usage. this didn't showed me what was the exact script which was causing this issue.

Then I tried ps command to find out any hint on this.

ps -aux | grep mysqld

The output was as follows. enter image description here

It also didn't gave any hint about this running php script. In this kind of scenario how we can troubleshoot exactly to identify the trouble making scripts and commands. Please help me on this.

Thanks in advance.

share|improve this question

migrated from Feb 23 '13 at 22:31

This question came from our site for professional and enthusiast programmers.

Well, mysqld is the process that's using the most CPU. With a command like ps aux --forest (or, more prettily, pstree), you can visualise processes in a tree structure, so you can easily see parentage. – Cairnarvon Feb 23 '13 at 22:02

Tell mysql server to log queries that takes more than X seconds to complete.

Mysql Slow Query Log

share|improve this answer
Absolutely - and use "explain" to show how the request is broken down. The OP's problem is not the process consuming the resources - which is correctly identified as MySQL, rather the way the process is doing its calculations. – davidgo Feb 23 '13 at 22:37
If the OP can identify the query, then he can identify the script that generate it and try to fix the query or the way the script build the query itself – Davide Berra Feb 24 '13 at 9:02
sudo apt-get install powertop
sudo powertop

Standard package is version 1.x and gives percentage CPU usage of every process in cool list, newer versions like 2.x show power consumption YAY (only for Intel chips :/)

share|improve this answer

You must log in to answer this question.