I am having trouble getting my PHP script to run a bash command/script. Here is an export of my PHP script (it is triggered when a HTML form is submitted):
}
chdir('/usr/local/sim/');
shell_exec('killall -9 sim');
shell_exec(./write-sims.sh');
chdir('/var/www/html/');
shell_exec('php -f test.php');
?>
I know that this PHP script is running okay as the 'killall -9 sim' command is executed as expected along with the 'write-sims.sh' script.
I put my bash command inside a PHP script as a test to see if that would work but it doesn't, 'test.php' is as follows:
<?php
shell_exec("find /usr/local/sim/data -maxdepth 1 -type f -name '*.conf' -exec sh -c '/usr/local/sim/build/src/sim --daemon --config {} &' \;");
?>
When I run test.php manually via cli it runs fine however whenever it is called by the first php script it doesn't run.
I have tried switching things around by putting the bash command in a .sh script and calling that from the first php script and also adding the bash command itself inside the first php script but neither work however I don't get any errors in the nginx error log which seems to suggest the script is running okay without errors.
Any idea why this is not running but runs fine when I do it manually? Thanks!
Related
I am trying to run a php file from shell script file or terminal in open-wrt platform. I have executed php files in crontab and those are running perfectly. i need to run a php file without putting it into crontab.I am trying it with the following command
chmod 777 /www/api/*
cd /www/api
php myphp.php
but it showing -ash: php: not found
I have also try it putting the following command on top of the script
#!/usr/bin/php
but it is not working. i could not figure out the problem!!!
You must install php-cli package, after you can run the script by
php-cli script.php
I want to execute bash script with php.
When i launch php script in browser i got an error " permission denied"
when i run php whoami , i get www-data. I granted all permisions for write and read.
When i run bash script as www-data it works. But via php im still getting the same error.Should i check php.ini settings or somethnig?
If i write unknown bash command in php script, using shell_exec(). I got an error sh: command not found.I think it should be "bash: comand not found".
Can u please help me out.
PHP
<?php
$message=shell_exec ("bash bashscript 2>&1");
print_r($message);
?>
bash
#!bin/bash
cp /myfiles/file aa
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
I have installed PHP CLI to execute php commands from console.
I have installed PHP CLI using this command -
sudo apt-get install php5-cli
When I run this
$vr=3; echo $vr;
Result :-
=3: command not found
If I run echo "test";
Result :- test
displays..
Can anyone tell why "command not found" displays..
The "echo "test" line is working because echo is a bash command.
You have to write your own php script, the run it by command line like this:
$ php myscript.php
In alternative, you can run php from your command line, then directly write or paste your script.
Then press CTRL+D to run it. Remember the at the beginning and at the end.
As third option, you can write a php script, putting in the first line this code:
#!/usr/bin/php
Obviously the php executable path must match the one in your system.
This way, you can chmod +x the script, then run it directly like this:
$ ./myscript.php
The fourth option is the interactive shell:
$ php -a
Interactive shell
php > echo 5+8;
13
[$ in front of commands means a command run by user]
You are entering PHP code into the Unix shell (e.g. bash). The Unix shell does not understand PHP code, so you have to run php first.
To run your PHP code from the command line:
$ php -r '$vr=3; echo $vr, "\n";'
3
To run your PHP code from the PHP interactive shell (which may or may not be compiled into PHP):
$ php -a
Interactive shell
php > $vr=3; echo $vr, "\n";
3
php >
(Hit Ctrl+D or type exit to get out of the PHP shell.)
To run your PHP code from a file named prog.php (which contains <?php before the code):
$ php prog.php
3
It seems you want something like this: http://www.php.net/manual/en/features.commandline.interactive.php
This gives you an interactive mode, where you can type PHP code and have it executed directly.
I have a bash script:
run.sh
#!/bin/sh
cd /var/www/project/bin/
CMD="./executable <full_path_to_file>;
$CMD
When I run this program from the terminal. (i.e. ./run.sh, it works fine)
However when I call it from PHP:
system("full_path_to_sh_file", $out);
It calls the script successfully, and even runs the executable, but now the executable throws an error saying that the supplied file can't be found.
Any ideas?
How do you run PHP script, from webserver or command line?
If from webserver, what user is it run (httpd or apache?)
Make sure the environment as same as when you are running from terminal (for example: same user)
Try this if running as different user:
sudo -u apache /fullpath/run.sh