Cannot run flyway commands in php - php

I am trying to run some flyway commands inside a function from php.
it's a simple clean function that goes something like this:
function clean_schema() {
chdir('my/current/path');
try{
exec('flyway clean', $output, $result);}
catch(Exception $e){
var_dump($e);
}
}
Now as output and result I get an empty array and the error 127. I've looked around and found that that means that I do not have flyway set as a system environment, but if I run the flyway clean command directly in my cmd shell program, it works just fine.

Error code 127 means the executable was not found in the given context. Your shell differs from one executed from the web context, probably because your PATH environment variable is different.
Have you tried executing the app with an absolute path instead? Learn where the executable is first:
which flyway
then:
exec('/absolute/path/flyway clean', $output, $result);

Related

Keep bash shell functions between shell_exec() requests

I have a PHP script that is run via CLI. In turn I want that script to call a bash script, but preferably I would like to break up the BASH requests so I can see the action as it is happening.
I can seem to set Environmental variables and they seem to exist between shell_exec() functions. But even when I have a source file like:
source ./bashes/functions.sh
And in the source file I use "export -f function-name" to export the functions in the script before executing the next line, the next line does not see the functions.
$file = file('./bashes/bash_testing.sh',FILE_SKIP_EMPTY_LINES);
//we want to watch this in realtime so write each line seperately to shell.
foreach($file as $command) {
$output->writeln(shell_exec($command));
}
The function $output->writeln is a helper function just to echo the returned result. But basically the error I get is
sh: now: command not found
now is defined as a function in the included bash_testing.sh shell script.
Anyone know how I can resolve this issue?
Here is the source to the ./bashes/functions.sh file:
function now {
date -u +%T
}
export -fx now
There is a way to maintain a single bash shell, execute commands and handle the return. I recently published a project that allows PHP to obtain and interact with a real Bash shell. Get it here: https://github.com/merlinthemagic/MTS
I would suggest not triggering a bash script but rather trigger the induvidual commands. That way you can handle the return and not have to build exception handling in bash.
After downloading you would simply use the following code:
//get a real bash shell.
$shell = \MTS\Factories::getDevices()->getLocalHost()->getShell('bash', false);
$return1 = $shell->exeCmd($command1);
//logic to handle the return
$return2 = $shell->exeCmd($command2);
//logic to handle the return
..... etc

PHP System Call Always Fails

(Seemingly) no matter what command I try to run with PHP's system function, the command always does nothing and then fails with exit code 1. For example,
system("echo 'this should definitely work'", $retval);
sets $retval = 1. So does
system("dir", $retval);
as well as running an executable that I've written; when I run
vfmt -h cat.v
from cmd.exe the command works and returns with exit code 0, but running
system("vfmt -h cat.v", $retval);
again sets $retval = 1. This vfmt.exe file is in the same directory as the src.php script that is attempting these system calls.
I am nearly at my wit's end trying to figure out what's wrong. What could possibly be causing this issue?
You should check your php.ini for line like the next:
disable_functions =exec,passthru,shell_exec,system,proc_open,popen,curl_exec,curl_multi_exec,parse_ini_file,show_source
^^^^ ^^^^^^^^^^^^^^^^^^
and such.
Also check your "safe mode" status, (php ver. < 5.4) if you have enabled it you can only execute files within the safe_mode_exec_dir and so on...
More information in the doc and for the execution of commands here and especially for system here.
echo is invariably a shell internal command, not a separate executable. You need something more like
system('/bin/sh -c "echo \'foo\'"'); // unix-ish
system('cmd.exe /c echo foo'); // windoze
Most likely issue is that the "working directory" is not what you expect.
This is hidden as you're checking return value and not screen output; they are different.
Firstly, to monitor: try using exec http://au2.php.net/manual/en/function.exec.php as this will help you debug by giving both screen output and return value.
Secondly, to fix:
1 - Specify full paths to both the executable and included files. Assuming this is Windows (you refer to cmd.exe) then run
system("c:\PathToPhpFile\vfmt -h c:\PathToCatVFile\cat.v, $retval);
or
2 - Change the working directory before calling system() or exec() by using chdir() first.
(Or you can do the cd in a batch file (windows) or concatenate the commands in Linux; but this is less portable.)
You need to redirect the output of the command you're running with system() in order for it to run in the background. Otherwise, PHP will wait for program execution to end before continuing.
system('mycommand.exe > output.log', $retval);
system('mycommand.exe 2>&1', $retval);
Not sure how you're invoking your script, but perhaps you're running into a PHP max_execution_time (or related) limit. Something to investigate. Also, since you're running on windows, you may need to use absolute paths with your commands when executing system calls as the file you're invoking may not be in any defined PATH.

Exec() command in Linux does not perform any action from a PHP page

I'm trying to run the following command from a PHP file in a web browser:
exec('festival --tts /var/www/test.txt &');
Unfortunately, nothing happens. I thought of trying 'echo' but there is nothing to return to see if the command is working. I set permissions for test.txt to 777. I also ran the command in a shell and it works just fine - just not when submitted by a PHP script. What am I doing wrong?
Provide the full path to the festival binary, you can find it out on the console with which festival command, then use it in your exec call, like this:
exec('/usr/bin/festival --tts /var/www/test.txt &');
Update:
You need to make sure the folder where you are creating the file has write permission for the user running php which usually is www-data on debian based distros.
If I understand correctly what you're trying to do, you aren't returning the input. The syntax for this is:
exec('command', $output);
where $output is the variable in which the script's output will be stored. Oddly, this output will be returned as an array, so don't forget to implode() it when you're done if you're expecting a string.
Does the festival program print any output? If so, try capturing that. Also check the return value.
exec('/usr/bin/festival --tts /var/www/test.txt &', $output, $return);
Dump the output array, like so:
var_dump($output);
Any non-zero return value usually indicates an error:
echo $return;

Problem with passthru on server

I have a problem trying to run passthru function in my php code (Joomla module). the code is following (this is only a snippet)
ob_start();
passthru("/usr/bin/whois 85.70.231.130 | /usr/bin/grep 'address:'",$code);
$whoisData = ob_get_contents();
ob_end_clean();
$whoisData = str_replace("address:", "", $whoisData);
$whoisArray = split("\n",$whoisData);
echo trim($whoisArray[1]);
when I run this on my localhost, it echoes what it should, but when I run this code on the production server, it echoes nothing and the $code variable contains 127 (command not found). I tryied add absolute paths to these commands into the passthru function, but it didn't helped. Interesting is, that when I run the code right from terminal via ssh and php command, it runs well, but when it's called from application context it doesn't. Does anybody know what I should to do?thanks
SOME EDITS..
safe_mode is on
webserver does not see into /usr/bin and /bin/ folders so what is the best way how to run these commands from php?
usr/bin/grep doesn't look like a valid path to a command.
The missing / at the beginning of the path to the second command might explain the command not found error... even if the first whois command is found.
Have you looked to see if your webserver / php is running chrooted?
print_r(glob('/*'));
if (file_exists('/usr/bin/grep') && file_exists('/usr/bin/whois')) {
print "maybe its a permissions thing?\n";
} else {
print "can't see executables required\n";
}
should give you a clue.
So I have already solved my problem with phpwhois library. I seems like with my server configuration is it unlikely that these functions will be working well. So thanks for your help:)

Executing cmd commands in Windows from PHP Issue

Is it possible to execute cmd commands in Windows OS with PHP exec() function?
I tried this:
<?php
try {
echo exec(
'O:\test\pdftk.exe O:\test\outputs\OPP\out.pdf O:\test\outputs\OPP\out2.pdf cat output O:\test\123.pdf'
);
} catch (Exception $e) {
echo $e->getMessage();
}
Basically, I'm trying to merge two pdf files with the pdftk program. If I just write the same exact command to the cmd by hand, it works and the O:\test\123.pdf file is created. But when I execute the above PHP file, nothing happens (blank page, the file is not created).
Can your PHP user access cmd.exe? You might find the tools at Microsoft's Sysinternals very useful; particularly the process monitor.
Try escaping the directory separator:
exec("O:\\test\\pdftk.exe O:\\test\\outputs\\OPP\\out.pdf O:\\test\\outputs\\OPP\\out2.pdf cat output O:\\test\\123.pdf");
Or even better, use single quotes instead:
exec('O:\test\pdftk.exe O:\test\outputs\OPP\out.pdf O:\test\outputs\OPP\out2.pdf cat output O:\test\123.pdf');
Here is a project that allows PHP to obtain and interact dynamically with a real cmd terminal. Get it here: https://github.com/merlinthemagic/MTS
After downloading you would simply use the following code:
//if you prefer Powershell, replace 'cmd' with 'powershell'
$shellObj = \MTS\Factories::getDevices()->getLocalHost()->getShell('cmd');
$strCmd1 = 'O:\\test\\pdftk.exe O:\\test\\outputs\\OPP\\out.pdf O:\\test\\outputs\\OPP\\out2.pdf cat output O:\\test\\123.pdf';
$return1 = $shellObj->exeCmd($strCmd1);
The return will give you the command return OR error from cmd, just as if you sat at the console. Furthermore, you can issue any command you like against the $shellObj, the environment is maintained throughout the life of the PHP script. So instead of bundling commands in a script file, just issue them one by one using the exeCmd() method, that way you can also handle the return and any exceptions.
try Executing using the Admin Privileges for command prompt

Categories