duanhaodi4809 2011-11-24 12:39
浏览 77
已采纳

如何用php一个接一个地执行.sh文件

I need to run a series of six .sh files on the server.

An example of one of the .sh files:

wget ftp://xxxxxx:xxxxxx@ftp.interhome.com/accommodation.xml.zip
unzip accommodation.xml.zip
php accommodation.php
rm -rf accommodation.xml.zip
rm -rf accommodation.xml

I tried running the following from a php file:

echo shell_exec('sh accomodation.sh');

Which was stupid because the file appears to execute repeatedly and I think I've just taken down the server. Whoops.

I've inherited this site and have never used .sh files before. I'm also a php novice.

How would I go about running the files only once and then running the next?

Many thanks

  • 写回答

1条回答 默认 最新

  • dongsi3826 2011-11-24 13:03
    关注

    you can do all this from within PHP, you do not need any shell-script.

    /* get the file via ftp */
    // connect to server
    $ftp = ftp_connect('ftp.interhome.com');
    // login
    $login = ftp_login($ftp,"username","password");
    // download file to tmp.zip
    $file = ftp_get($ftp, 'tmp.zip', 'accommodation.xml.zip', FTP_BINARY);
    // disconnect from server
    ftp_close($ftp);
    
    /* unzip the file */
    // new zip-instance
    $zip = new ZipArchive;
    // open downloaded file
    $res = $zip->open(’tmp.zip’);
    // check if file is readable
    if ($res === TRUE) {
      // extract to current directory
      $zip->extractTo(’./’);
      // close zip-file
      $zip->close();
    }
    
    /* your code from accommodation.php goes here */
    
    // delete files
    unlink('tmp.zip');
    unlink('accommodation.xml');
    

    voila

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line