We're having a lot of cronjobs on our server and looking for a way to manage them.
A lot of functionality is covered but one thing im still working on, is a way to list all the cronjobs that are currently running.
The following does the job kind of:
class Cronjob{
static private function stringToArray($jobs = '') {
$array = explode("
", trim($jobs)); // trim() gets rid of the last
foreach ($array as $key => $item) {
if ($item == '') {
unset($array[$key]);
}
}
return $array;
}
static public function getAllJobsRunning(){
$output = shell_exec("ps -ef | grep apache");
return self::stringToArray($output);
}
}
But this returns more tasks and not only the cronjobs like
apache 6018 6015 0 Sep12 ? 00:00:02 /usr/sbin/httpd -DFOREGROUND
apache 6022 6015 0 Sep12 ? 00:00:08 /usr/sbin/httpd -DFOREGROUND
apache 6023 6015 0 Sep12 ? 00:00:07 /usr/sbin/httpd -DFOREGROUND
apache 7022 6015 0 14:14 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
root 8544 8485 0 14:41 pts/1 00:00:00 su apache
apache 8545 8544 0 14:41 pts/1 00:00:00 bash
apache 8642 8640 0 14:43 ? 00:00:00 /bin/sh -c /usr/bin/php /path_to_file/something.php > /var/log/cronlog/dailylogs.log 2>&1
apache 8643 8642 0 14:43 ? 00:00:04 /usr/bin/php /path_to_file/something.php
apache 11132 15438 0 14:58 ? 00:00:00 sh -c ps -ef | grep apache
apache 11133 11132 0 14:58 ? 00:00:00 ps -ef
apache 11134 11132 0 14:58 ? 00:00:00 grep apache
apache 11454 6015 0 Sep12 ? 00:00:06 /usr/sbin/httpd -DFOREGROUND
apache 15438 6015 0 11:29 ? 00:00:02 /usr/sbin/httpd -DFOREGROUND
apache 17761 6015 0 11:51 ? 00:00:01 /usr/sbin/httpd -DFOREGROUND
apache 18274 6015 0 Sep13 ? 00:00:06 /usr/sbin/httpd -DFOREGROUND
root 18911 11790 0 12:04 pts/0 00:00:00 su apache
apache 18912 18911 0 12:04 pts/0 00:00:00 bash
apache 21863 6015 0 12:42 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 24419 6015 0 Sep13 ? 00:00:02 /usr/sbin/httpd -DFOREGROUND
apache 24436 6015 0 Sep13 ? 00:00:02 /usr/sbin/httpd -DFOREGROUND
While im only interested in the:
apache 8642 8640 0 14:43 ? 00:00:00 /bin/sh -c /usr/bin/php /path_to_file/something.php > /var/log/cronlog/dailylogs.log 2>&1
apache 8643 8642 0 14:43 ? 00:00:04 /usr/bin/php /path_to_file/something.php
Now I could write a regex that filters out the lines I want but Im wondering if there isnt a easier way to do this.