从php脚本执行centos命令

发布于 2024-12-13 10:09:36 字数 1608 浏览 3 评论 0原文

我正在centos服务器上工作,我必须以tbz格式将数据从不同的服务器下载到centos服务器,然后我必须提取它,提取完成后我必须将数据从它导入到mysql数据库表。

我正在做的是 从服务器获取大约 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);
    }

我只是执行命令,但它没有做任何事情。但是当我在服务器上手动尝试所有这些命令时,它们工作正常。任何人都可以指导我吗?执行此操作的正确方法应该是什么?

I am working on a centos server and i have to download data from different server to the centos server in tbz format, then i have to extract it and after completion of the extraction i have to import data from it to mysql database table.

what i am doing is
Getting data from the server which is around 700MB in size but when i am executing command to extract it and others nothing happens.

$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);
    }

i am simply executing the commands but its not doing anything. But when i try all these commands manually on server they work fine. Can any one guide me on this? What should be the correct approach to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

定格我的天空 2024-12-20 10:09:36

您不能 exec("cd /some/dir"); 因为这会启动一个 shell,该 shell 会转到此目录,然后退出。对于你的 PHP 程序来说,它什么也不做——工作目录不会改变。

您需要chdir('/some/dir')或die(“无法更改目录”);

You can not exec("cd /some/dir"); as this would start a shell, which would go to this directory and then exit. For your PHP program it does nothing — working directory will not change.

You'd need to chdir('/some/dir') or die("Cannot change directory");.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文