I am tryng to execute java command from php, but the output array keeps returning empty. Any help would be appreciated.
Below is the code that i have tried
<?php
exec( "/usr/bin/java -version",$output);
print_r($output);
?>
Also i checked the error log and found the following
sh: /usr/bin/java: Permission denied
and when i try the code
<?php
exec( "java -version",$output);
print_r($output);
?>
i get
sh: java: command not found
what should i do to allow my php file to execute java commands?
Use cron to execute the java program.
1)write argument name to a file from your php program.
$Handle = fopen("myfile", 'w');
fwrite($Handle, "argument");
fclose($Handle);
2) Create a bash script to read that argument name and run java program.
#!/bin/sh
arg=$(head -n 1 myfile)
/usr/bin/java argument 2>&1
>myfile
save it as mycron.sh
chmod +x mycron.sh
3) Configure the script in crontab
crontab -e and paste this
* * * * * mycron.sh
Related
Here is a command I successfully execute in terminal:
PHP process_scan.php
I want to execute that code every time a user visits a file, which exists in the same directory as the file mentioned in the command.
Here is the code in the file:
exec('PHP process_scan.php 2>&1', $output, $retval);
Running the file in the browser, $retval = 127 wit $output = "sh: PHP: command not found".
How can I successfully run the command every time a user visits the file?
Instead of referencing PHP, I got this to work by executing PHP bin file:
exec('/usr/bin/php process_scan.php 2>&1', $output, $retval);
I have a PHP file (/path/to/file.php) containing an exec-command:
$result = exec('lftp -u USER,PASS sftp://USER#IP:PORT -e "cd FOLDER; mput -E FILE; quit;"');
When I run the command "php /path/to/file.php" in the terminal, $result has a value "X bytes transferred"
When I create a cron task for the same user using the exact same command, the $result is always an empty string. The command still works though, the file can be found on the FTP-server.
How can I get the cron version to output something so I can confirm that the transfer was succesfull?
So the solution was to add the following setting to a LFTP config file (for example /etc/lftp.conf):
set cmd:interactive yes
I have a shell script in Linux that performs SFTP to get some files. It works fine when I execute it from a terminal.
I am trying to call the script from PHP. It seems to work until the echo, and then it doesn't do anything.
The script and the PHP file are in the same folder.
This is the PHP code:
<?php
$comando = "sh ftpgesdoc.sh";
$result=exec($comando);
echo $result;
?>
And this is shell script. When I execute from the web, I can see the echo "ejecutando sftp", but nothing happens after this point.
#!/bin/sh
echo "ejecutando sftp"
folder="/aaa/bbb"
file="xxx.PDF"
sftp UserXX#nnn.nn.n.nn << EOF
cd $folder
get $file test.pdf
EOF
exec returns only the last line from the command output. If you want to capture entire output, use proc_open. See this answer, for instance.
you have to give the full path to file
and use this 2>&1 and know the error
try something like this
$comando = "sh pathTofile/location/ftpgesdoc.sh";
if(exec("$comando 2>&1", $output, $return_var))
{
print_r($output);
echo "<br>";
print_r($return_var);
}
I am trying to run a bash script from the web using php exec() method. The same bash script when ran from the terminal ./test.sh works fine executes all the command without any error. But when i try to run it from the command line it would absolutely not work. They both are in the same location. I have even give chmod 777 permission to that file.
Commnads like ls , pwd etc work but cf isn't working ?
test.sh
/usr/bin/cf login -a https://api.stage1.ng.bluemix.net -o jetmak#ca.blue.com -s dev -u jenk#ca.blue.com -p pass22
/usr/bin/cf api
/usr/bin/cf
executeshellfromphp.php
header('Content-Type: application/json');
$bluemixid = $_POST['bluemixid'];
$bluemixpwd = $_POST['bluemixpswd'];
$env = $_POST['env'];
$restart = $_POST['action'];
$result =shell_exec('sh /var/www/shellscriptphp/test.sh '.$env.' '.$restart.' '.$bluemixid.' '.$bluemixpwd.' ');
echo json_encode(array("result"=>$result));
exit();
Error:
FAILED
Config error: Error writing to manifest file:.cf/config.json
open .cf/config.json: no such file or directory
I am trying to Execute a multiple commands in php using exec() and shell_exec but i am getting a null value back which i shouldn't and nothing is happening (if i copy and paste the strings below in the command line it will work fine and accomplish the job needed) this is the commands i am using:
$command = "cd /../Desktop/FolderName;";
$command .= 'export PATH=$PATH:`pwd`;';
$command .= 'Here i execute a compiler;';
and then i use the escapeshellcmd()
$escaped_command = escapeshellcmd($command);
then
shell_exec($escaped_command);
any ideas what i am doing wrong and i also tried escapeshellarg() instead of escapeshellcmd()?
Solution: the Problem was the permission of the execution compiler for other owners is non and this was the problem.
because when you are using exec() function in php the owner of the file will be www-data so you need to give permission for the www-data either from the ACL of ubuntu or whatever linux based operating system(you can know the owner by doing this exec('whoami')), or by the files you need to execute.
(Sorry my bad English)
On Linux you can add your Commands in a Shell Script.
You can put this in any file:
#!/bin/bash
cd /../Desktop/FolderName
export PATH=$PATH:`pwd`
EXECUTE COMPILER
And save this as fille.sh
Then, add execution permissions:
chmod +x path/to/file.sh
From PHP, you can call this Script executing:
shell_exec('sh path/to/file.sh');
Hope this helps!