PHP shell_exec command doesn't work - php

I am trying learn to execute shell scripts from within PHP code. So, I made a test program to execute a bash script from within PHP. However, it has no effect. The relevant code is shown below.
<?php
.......
shell_exec('/bin/bash /var/www/html/just_touch.sh');
?>
The just_touch.sh script just creates a new file, like as shown below.
touch /home/user/some.txt
I was expecting to have file /home/user/some.txt after execution, but no, it isn't made. What mistake, am I doing?
P.S: The following code works though.
$output = shell_exec('ls /home/user');
echo $output;
Does this have anything to do with permissions?
Moreover, I notice that while this prints "Can you see me?".
$output = shell_exec('echo Can you see me?');
echo $output;
This doesn't!
shell_exec('echo Can you see me?')
What is going on here?

Stderr is lost when using shell_exec. You might wan't to use:
shell_exec('/bin/bash /var/www/html/just_touch.sh 2>&1');

Related

Show errors or console file lua in editor php

I use PHP for edit code LUA, change and save file. I can execute the file, but I can not use show if I have error, as console.
example:
my code lua:
print "hello"
and code php:
<?php
$output = passthru("sudo /usr/local/bin/lua /var/www/test.lua");
//show output
echo "<pre>$output</pre>";
?>
this work, but I can show errors (console), if code has.
i think use the code io.stdin or io.stdout in lua
thanks
See this comment: http://php.net/manual/en/function.passthru.php#101148
When Lua fails and responds with an error, passthrough can't capture the result, even though I get the STDERR text without any problems, you may, as suggested in the comment, try piping the Lua result to tee program.
$output = passthru("sudo /usr/local/bin/lua /var/www/test.lua | tee");

Running script and executing PHP for output

I'm looking to run a program, and for every output line it generates, execute a PHP script and pass the line content to it.
I know, pretty hard to understand. Here's an example:
Execute script -> script outputs 'Initializing script on 127.0.0.1'. Now it needs to execute a command like php5 input.php 'Initializing script on 127.0.0.1'.
Is this doable? If so, how would I go about doing this?
Edit: to clarify; I basically want command > log.txt but in stead of writing the output to that file, writing it to a PHP script as an argument
PHP is an interpreter much like Bash, Python, etc, so you can do "normal" scripting with it. For example:
#!/usr/bin/php5
<?php
echo "Hello, world!\n";
while($line = fgets(STDIN)) {
echo "> " . $line;
}
?>
Mark the file as executable, then run:
$ /program/that/generates/lines | /path/to/your/php/script
However, contrary to your original question, it sounds to me like you actually want to use JavaScript and possibly AJAX for web purposes. Sane web applications will have the said script run in the background and safely write the results to a file or stream, using AJAX to read it and update the information on the current page.

Printing / Echoing To Console from PHP Script

I'm debugging a PHP script that runs a couple SQL queries and emails a set of users. I'm sure this is a very basic thing, but every time I try to echo, print, or print_r it doesn't appear while running the script.
So say I have this in the script:
print("This should print");
echo "on the command line";
When I run the script via command line php script.php it doesn't actually print anything to the command line while running the script.
Is there a way of having PHP print to console? I feel like I'm missing something extremely basic here.
Thats the method of doing it.
Things to check for are output buffering
http://php.net/manual/en/function.ob-flush.php
Is that code actually being run ? Make sure it doesnt branch before it gets there
A good approach could be:
function log($message)
{
$message = date("H:i:s") . " - $message - ".PHP_EOL;
print($message);
flush();
ob_flush();
}
It will output to your terminal each line you call. Just use :
log("Something");
log("Another line");
The following code working fine for me.
<?php
print("This should print");
echo "on the command line";
?>
with tags.
I know it is old, but I am going post my own solution to this just in case if someone would run into similar situation. I had a problem that a legacy command line PHP script wouldn't print or echo anything to the terminal when DSN is incorrectly configured and the script got stuck for very long time (not sure how long, never waited for it to terminate by itself). After putting ob_end_flush() in the entry line of the script, the output came back to the terminal. So it turned out that all output was buffered and since the script stuck at some point, the output stayed buffered and so never went to the terminal.

Calling a PHP script from within PHP

I have a PHP script that I'd like to run both via the browser and from the command line. When I run the script from the command line, it executes without a problem. However, when I call it via a function such as exec or passthru, it doesn't work. I'm not getting any output from the call and I see no errors in the logs. I'm very confused...
echo exec('php /usr/share/nginx/www/function.php arg1');
Any ideas?
The output is captured in a parameter variable as an array and not returned back, when you use exec(). So do something like this:
exec('php /usr/share/nginx/www/function.php arg1', $output);
print_r($output);
#David,
A few things to check.
Create a PHP Info page and see if exec or passthru is enabled. Hosts disable it for security.
Try:
<?php
$exe = exec('php /usr/share/nginx/www/function.php arg1');
var_dump($exe);
?>
You could even do:
<?php
if(function_exists('exec')) {
echo "exec is enabled";
}
?>

error executing shell script in PHP

I am trying to execute a shell command via:
<php echo shell_execute("tac /home/kusmuk/access-logs/kusmuk.org"); ?>
But it does not give any output. What could be the reason?
Although it does not work, the following lines work as expected:
<php echo shell_execute("ls -al triogrup.com"); ?>
//outputs: -rw-r----- 2 root kusmuk 28640 Aug 19 17:44 kusmuk.org
<php echo shell_execute("pwd"); ?>
//outputs: /home/kusmuk/public_html
Greg's tip is good. You'll probably end up with some kind of permissions problem.
However, I would say it's a good idea to avoid launching system calls from PHP if possible. The debugging can be a pain and if you're passing parameters it is very easy to make security holes. Native PHP code is much easier to handle.
‘tac’ is simple enough that you should be able to do it fine from within PHP. For example a trival version that spits out the whole file in one go:
$log= file_get_contents('/home/kusmuk/access-logs/kusmuk.org');
echo implode("\n", array_reverse(explode("\n", $log)));
Try this:
echo shell_exec("tac /home/kusmuk/access-logs/kusmuk.org 2>&1");
It will redirect stderr to stdout so hopefully you should see why it's not working

Categories