Created
February 26, 2012 17:17
-
-
Save s4l1h/1917827 to your computer and use it in GitHub Desktop.
Bir komutu saniye cinsinden istediğimiz sürede çalıştırmaya yarar.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
ini_set('max_execution_time', 0); | |
set_time_limit(0); | |
header('Content-type: text/html; charset=utf-8'); | |
// php çalıştırılma çekli | |
if (PHP_SAPI !== 'cli') { | |
echo "Bu Uygulama Sadece Konsoldan Çalışır"; | |
exit; | |
} | |
// Zaman | |
date_default_timezone_set('Europe/Istanbul'); | |
// Log Dosyası | |
define('log_dosyasi', 'CronFixLog.txt'); | |
// Son Çalıştırılma zamanını yazacağız | |
define('zaman_dosyasi', 'CronFixTime.txt'); | |
// Komut Çalıştırıldığında zaman farklı oluşuyorsa o farkı süreden düşsünmü? | |
define('zaman_farki', TRUE); | |
// Yardım Fonksiyonu | |
function help() | |
{ | |
echo "Örnek Kullanım\n"; | |
echo basename(__FILE__) . ' -command="/usr/bin/wget http://www.blabla.com/a.txt" -time="10" &'; | |
echo "\nTime Değeri Saniye Cinsinden"; | |
} | |
// Gönderilen argv sayısı 3'mü? | |
if (count($argv) != 3) { | |
help(); | |
exit; | |
} | |
// Birincisi Komut | |
$command = $argv['1']; | |
// İkincisi Zaman | |
$time = $argv['2']; | |
// Komutu Check Edelim | |
if (preg_match('@^-command=(.*)@', $command, $command_cikti) == FALSE) { | |
help(); | |
exit; | |
} | |
// Zamanı Check Edelim | |
if (preg_match('@^-time=(\d+)@', $time, $time_cikti) == FALSE) { | |
help(); | |
exit; | |
} | |
// Komut | |
$command = $command_cikti['1']; | |
// Süre | |
$time = $time_cikti['1']; | |
// Yeni Komut Çıktısını Log Dosyasına Yazacak | |
$yeni_komut = $command . " > " . log_dosyasi . " 2>&1"; | |
// Oke Mesajı Gönderelim | |
echo "Oke! CronFix Her '" . $time . "' Saniyede Bir '" . $command . "' Komutunu Çalıştıracak Şekilde Başlatıldı!\n"; | |
// Süreki Bir Döngü | |
while (1) { | |
if (zaman_farki == TRUE) { | |
$stime = time(); | |
} | |
// Komut Çalıştıralım | |
exec($yeni_komut); | |
// Son çalıştırılma zamanını dosyaya yazalım | |
$dosya = fopen('CronFixTime.txt', "w+"); | |
fwrite($dosya, date('Y-m-d H:i:s', time()) . " Tarihinde " . $command . " Komutu Çalıştırıldı!"); | |
fclose($dosya); | |
if (zaman_farki == TRUE) { | |
$ftime = time(); | |
// Bu İşlemler sırasında oluşan zaman farkını düşelim | |
$fark = $ftime - $stime; | |
$ntime = $time - $fark; | |
$ntime = $ntime > 0 ? $ntime : 0; | |
// $ntime kadar bekle | |
sleep($ntime); | |
} else { | |
// $time kadar bekle | |
sleep($time); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment