2011-11-03 174 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");