2011-11-03 171 views
0

我正在一個centos服務器上,我必須從不同的服務器下載數據到tbz格式的centos服務器,然後我必須提取它,完成提取後,我必須導入數據從它到mysql數據庫表。執行從php腳本centos命令

我在做什麼是從一個繞大小700MB,但是當我執行命令解壓它和其他什麼也沒有發生在服務器 獲取數據。

$files = array('20111101.tbz', '20111101.tbz.md5' ,'20111102.tbz' ,'20111102.tbz.md5') ; 

    //folder names of the folders formed 
    $folder1 = "20111101"; 
    $folder3 = "20111102"; 

    //command to extract all the downloaded .tbz files 
    $command1 = "cd /var/www/html/mywork1/mywork2/mywork3/"; 
    exec($command1." 2>&1", $output); 
    $command2 = "tar -xjf ".$files[0]; 
    exec($command2." 2>&1", $output); 
    $command4 = "tar -xjf ".$files[2]; 
    exec($command4." 2>&1", $output); 

    //command to populate the data into the database 
    $command6 = "cd /var/www/html/mywork1/mywork2/mywork3/mywork4"; 
    exec($command6." 2>&1", $output); 
    $command7 = "./runit.py /var/www/html/mywork1/mywork2/mywork3/$folder1"; 
    exec($command7." 2>&1", $output); 
    $command9 = "./runit.py /var/www/html/mywork1/mywork2/mywork3/$folder3"; 
    exec($command9." 2>&1", $output); 

    //command to remove the folders created after population of data 
    $command10 = "rm -rf $folder1"; 
    exec($command10." 2>&1", $output); 
    $command11 = "rm -rf $folder3"; 
    exec($command11." 2>&1", $output); 

    foreach ($files as $file) 
    { 
     //command to remove all .tbz files downloaded. 
     $command12 = "rm -rf $file"; 
     exec($command12." 2>&1", $output); 
    } 

我只是執行命令,但它沒有做任何事情。但是當我在服務器上手動嘗試所有這些命令時,它們工作正常。任何人都可以指導我嗎?應該採取什麼正確的方法來做到這一點?

+1

爲什麼這個標籤爲「Python」? – Johnsyweb

+0

你的問題是,你不知道發生了什麼。所以請啓用'display_errors',將'error_reporting'設置爲'-1'。在'exec'('echo $ output;')之後顯示命令的輸出。您的Web服務器用戶是否具有對這些目錄的寫入權限? – vstm

+0

不回答你的問題,但你有沒有嘗試把所有這些命令放在Bash腳本中,並從PHP腳本中運行它? –

回答

1

你不能exec("cd /some/dir");,因爲這將啓動一個shell,它會去這個目錄,然後退出。對於你的PHP程序,它什麼都不做—工作目錄不會改變。您可能需要登錄chdir('/some/dir') or die("Cannot change directory");