system(), exec() only partially runs script and returns 0.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
colombiunpride
Forum Newbie
Posts: 4
Joined: Wed Jun 10, 2009 3:25 pm

system(), exec() only partially runs script and returns 0.

Post by colombiunpride »

Hey all, I've spent the past few days trying to figure out how to get a Perl scripts and Batch commands to work properly from PHP. That is I've tried system(), exec(), and passthru().
Basic ones like "dir" work fine.

Either the script doesn't execute at all or it only executes and prints part of the script.. Here is a specific example. I'm running XAMPP, so I have PHP, MySql, and Apache.

On loading the PHP page part of he script will execute, I know so because the attatched code snippet succesfully copies a file and creates another one in addition to the copied one.
The print statement before the for loop prints as well, but the print statement in the for loop doesn't. It just seems like the execution of the code stops there.

I get a "0" returned from the call to system() as well. It seems like the script partially executes, fails and then returns 0.

Thanks ahead of time for the assistance.

Code: Select all

 
<?php
 
$command = 'test.pl';
$myOutput = system($command, $rc);
echo "$rc";
echo "<pre>".$myOutput."</pre>";
 
?>

Code: Select all

 
use File::Copy;
 
#Read in file and print to command line
 
$infFile = "oemsetup.inf";
open(INFO, $infFile);
@lines = <INFO>;
close(INFO);
 
$infTable = "id2.csv";
$valid = "valid_PCI";
 
open(INFO,$infTable);
@table = <INFO>;
close(INFO);
 
open VALID, ">$valid.csv";
 
@table2=@table;
 
open NEW, ">pciENTRY.csv";
 
$table2[0] =~ s//,Projects,CO,DeviceDesc/;
 
$found=0;
 
for($counter=1;$counter<@table2;$counter++){
    
    $table2[$counter] =~ s//,/;
}
[color=#FF0040]print "made it to here \n";[/color]
 
#Code stops working here, no error message, just doesnt execute onward.
 
foreach $line  (@lines){
[color=#FF0000]print "this won't print \n";[/color]
    local($VID, $tVID,$SSID,$tSSID);
 
Last edited by Benjamin on Thu Jun 11, 2009 7:16 pm, edited 1 time in total.
Reason: Changed code type from text to php.
colombiunpride
Forum Newbie
Posts: 4
Joined: Wed Jun 10, 2009 3:25 pm

Re: system(), exec() only partially runs script and returns 0.

Post by colombiunpride »

To see if the problem is my script or how my server is configured, does anyone mind e-mailing me some simple working PHP code, as well as a command line script, or PERL script that does seveal things? (Can be anything, create a file, write to one, delete one, do some stuff in a for loop)

I have a Windows and Linux machine to test on. If it turns out that the provided code doesn'twork on my setup I can better figure out whats different on my setup.


Fernando.Arias@lsi.com

Thanks all!

-Fernando
colombiunpride
Forum Newbie
Posts: 4
Joined: Wed Jun 10, 2009 3:25 pm

Re: system(), exec() only partially runs script and returns 0.

Post by colombiunpride »

I posted a new thread by accident, but figured out my problem. My script was trying to open a file that was non existant. So it exited.

I think the majority of issues with these functions is that they seem to just not work.
In reality, the usual case is that script is launched and exits due to an error before it starts or in the middle of running.

What helped me was the following.

1.) Run the script from the local machine not usnig PHP and verify that they work to begin with.

2.) Pipe the output to a error log when calling PHP to see if the script exited once being called.
My script was failing and all I saw was the PHP page post back without any details, making my initial attempts to solving the issue near impossible.

The following worked for me.


I'm new to this but using "1>" piped the standard output, and using "2>" piped any errors I had. Someone please confirm if I am right.

PHP Code

Code: Select all

 
<?php
$WshShell = new COM("WScript.Shell");
$oExec = $WshShell->Run("cmd /C perl C:\\indigoampp\\apache-2.2.11\\htdocs\\redo.pl [color=#FF0000][b]1>[/b][/color] C:\indigoampp\apache-2.2.11\htdocs\log.txt", 0, true);
 
echo "<pre>Continued</pre>";
?>
 
Perl Script

Code: Select all

 
print "Hello from perl!";
 
for($i=0;$i<10;$i++) {
    sleep(1);
    print "<br>Another one!</br>\n";
}
 

It basically runs the PHP page, launches the script and posts to the broswer "continue". The perl script runs in the background printed "Another One" ten times. That printed output is piped to a log.txt file.

I had to use "\\" instead of "\" because I was getting a file not found error when trying to launch my perl script.

I had to :banghead: for about 4 days trying desperately to figure something this simple out.
Lame. haha

Hope this saves someone from the frustration I went through.
Last edited by Benjamin on Thu Jun 11, 2009 7:17 pm, edited 1 time in total.
Reason: Changed code type from text to php.
Post Reply