Within my PHP script I want to execute an external file on my system (called MODEL).
I checked around on the web and saw that I need to use either exec() or shell_exec().
This is what I coded:
// Now we need to make the model execute
$cmd = escapeshellcmd( "./path/to/MODEL" ) ;
exec( $cmd ) ;
I also tried:
// Now we need to make the model execute
$cmd = escapeshellcmd( "/path/to/MODEL" ) ;
exec( $cmd ) ;
(removing the '.' in front of the path)
I also tried both versions with the escapeshellcmd removed. When I echo out the command it looks fine, but the system is not executing the command.
I checked my php installation and it says that safe mode is OFF:
safe_mode Off Off
safe_mode_exec_dir no value no value
safe_mode_gid Off Off
safe_mode_include_dir no value no value
The only other thing I can think of is that the php/apache user has not got permission to execute the model. However, ls-l shows the following permissions:
-rwxr-xr-x
This means that EVERYONE can execute the model.
Any ideas anyone? All help appreciated! Thanks!have you try without the escapeshellcmd?Yes.
Originally posted by tatlar
I also tried both versions with the escapeshellcmd removed. When I echo out the command it looks fine, but the system is not executing the command.Just trying to even get the system to do a simple 'ls' command does not ouput anything:
$cmd = "ls" ;
if( !exec( $cmd, $huh ) {
print "Could not execute!" ;
}
print_r( $huh );
FYI the model is in the CGI directory and so is globally executable.
Thanks.just try your code and you miss a closing ) in the if statement
beside that it work on my server
$cmd = "ls" ;
if( !exec( $cmd, $huh )) {
print "Could not execute!" ;
}
print_r( $huh );
sorry about my first reply! seem like i did not read all of your message.Ha!
You were right! Those *** parentheses....
Thats 2 I owe ya...Okay - so if just doing simple commands like ls work okay, and the model is executable, then I can't work out what the problem is....
I can make the command run from the command line.....
okay, now I am getting somewhere....
with this code:
$cmd = "/path/to/MODEL" ;
if( !exec( $cmd, $huh ) ) {
print "Could not execute the command!" ;
}
print_r( $huh );
I get the following output from the print_r function:
Array ( [0] => error opening model file [1] => error reading from model file )
any ideas where I am going wrong? the model file has the following permissions:
-rwxr-xr-x
so I should be able to both open and read the model. hold on, I don't want to open it, just execute it. what do I need to do differently??
Thanks...Array ( [0] => error opening model file [1] => error reading from model file )
that seem to be your script 'MODEL' response...
is your script read a file or something?Originally posted by illogique
that seem to be your script 'MODEL' response...
is your script read a file or something?
Yes, the script takes input parameters from one txt file and then spits out a series of arrays into another txt file. Viz:
input.txt => MODEL => output.txt
The input.txt file has rwx for the world.To get around this (or maybe make it more complicated!!) I wrote a shell script that executes the model. So, essentially I should just be able to execute the shell script and that will execute the model.
The shell (bash) script works fine from the command line, but again does not work from within my php script:
$cmd = "/path/to/shell/script/execute.sh" ;
if( !shell_exec( $cmd ) ) {
print "Could not execute the command!" ;
}is output.txt rwx for all?
are they in the same folder?
have you try cd into another directory then ./path/model to make it run?is output.txt rwx for all?
are they in the same folder?
Yes to both
have you try cd into another directory then ./path/model to make it run?
AHA! In the terminal I just went up one directory and tried to execute the command and got the same error:
error opening model file
error reading from model file
Hmm weird. The directory is rwx for everyone:
drwxrwxrwx
Okay - thanks illogique thats given me something to work from.it seem that your script try to open file from the current directory, not from the script directory...I am still having the problem.
I think the issue is that when I run processes from within PHP (and hence Apache) the server uses user 'www'. User 'www' does not have permission to delete the output.txt file, so the model cannot run.
At least that is my best guess so far....Okay. Easy. I have it working now. I just chowned the output.txt to user www and now it updates fine.
Thanks for all the help illogique!!
@invasion@
I checked around on the web and saw that I need to use either exec() or shell_exec().
This is what I coded:
// Now we need to make the model execute
$cmd = escapeshellcmd( "./path/to/MODEL" ) ;
exec( $cmd ) ;
I also tried:
// Now we need to make the model execute
$cmd = escapeshellcmd( "/path/to/MODEL" ) ;
exec( $cmd ) ;
(removing the '.' in front of the path)
I also tried both versions with the escapeshellcmd removed. When I echo out the command it looks fine, but the system is not executing the command.
I checked my php installation and it says that safe mode is OFF:
safe_mode Off Off
safe_mode_exec_dir no value no value
safe_mode_gid Off Off
safe_mode_include_dir no value no value
The only other thing I can think of is that the php/apache user has not got permission to execute the model. However, ls-l shows the following permissions:
-rwxr-xr-x
This means that EVERYONE can execute the model.
Any ideas anyone? All help appreciated! Thanks!have you try without the escapeshellcmd?Yes.
Originally posted by tatlar
I also tried both versions with the escapeshellcmd removed. When I echo out the command it looks fine, but the system is not executing the command.Just trying to even get the system to do a simple 'ls' command does not ouput anything:
$cmd = "ls" ;
if( !exec( $cmd, $huh ) {
print "Could not execute!" ;
}
print_r( $huh );
FYI the model is in the CGI directory and so is globally executable.
Thanks.just try your code and you miss a closing ) in the if statement
beside that it work on my server
$cmd = "ls" ;
if( !exec( $cmd, $huh )) {
print "Could not execute!" ;
}
print_r( $huh );
sorry about my first reply! seem like i did not read all of your message.Ha!
You were right! Those *** parentheses....
Thats 2 I owe ya...Okay - so if just doing simple commands like ls work okay, and the model is executable, then I can't work out what the problem is....
I can make the command run from the command line.....
okay, now I am getting somewhere....
with this code:
$cmd = "/path/to/MODEL" ;
if( !exec( $cmd, $huh ) ) {
print "Could not execute the command!" ;
}
print_r( $huh );
I get the following output from the print_r function:
Array ( [0] => error opening model file [1] => error reading from model file )
any ideas where I am going wrong? the model file has the following permissions:
-rwxr-xr-x
so I should be able to both open and read the model. hold on, I don't want to open it, just execute it. what do I need to do differently??
Thanks...Array ( [0] => error opening model file [1] => error reading from model file )
that seem to be your script 'MODEL' response...
is your script read a file or something?Originally posted by illogique
that seem to be your script 'MODEL' response...
is your script read a file or something?
Yes, the script takes input parameters from one txt file and then spits out a series of arrays into another txt file. Viz:
input.txt => MODEL => output.txt
The input.txt file has rwx for the world.To get around this (or maybe make it more complicated!!) I wrote a shell script that executes the model. So, essentially I should just be able to execute the shell script and that will execute the model.
The shell (bash) script works fine from the command line, but again does not work from within my php script:
$cmd = "/path/to/shell/script/execute.sh" ;
if( !shell_exec( $cmd ) ) {
print "Could not execute the command!" ;
}is output.txt rwx for all?
are they in the same folder?
have you try cd into another directory then ./path/model to make it run?is output.txt rwx for all?
are they in the same folder?
Yes to both
have you try cd into another directory then ./path/model to make it run?
AHA! In the terminal I just went up one directory and tried to execute the command and got the same error:
error opening model file
error reading from model file
Hmm weird. The directory is rwx for everyone:
drwxrwxrwx
Okay - thanks illogique thats given me something to work from.it seem that your script try to open file from the current directory, not from the script directory...I am still having the problem.
I think the issue is that when I run processes from within PHP (and hence Apache) the server uses user 'www'. User 'www' does not have permission to delete the output.txt file, so the model cannot run.
At least that is my best guess so far....Okay. Easy. I have it working now. I just chowned the output.txt to user www and now it updates fine.
Thanks for all the help illogique!!
@invasion@