test.php:
<?php
print_r($argv);
?>
Shell:
$ php -q test.php foo bar
Array
(
[0] => test.php
[1] => foo
[2] => bar
)
Answer from schneck on Stack Overflowtest.php:
<?php
print_r($argv);
?>
Shell:
$ php -q test.php foo bar
Array
(
[0] => test.php
[1] => foo
[2] => bar
)
If you have webserver (not only just php interpreter installed, but LAMP/LNMP/etc) - just try this
wget -O - -q -t 1 "http://mysite.com/file.php?show=show_name" >/dev/null 2>&1
where:
- « -O - » — (Letter "O", not zero!) redirect "downloaded html" to stdout
- « >/dev/null 2>&1 » — redirect stdout & stderr output to nowhere
- « -q » — quiet wget run
- « -t 1 » — just 1 try to connect (not like default 20)
In PHP's "exec" it'll be smth like this:
function exec_local_url($url) {
exec('/usr/bin/wget -O - -q -t 1 "http://'. $_SERVER['HTTP_HOST'] .'/'
. addslashes($url) . '" >/dev/null 2>&1'
);
}
// ...
exec_local_url("file.php?show=show_name");
exec_local_url("myframework/seo-readable/show/show_name");
So, you don't need to change your scripts to handle argc/argv, and may use $_GET as usually do.
If you want jobs runned in background - see for ex. Unix/Windows, Setup background process? from php code
I use approach with wget in my cron jobs; hope it helps.
Videos
You're starting your command with . - that will be interpreted as shell-source command, which is not what you want, obviously. Instead specify full path and do like:
$result = shell_exec('/bin/bash /full/path/to/script.sh param1 param2');
//var_dump($result);
-also, make sure your php user have permission to execute your sh-script and that PHP can use functions like exec() (they could be disabled by configuration)
You need to use quotes to send arguments, try this :
$command_result = shell_exec('script.sh "'.$param1.'" "'.$param2."');
Passing parameters to a shell script from PHP.
Its all about the "strings", and when to "double quote" for expansion.
<?php
/* exec("/csvexport.sh $table"); */
/* double quote here because you want PHP to expand $table */
/* Escape double quotes so they are passed to the shell because you do not wnat the shell to choke on spaces */
$command_with_parameters = "/path/csvexport.sh \"${table}\"";
$output_from_command = "";
$command_success = "";
/* double quote here because you want PHP to expand $command_with_parameters, a string */
exec("${command_with_parameters}", $output_from_command, $command_success);
/* or to keep it simple */
exec("/path/csvexport.sh \"${table}\"");
/* show me what you got */
echo"${command_success}\n${output_from_command}\n";
?>
BTW: I did not test this snippet.
I'm not a PHP guy, but it seems you'll need to do something like
exec(escapeshellcmd("/csvexport.sh \"$query\" $table"));
Does PHP have a function where you can call the command and pass the arguments separately?
some_exec_function("/csvexport.sh", $query, $table); # ???
The problem here is that you are quoting the entiore command you are trying to run as a single variable. As a result, you're not running php with foo.php as an argument but instead are attempting to execute a file called php foo.php. Here's a simpler example to show you what I mean:
$ var1="echo "
$ var2="foo"
$ set -x ## debugging info
var1$var2"
+ 'echo foo' ### The shell tries to execute both vars as a single command
bash: echo foo: command not found
var1" "$var2"
+ 'echo ' foo ### The shell tries to execute 'echo ' (echo with a space)
bash: echo foo: command not found
So, the right way is to remove the space and quote each variable separately:
$ var1="echo"
$ var2="foo"
var1" "$var2"
If you do that though, you'll hit the next error. The . is the source command. That tries to read a shell script and execute it in the current session. You are giving it a php script instead. That won't work, you need to execute it, not source it.
Finally, always avoid using CAPITAL variable names. The shell's reserved variables are capitalized so it's a good idea to always use lower case variable names for your scripts.
Putting all this together (with a few other minor improvements), what you want is something like:
#!/bin/sh
list="/path/to/my/site/dir"
config="/usr/bin/php"
for i in "$list"
do
"$config" "$i"/test.php
done
The problem in your code is the line:
. "${CONFIG}${i}/test.php"
Remove the .
Here is another example:
$ ls -l
-rwxrwxr-x 1 bg bg 67 Oct 20 09:42 index.php
-rwxrwxr-x 1 bg bg 68 Oct 20 09:43 test.sh
index.php
<?php
shell_exec('echo Hello > /tmp/hello.txt');
?>
test.sh
#!/bin/bash
/usr/bin/php index.php
Several possibilities:
- You have safe mode enabled. That way, only
exec()is working, and then only on executables insafe_mode_exec_dir execandshell_execare disabled in php.ini- The path to the executable is wrong. If the script is in the same directory as the php file, try
exec(dirname(__FILE__) . '/myscript.sh');
You might have disabled the exec privileges, most of the LAMP packages have those disabled. Check your php.ini for this line:
disable_functions = exec
And remove the exec, shell_exec entries if there are there.
Good Luck!