what does exec() return here? - php

Following is a query string to import database:
$query = "mysql --user" . $this->userName . " --password " . $this->DatabaseName . " < " . $completePathOfSQLFile;
$resultQuery = exec($query);
The command executes fine. But what does exec function return? I need to make sure that the operation was performed correctly and return true | false accordingly.

From the manual
Return Values
The last line from the result of the command.
But note that you can also pass a third argument to exec which will be the return status of the command. Non-zero values are usually error states, the mysql client may use that sensibly. The manual has notes on various other approaches too, it's worth reading.

As Quentin said 3rd parameter of exec is return_var
string exec ( string $command [, array &$output [, int &$return_var ]] )
mysql will return non-zero exit code in case of an error.
# mysql -u root -e "SELECT 1" > /dev/null 2>&1
# echo $?
0 <- returns 0 for success
# mysql -u root -e "SOMETHING WRONG" > /dev/null 2>&1
# echo $?
1 <- Wrong statement
Please note that if the file is empty , it will still return "0", since there is no SQL syntax error or wrong arguments given to mysql client.
So You can add this to your code :
$query = "mysql --user" . $this->userName . " --password " . $this >DatabaseName . " < " . $completePathOfSQLFile;
$resultQuery = exec($query,$output,$return_var);
if (intval($return_var) !== 0) {
echo "error: ".$output;
}

Related

Passing URL parameters from php script to bash

I'm trying to pass parameters from an URL (PHP script) to a Bash using a case control statement. The Bash script itself is working correctly, if run from the Linux machine, but calling via the URL returns and empty white page.
The bash script
case $1 in
allBG)
echo "Prilagane na status 1 - All BG";
cp /root/copyjob/status/xml/status1_allBG/tvip_provision.xml >/var/www/html/prov.mac/$2/tvip_provision.xml;
;;
allEU)
echo "Prilagane na status 2 - All EU za"
cp /root/copyjob/status/xml/status2_allEU/tvip_provision.xml >/var/www/html/prov.mac/$2/tvip_provision.xml
;;
eurotv)
echo "Prilagane na status 3 - eurotv";
cp /root/copyjob/status/xml/status3_eurotv/tvip_provison.xml >/var/www/html/prov.mac/$2/tvip_provision.xml;
;;
gotv)
echo "Prilagane na status 4 - gotv";
cp /root/copyjob/status/xml/status4_gotv/tvip_provision.xml >/var/www/html/prov.mac/$2/tvip_provision.xml;
;;
bixi)
echo "Prilagane na status 5 - bixi";
cp /root/copyjob/status/xml/status5_allbg/tvip_provision.xml >/var/www/html/prov.mac/$2/tvip_provision.xml;
esac
The php script
$status = $_GET['status'];
$mac = $_GET['mac'];
echo exec('/root/copyjob/status/status.sh $status $mac');
You need to use double quotes, if you want the PHP to be interpreted or end your string and concat the variables to your string.
So its either:
echo exec("/root/copyjob/status/status.sh $status $mac");
or:
echo exec('/root/copyjob/status/status.sh ' . $status . ' ' . $mac);
Edit: Sorry, missed the echo. If you wanna output something from it you got to use the 2nd and 3rd parameter of exec:
<?php
$status = $_GET['status'];
$mac = $_GET['mac'];
$output = array();
$return_var = null;
exec("/root/copyjob/status/status.sh $status $mac", $output, $return_var);
var_dump($output);
var_dump($return_var);

What does exit code -1 mean in php's exec method

I am executing the following php code, and the status returned is -1, while output array is empty. What does this mean?
$cmd = "sort -T /var/www/html/my_app/protected/runtime/sort// -t '|' /var/www/html/my_app/protected/runtime/workflow/db-export/filename_1818.txt -o /var/www/html/my_app/protected/runtime/workflow/db-export/filename_1818.txt 2>&1";
exec($cmd, $output, $status);
print_r($output);
echo $status;

php - shell_exec , exec, etc.. can not get output like in terminal. even with 2>&1

First let's run the command via terminal:
$ echo 1; /etc/init.d/apache3 restart; echo 2;
result.. ( apache3 on purpose to see an error )
1
bash: /etc/init.d/apache3: No such file or directory
2
awsome.
now let's run this via php..
<?php
$command = "echo 1; /etc/init.d/apache3 restart; echo 2; 2>&1";
$response = shell_exec("$command");
echo $response;
?>
all I see on the browser is: 1 2
I tried all sorts of things. replaced the semi colons with "&&"..
tried all the php stuff such as..
passthru()
exec()
system()
popen()
i tried it all pretty much. been hours.. can not get it to show me the same stuff i see via terminal.
You have to use 2>&1 after the restart command
Your command :
$command = "echo 1; /etc/init.d/apache3 restart; echo 2; 2>&1";
yours has "2>&1" at the end which has no use.
Also you will add to 2>&1 after each command in case others uses STDERR
$command = "echo 1; /etc/init.d/apache3 restart 2>&1; echo 2 ";
Consider using exec. The basis function only returns the first line of the output:
$response = exec("$command"); // just the first line
But use the additional parameters to capture both the output (as an array) and the return value
$retval = NULL;
$output = NULL;
$response = shell_exec("$command", $output, $retval); // last two params are passed by reference and modified by the command
Also, as user993553 posted, the output captured by these cli functions in PHP will usually just return stdout and not stderr. You can append " 2>&1" to any given command (note the space before the 2) in order to route stderr into the output.
That said, your function becomes:
$command = "echo 1; /etc/init.d/apache3 restart 2>&1; echo 2;";
$retval = NULL;
$output = NULL;
$response = exec($command, $output, $retval);
var_dump($output);
and the output:
array(3) {
[0] =>
string(1) "1"
[1] =>
string(37) "sh: 1: /etc/init.d/apache3: not found"
[2] =>
string(1) "2"
}
EDIT: you can also check $retval for an error condition. If it's not empty or zero, then signifies an error.
From the manual of shell_exec:
ALSO, note that shell_exec() does not grab STDERR, so use "2>&1" to
redirect it to STDOUT and catch it.

linux printing via lp, how I can print more than one copy?

I need to print a (custom) number of copies on a DYMO-450 labelprinter, using a apache based local linux server, running php, wkhtmltopdf, xvfb and stuff...
If have trouble with giving a number of copies to the print job. Normally it shout be done with
lp -d PRINTER DOCUMENT -n2 // or possibly -n 2
for 2 copies.
But the DYMO doesn't.
Currently I am am using this workaround, which does the job, but I am waiting up to 3 seconds between each printed label:
$printcmd = '';
for ( $p=0; $p < $_REQ['copies'] ; $p++ ) {
if ($p>0) $printcmd .= '&& ';
$printcmd .= 'lp -d ' . $cfg['labelprinter'] . ' ' . $pdf_file . ' > print.log 2>&1';
}
system($printcmd . ' &');
But this doesn't suck at all.
Any suggestions ?
The (late) answer is, adding -o Collate=True
lp -n num-copies -o Collate=True filename
thanks to http://www.cups.org/documentation.php/options.html

Execute background system command and capture output in a array using PHP

I'm trying to ping a bunch of IPs using PHP/HTML.
<?php
$ip=array("192.10.1.1","192.10.1.2","192.10.1.3","192.10.1.4","192.10.1.5")
$list=count($ip);
$instruction = "";
for ($x=0; $x<$list; $x++)
{
if ($x > 0)
{
$send2_bg =" & ";
}
else
{
$send2_bg = "";
}
$instruction = $instruction.$send2_bg."ping -c 2 -w 1 ". $ip[$x]." | grep -i received | awk '{print $4}'" ;
}
echo $instruction;
$result =exec($instruction);
?>
Expected output array
1 1 0 0 2
But I'm failing to get the output, The instruction is constructed perfectly but after exec(), the output is not as I expect.
exec() just returns the last line of the output
shell_exec() returns all output
Try capturing the output like this:
exec($command, $host, $output);
print_r($output);
The problem is that you are echoing the instruction var see that link - PHP manual
Please reffer to the parameter output and echo that one instead the instruction var.

Categories