Running AMPL code from PHP (Laravel Process) - php

I'm trying to run an ampl .run (or any ampl code) file from Laravel using Symfony Process. My code is as below:
$commandArray = array('./ampl');
$process = new Process($commandArray);
$process->setWorkingDirectory('/usr/local/bin/amplitude/amplide.linux64');
$process->run();
if (!$process->isSuccessful())
{
throw new ProcessFailedException($process);
}
dd($process->getOutput());
But I cannot start ampl. I get an error like:
""" The command "'./ampl'" failed.\n \n Exit Code: 2(Misuse of shell
builtins)\n \n Working directory:
/usr/local/bin/amplitude/amplide.linux64\n \n Output:\n
================\n \n \n Error Output:\n
================\n """
I suspected this was a permissions error in the directory but when I use:
$commandArray = array('ls');
it works and outputs the list of files and folders. I understand that ampl is basically a terminal program, so how do I access and write commands to it?
If someone can explain how to access terminal programs from Process, I think it would be very helpful. Thank you in advance.

I figured out the issue here was that just calling the ampl console by typing ./ampl does not trigger any response. As it cannot be interpreted as a actual command, Laravel gives an error. The trick is to pass an actual command to the process object and to always give the full path to any .run/.mod/ .dat file you are reffering. For example:
$commandArray = array('./ampl path/to/example.run;');
or
$command = './ampl path/to/example.run';
works fine and will give the response.
Another important thing I noticed was that, since AMPL is basically another program running in the terminal, we cannot pass different commands seperately to the process using arrays like the following:
$commandArray = array('./ampl model.mod;','./ampl data.dat;', './ampl solve;');
This will not work. Neither will this:
$commandArray = array('./ampl, model.mod; data.dat; solve;');
Ideally it is best to have everything in a .run file and then execute it.
If you need to pass parameters to the .dat file from Laravel, passing this into the commands using string concatnation causes issues, although I do not exactly know why. I would suggest to use the Storage class in Laravel to update the .dat file first and then run the ampl problem using a .run file.

Related

Execute a windows shortcut from php

I have a situation where I need to call a batch file from a php script... however this batch file needs to run as admin in order to work.
My solution was to create a shortcut to the batch file and check the box to run as admin from the shortcut... however I can't get php to call the shortcut.
I have tried:
exec("C:/path/movefiles_admin.lnk")
and
system("cmd /c C:/path/movefiles_admin.lnk");
Neither of which work. Any suggestions?
Try this:
exec("START C:/path/movefiles_admin.lnk");
START Starts a separate Command Prompt window to run a specified program or command.
You can run nonexecutable files through their file association by typing the name of the file as a command
If your PHP has issues executing shortcut to batch file, try executing simple read and write actions to a test.txt file. (To check whether you have PHP running in safe mode).
If it doesnt do these basic actions then you have some configuration issues.
If a program is started with exec function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends.
Please refer this link for your version of PHP: manual

There is a code snippet in my PHP script that fetches certain properties for a userID using an API call

There is a code snippet in my PHP script that fetches certain properties for a userID using an API call.
Some of the userIDs do not have certain fields. Like every user might not have an owner. So, that field does not return anything and I get the following when I run the script in browser:
Notice: Trying to get property of non-object
This is fine for me as the code still executes further.
When I run the same script through a cron, I get a fatal error and the script's execution stops. How can I run the script exactly as it is running in the browser so that I do not get the fatal error?
Solved
This is what works for me:
cd /home/####/public_html/####/; /usr/local/bin/php -c -f file.php
"cd /home/####/public_html/####/;" changes the directory to where my "file.php" is located. Can anyone let me know why the above command works and the following does not:
cd /home/####/public_html/####/; /usr/local/bin/php file.php
Solution 1, if you can change the script:
You can check to ensure that the var is set before you fill it. For example:
$name = "";
if (isset($var['name'])) {
$name = $var['name'];
}
Thats the best example I could give without a code sample... You might also be able to get away with the # error suppression, but Im not sure how well it works with var assignments.
UPDATE:
The # error suppression worked in a quick cli test on my mac:
#$name = $var['name'];
The # sign tells the interpreter to suppress errors on that line and continue. While this works, the first example is cleaner.
Solution 2, if you cannot update the script:
Change the cron to run the script through curl/wget. Put the script in a location where it can be called through the server (even an internal-only vhost), and call it through curl/wget. The other interpreter will be used as opposed to the CLI.

php exec() is not executing the command

I have tried to use exec() with 'whoami' to check if it works and I got the result of
nt authority\system
Now I need to run a .exe file with parameters from php via exec() function.
I tried this in command prompt and it actually runs the program with given parameters. This is the example command.
NOTE the exe file gets 3 inputs (folder, file_name, report_file_nmae)
> ..\..\some_file.exe folder="C:\path_to_folder" param=1.xml report=2.xml
But when I run this command from php file:
exec('..\..\some_file.exe folder="C:\path_to_folder" param=1.xml report=2.xml');
nothing is happening. This is the first time I am using exec() function, so I am not familiar with its details. What is wrong?
I tried using:
\\ instead of \
escapeshellarg() on the directory
added "" around directory folder names
No luck
Addendum:
echo exec($command) // echos < .... why?
or
exec($command, $output);
print_r($output); // Array()
I even changed the permission on the file to full control to all users.
If I call the program from command prompt, I can see the icon appearing next to clock for a second.
But the same call from php will not even call the program.
Edit
Even exec('notepad.exe'); is not working. Something has to be done with php configurations maybe?
I already said that I was new to exec() function. After doing some more digging, I came upon 2>&1 which needs to be added at the end of command in exec().
Thanks #mattosmat for pointing it out in the comments too. I did not try this at once because you said it is a Linux command, I am on Windows.
So, what I have discovered, the command is actually executing in the back-end. That is why I could not see it actually running, which I was expecting to happen.
For all of you, who had similar problem, my advise is to use that command. It will point out all the errors and also tell you info/details about execution.
exec('some_command 2>&1', $output);
print_r($output); // to see the response to your command
Thanks for all the help guys, I appreciate it ;)
You might also try giving the full path to the binary you're trying to run. That solved my problem when trying to use ImageMagick.

Using PHP in command line to read staged files in git

I don't really know where to begin with this as I am not very good at git, and am new to running php scripts via command line.
What I would like to be able to do is make a call in my php script (ran via command line on unix) that will figure out what files are staged to be commit, and return them preferably in an array.
This script will be ran from home ~/
The repos are in ~/repos/
I wouldn't mind passing in the repo name to a function that returns this array.
Look at the format of the output from git status
Read the documentation on exec
write base script
test script
post back on SO for specifics
Profit
Try something like this:
$path = '/path/to/git/working/copy';
$status = explode(PHP_EOL, shell_exec("git status $path") );
print_r( $status );
Cheers

PHP exec() with CUDA

exec("fun.exe input/input.txt ");
I want to run an CUDA program in PHP,
the task is:
load data from an input.txt. (argument)
calculating.
write an output.txt.
and PHP read ouput.txt to do next task.
In server1(Apache ,Windows XP), it can run perfectly,
but in server2,3(Apache, Windows 7),the output is wrong.
The program doesn't crash and there's no any error message in the page,
it seems like something wrong during the execution.
Next I try exec the All CPU-side version (same calculation),server2,3 can run correctly.
If I exec the fun.exe(CUDA version) in server2,3 directly(double click or in command line),the program also run perfectly.
Any idea on why server2,3 can't run the program? Thanks.
First, try using the full path to the executable. Then the full path to the input file too.
If that doesn't work, then try modifying the file permissions (try with full 777 permissions, if that works then you know where your problem lies).
Try to use the entire path (windows version using backslash).

Categories