从php脚本执行centos命令
我正在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能
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");
.