Changeset 12200

Show
Ignore:
Timestamp:
05/30/11 04:14:58 (2 years ago)
Author:
mbrevda
Message:

re #4593 - add a function to help set crontabs more properly. Still a far cry from a proper cron manager, but better than nothing. Not sure this is the correct file, feel free to locate if you feel its nesesary

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • freepbx/trunk/amp_conf/htdocs/admin/libraries/utility.functions.php

    r12167 r12200  
    363363  return $contents; 
    364364} 
     365 
     366/** 
     367 * function edit crontab 
     368 * short Add/removes stuff rom conrtab 
     369 * long Use this function to programmatically add/remove data from the crontab 
     370 * will always run as the asterisk user 
     371 * 
     372 * @pram string 
     373 * @pram mixed 
     374 * @returns bool 
     375 */ 
     376 
     377function edit_crontab($remove = '', $add = '') { 
     378  global $amp_conf; 
     379  $cron_out = array(); 
     380  $cron_add = false; 
     381 
     382  //if were running as root (i.e. uid === 0), use the asterisk users crontab. If were running as the asterisk user,  
     383  //that will happen automatically. If were anyone else, this cron entry will go the current user 
     384  //and run as them 
     385  $current_user = posix_getpwuid(posix_geteuid()); 
     386  if ($current_user['uid'] === 0) { 
     387    $cron_user = '-u' . $amp_conf['AMPASTERISKWEBUSER'] . ' '; 
     388  } else { 
     389    $cron_user = ''; 
     390  } 
     391 
     392  //get all crontabs 
     393  $exec = '/usr/bin/crontab -l ' . $cron_user; 
     394  exec($exec, $cron_out, $ret); 
     395 
     396  //make sure the command was executed successfully before continuing 
     397  if ($ret > 0) { 
     398    return false; 
     399  } 
     400 
     401  //remove anythign that nteeds to be removed 
     402  foreach ($cron_out as $my => $c) { 
     403    //ignore comments 
     404    if (substr($c, 0, 1) == '#') { 
     405      continue; 
     406    } 
     407 
     408    //remove blank lines 
     409    if (!$c) { 
     410      unset($cron_out[$my]); 
     411    } 
     412 
     413    //remove $remove 
     414    if ($remove) { 
     415      if (strpos($c, $remove)) { 
     416        unset($cron_out[$my]); 
     417      } 
     418    } 
     419  } 
     420 
     421  //if we have $add and its an array, parse it & fill in the missing options 
     422  //if its a string, add it as is 
     423  if($add) { 
     424    if (is_array($add)) { 
     425      if (isset($add['command'])) { 
     426        $cron_add['minute']   = isset($add['minute']) ? $add['minute']  : '*'; 
     427        $cron_add['hour']   = isset($add['hour']) ? $add['hour']    : '*'; 
     428        $cron_add['dom']    = isset($add['dom'])  ? $add['dom']   : '*'; 
     429        $cron_add['month']    = isset($add['month'])  ? $add['month']   : '*'; 
     430        $cron_add['dow']    = isset($add['dow'])  ? $add['dow']   : '*'; 
     431        $cron_add['command']  = $add['command']; 
     432        $cron_add = implode(' ', $cron_add); 
     433      } else { 
     434        //no command? No cron! 
     435        $cron_add = ''; 
     436      } 
     437    } else { 
     438      //no array? Just use the string 
     439      $cron_add = $add; 
     440    } 
     441  } 
     442 
     443  //if we have soemthing to add 
     444  if ($cron_add) { 
     445    $cron_out[] = $cron_add; 
     446  } 
     447 
     448  //write out crontab 
     449  $exec = '/bin/echo "' . implode("\n", $cron_out) . '" | /usr/bin/crontab ' . $cron_user . '-'; 
     450  exec($exec, $out_arr, $ret); 
     451 
     452  return ($ret > 0 ? false : true); 
     453}