Skip to end of metadata
Go to start of metadata

You can limit number of cron jobs, if you use PHP for DevOps software in CentOS 7, which can be easily implemented by checking process by shell_exec.

Below function enables you to check number of process that based on "PHP". 

function count_process($process_name)
{
	// check process in the process list
	$bot_count_cmd = "ps aux | grep \"{$process_name}\" | wc -l";
	$bot_count=shell_exec($bot_count_cmd);
	$bot_count=intval( trim($bot_count) )-2; // you may need to tune the number based on your running environment

	return $bot_count;
}

Note that the working environment above is based on that all the commands are remotely managed and executed by ssh, so all the PHP functions are composed of "bash -c cd..." and the actual php functions like below that is why $bot_count is divided by two.

root      1276  0.0  0.0 113284  1604 ?        Ss   17:45   0:00 bash -c cd "/pub/production/_bot" && php cdn.php
root      1295  1.5  0.1 539480 44648 ?        S    17:45   0:45 php cdn.php
root      2632  0.0  0.0 113284  1600 ?        Ss   17:50   0:00 bash -c cd "/pub/production/_bot" && php cdn.php
root      2651  0.5  0.1 613684 44888 ?        S    17:50   0:13 php cdn.php
root      4062  0.0  0.0 113284  1596 ?        Ss   15:50   0:00 bash -c cd "/pub/production/_bot" && php cdn.php
root      4086  1.0  0.1 539388 46224 ?        D    15:50   1:44 php cdn.php

Below is an application example to run certain function by the number of bots running on the system.

// check cdn.php in the process list
$bot_count=process_count("php cdn.php");

// we will just keep max 5 cdn bots
if (($bot_count)<5) $m_cms->cdn_update( 60*20);

Below is an application example to exit by the number of bots running on the system.

// check encoding.php in the process list
$bot_count=process_count("php encoding.php");

// we will just keep max 2 encoding bots
if (($bot_count)>2) exit;

Below is another example to check redis and restart its demon

check_redis.sh
#!/bin/php
<?
function count_process($process_name)
{
        $cmd = "ps aux | grep \"{$process_name}\"";
        $res=shell_exec($cmd);
        echo $res . "\n";

        // check process in the process list
        $bot_count_cmd = "ps aux | grep \"{$process_name}\" | wc -l";
        $bot_count=shell_exec($bot_count_cmd);
        $bot_count=intval( trim($bot_count)-2 ); // you may need to tune the number based on your running environment

        return $bot_count;
}

if (count_process("redis-server")<1)
{
        shell_exec("sudo service redis restart");
}

?>