Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Slow cronjobs on Cent OS 5

I have 1 cronjob that runs every 60 minutes but for some reason, recently, it is running slow.

Env: centos5 + apache2 + mysql5.5 + php 5.3.3 / raid 10/10k HDD / 16gig ram / 4 xeon processor

Here's what the cronjob do:

  1. parse the last 60 minutes data

    a) 1 process parse user agent and save the data to the database

    b) 1 process parse impressions/clicks on the website and save them to the database

  2. from the data in step 1

    a) build a small report and send emails to the administrator/bussiness

    b) save the report into a daily table (available in the admin section)

I see now 8 processes (the same file) when I run the command ps auxf | grep process_stats_hourly.php (found this command in stackoverflow)

Technically I should only have 1 not 8.

Is there any tool in Cent OS or something I can do to make sure my cronjob will run every hour and not overlapping the next one?

Thanks

like image 802
Tech4Wilco Avatar asked Oct 25 '11 18:10

Tech4Wilco


1 Answers

Your hardware seems to be good enough to process this.

1) Check if you already have hanging processes. Using the ps auxf (see tcurvelo answer), check if you have one or more processes that takes too much resources. Maybe you don't have enough resources to run your cronjob.

2) Check your network connections: If your databases and your cronjob are on a different server you should check whats the response time between these two machines. Maybe you have network issues that makes the cronjob wait for the network to send the package back.

You can use: Netcat, Iperf, mtr or ttcp

3) Server configuration Is your server is configured correctly? Your OS, MySQL are setup correctly? I would recommend to read these articles:

http://www3.wiredgorilla.com/content/view/220/53/

http://www.vr.org/knowledgebase/1002/Optimize-and-disable-default-CentOS-services.html

http://dev.mysql.com/doc/refman/5.1/en/starting-server.html

http://www.linux-mag.com/id/7473/

4) Check your database: Make sure your database has the correct indexes and make sure your queries are optimized. Read this article about the explain command

If a query with few hundreds thousands of record takes times to execute that will affect the rest of your cronjob, if you have a query inside a loop, even worse.

Read these articles:

http://dev.mysql.com/doc/refman/5.0/en/optimization.html

http://20bits.com/articles/10-tips-for-optimizing-mysql-queries-that-dont-suck/

http://blog.fedecarg.com/2008/06/12/10-great-articles-for-optimizing-mysql-queries/

5) Trace and optimized PHP code? Make sure your PHP code runs as fast as possible.

Read these articles:

http://phplens.com/lens/php-book/optimizing-debugging-php.php

http://code.google.com/speed/articles/optimizing-php.html

http://ilia.ws/archives/12-PHP-Optimization-Tricks.html

A good technique to validate your cronjob is to trace your cronjob script: Based on your cronjob process, put some debug trace including how much memory, how much time it took to execute the last process. eg:

<?php  echo "\n-------------- DEBUG --------------\n"; echo "memory (start): " . memory_get_usage(TRUE) . "\n";  $startTime = microtime(TRUE); // some process $end = microtime(TRUE);  echo "\n-------------- DEBUG --------------\n"; echo "memory after some process: " . memory_get_usage(TRUE) . "\n"; echo "executed time: " . ($end-$start) . "\n"; 

By doing that you can easily find which process takes how much memory and how long it takes to execute it.

6) External servers/web service calls Is your cronjob calls external servers or web service? if so, make sure these are loaded as fast as possible. If you request data from a third-party server and this server takes few seconds to return an answer that will affect the speed of your cronjob specially if these calls are in loops.

Try that and let me know what you find.

like image 63
Book Of Zeus Avatar answered Sep 23 '22 01:09

Book Of Zeus