gpt4 book ai didi

php - 从 php 脚本执行 centos 命令

转载 作者:行者123 更新时间:2023-12-04 19:39:13 27 4
gpt4 key购买 nike

我正在使用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);
}

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

最佳答案

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

您需要 chdir('/some/dir') or die("Cannot change directory"); .

关于php - 从 php 脚本执行 centos 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7990417/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com