How to run PHP CLI command form wget? - php

I want to run a php cli command from wget but it doesn't work:
run.sh file:
wget -t 1 --timeout=40 -e "php file.php"
error:
wget: Invalid command --execute "php file.php"
The files "file.php" and "run.sh" are in the same directory.

Related

Why can't I run the composer script from PHP using exec, shell_exec, system?

I can run about any binary but not that composer.phar using PHP's exec or shell_exec.
In the same folder I have composer and another php executable with same permissions:
ls -lh
total 1,8M
-rwxr-xr-x 1 me me 1,8M août 22 20:48 composer.phar
-rwxr-xr-x 1 me me 39 août 22 21:05 test.php
Test.php contains:
#!/usr/bin/env php
<?php
print 'hello';
I then have this script:
<?php
print $cmd = "composer.phar --version 2>&1" ;
print "<br>";
$return = exec( $cmd );
var_dump($return);
print "<br><br>";
print $cmd = "test.php 2>&1";
print "<br>";
$return = shell_exec( $cmd );
var_dump($return);
Here is what I get:
composer.phar --version 2>&1
[...]Process.php:81:string 'sh: 1: : Permission denied' (length=26)
test.php 2>&1
[...]Process.php:88:string 'hello' (length=5)
Why do I get that string 'sh: 1: : Permission denied' error? I tried executing in PHP with /usr/bin/env php composer.php /usr/bin/php composer.php, I get the same error.
I solved it by disabling the xdebug extension.
From the doc:
To improve performance when the xdebug extension is enabled, Composer
automatically restarts PHP without it.
So I guess that this "PHP restart" is an issue when calling the binary/phar from PHP.
It is be possible to use the environment variable COMPOSER_ALLOW_XDEBUG to get it working with xdebug, also disbaling a few xdebug options that could alter performances:
<?php
$result = shell_exec('COMPOSER_ALLOW_XDEBUG=1 /usr/bin/env php -d xdebug.remote_enable=0 -d xdebug.profiler_enable=0 -d xdebug.default_enable=0 composer.phar --version 2>&1');
I think it will work with system('php /usr/local/bin/composer install -d /...'), because if you just run composer directly in a shell it will work by reading the shebang at the beginning of the file that says it should be executed by php, but using system() you don't have a shell so you need to specify this yourself.

Should how I write PHP and PHPUnit command?

As my phpunit file test/a.php needs to use the PHP extension A.so, I write the following command:
php -d extension=A.so -d max_execution_time=100 /usr/local/bin/phpunit -v --debug test/
When I run the command and it will output the following info:
/usr/bin/env php -d allow_url_fopen=On -d detect_unicode=Off /usr/local/Cellar/phpunit/5.4.6/libexec/phpunit-5.4.6.phar "$#"
Why does it not run the phpunit code?
If I add extension=A.so to php.ini file and run phpunit file, it will be OK.
So, I want to combine the php command and the phpunit command.

exec () php, sh script runs partially

sorry for bad english..
i have php file like this:
<?php
exec(`sh /tmp/script.sh`);
echo "Work!";
?>
and this is the script:
#!/bin/bash
url="http://someweb.com/get.php?user=user&pass=pass";
wget -O /tmp/file.txt $url
sed -i 's/#Test_file/Ok_Test_file/' /tmp/file.txt
cp /tmp/file.txt /var/www/_client/personale/file.txt
Now when load file.php to the browser, the script works ,but only commands
wget and sed are performed , except cp which doesn't work..does not copy the file!
If i run the script to terminal manually (Debian 8) all cmd are executed...
Where is the problem?
Thanks.
Joele
PHP likely does not have permission to execute the command. Try using sudo to execute the command.

run php output as shell script

I want to use shell and php together.
First I tried:
shell_exec vs functions is disabled and must be so.
But php does not give me permission to run shell_exec()
So, I gave up and tried to make a .sh, call php,store output of php as sh file and run sh file.
Here is sh code
#!/bin/bash/
php test.php -> running php file/ test.php saves commands in script.sh
sh script.sh -> running the commands
rm script.sh -> removing the commands
But there must be a better way from this file process.
Can I run output of test.php directly in .sh file?
Can I run shell_exec ?
Note: I have root access of the server.
You can pipe (|) the output of PHP to sh to be executed:
$ php -r 'echo "echo \$PATH";' | sh
outputs:
/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/Applications/MAMP/Library/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
I've read some statements using -r for simplicity, but you can read from a file by passing a script to php:
$ php test.php | sh

How to execute remote SSH command in background using PHP exec()?

Is there a simple way to execute SSH commands in the background on remote machines from PHP without using ssh2_*? The PHP script is executed by the user from bash (no Apache involved), so it's not an issue of rights. I've tried doing this:
exec("ssh -f -o UnknownHostsFile=/dev/null -o StrictHostKeyChecking=no -i {$keyFile} {$user}#{$ip} {$remoteCommand} 2>&1 >/dev/null </dev/null");
For example:
exec("ssh -f -o UnknownHostsFile=/dev/null -o StrictHostKeyChecking=no -i /home/data/id_rsa user#192.168.0.19 '/home/user/script.sh ; exit' 2>&1 >/dev/null </dev/null");
All PHP variables have been escaped with escapeshellarg() and $remoteCommand is a bash script on the remote machine that sleeps for a few minutes and then starts executing some commands.
My problem is that if I execute that SSH command from bash, it gives control back to bash immediately. If I execute it from php using exec() it waits until the remote command executes. I've tried adding 2>&1 >/dev/null </dev/null after /home/user/script.sh, but the execution still doesn't return control to the PHP script.
I think you are missing an & at the end of your command for sending the execution to the background.

Categories