2

I'm on OS X and I'm trying to write an interface for for an interpreter, the communication with the interpreter looks like this in PHP:

$f = fopen('mmascript.m', 'w');
fwrite($f, "#!/Applications/mma/Contents/MacOS/MathematicaScript -script\n");
fwrite($f, 'Print[100]');
fclose($f);
chmod('mmascript.m', 0777);
passthru('mmascript.m', $error);
echo $error;

It displays error 127, "command not found". If I cd into that folder and try to run the file I get the error message

bad interpreter: Operation not permitted

If I copy it to another folder, f.e. cp ./mmascript.m ~/Desktop/mmascript.m, it might give the same issue initially but will then magically start working. Since the path to the interpreter is absolute that should mean that the path is correct. Any ideas?

8
  • "If I cd into that folder" the folder containing the php script or the folder containing the MathematicaScript file?
    – 111---
    Commented Apr 23, 2015 at 21:01
  • @datUser Both the PHP script and mmascript.m are in the same folder.
    – user1995
    Commented Apr 23, 2015 at 21:06
  • can you try wit full path to 'mmascript.m' or with './mmascript.m' ?
    – taliezin
    Commented Apr 23, 2015 at 21:08
  • @taliezin From the command line that gives the error I've been talking about; in the PHP code it now gives a more understandable error, 126, "Command invoked cannot execute" which indicates "Permission problem or command is not an executable."
    – user1995
    Commented Apr 23, 2015 at 21:11
  • I ment to change in your script where is 'mmascript.m' with '/path/mmascript.m' and you run php your_script.php ?
    – taliezin
    Commented Apr 23, 2015 at 21:14

1 Answer 1

0

Lets add some Debugging Info, like so:

$f = fopen('mmascript.m', 'w');
echo "fopen complete."
fwrite($f, "#!/Applications/mma/Contents/MacOS/MathematicaScript -script\n");
echo "fwrite 1 complete."
fwrite($f, 'Print[100]');
echo "fwrite 2 complete.
fclose($f);
echo "close complete."
chmod('mmascript.m', 0777);
echo "Permissions Successfully Changed."
passthru('mmascript.m', $error);
echo "Passthru Complete."
echo $error;

My gut feeling is that passthru won't echo, because the passthru needs a command followed by your filename.

14
  • It returns "fopen complete.fwrite 1 complete.fwrite 2 complete.close complete.Permissions Successfully Changed.Passthru Complete.126" Also note that 126 is what passthru has assigned to $error, and passthru is not supposed to echo anything when it has thrown an error.
    – user1995
    Commented Apr 23, 2015 at 21:40
  • See Error Gode 126. As I suspected Passthru requires an executable followed by your filename. See PHP Passthru man page
    – eyoung100
    Commented Apr 23, 2015 at 21:45
  • I have seen that, could you explain it to me? The parameter list reads: "command. The command that will be executed." ./mmascrip.m is a command that works in the terminal sometimes, but often not as I described in my question. It shouldn't have anything to do with passthru since I'm getting the same thing if I execute the file in the terminal, no PHP involved.
    – user1995
    Commented Apr 23, 2015 at 21:50
  • If you want MathMatica to execute your script Passthru must look like passthru('/path/to/mathmatica/mathmatica.exe mmascript.m', $error); The way your script is written passthru is treating your script as the executable, which it isn't. Since it isnt executable the operation is not permitted.
    – eyoung100
    Commented Apr 23, 2015 at 21:54
  • I thought the shebang in the beginning of the file means that it is executable. It works your way if I call it from the terminal by php -f test.php - thank you! - however if I run it as a website on a local server then I get the error 1, which is a catch-all for all kinds of errors. So now I'm facing the problem of why it is working from the terminal but not in the browser. Could it be because the server is a different user, I'm told typically the server is 'www'?
    – user1995
    Commented Apr 23, 2015 at 22:16

You must log in to answer this question.