How to run "ANT" command in batch file through PHP? - php

My batch file includes following code,
cd C:\Ant
Ant
How can I execute this file in PHP.
I've tried everything including,
system();
exec();
passthru();
but none of these functions worked for me.

Batch files are no executables. Try
cmd /c your-batch-file.bat
Here cmd.exe is the executable. The /c options tells cmd to run the following command (here your batch file).
Update:
Without any PHP knowledge: the functions you are trying to use might be disabled due to safe mode.
It would be helpful (for any PHP developers) if you post the code line that tries to call the bat. Maybe you just missed a parameter or something like that.

Related

Execute a windows shortcut from php

I have a situation where I need to call a batch file from a php script... however this batch file needs to run as admin in order to work.
My solution was to create a shortcut to the batch file and check the box to run as admin from the shortcut... however I can't get php to call the shortcut.
I have tried:
exec("C:/path/movefiles_admin.lnk")
and
system("cmd /c C:/path/movefiles_admin.lnk");
Neither of which work. Any suggestions?
Try this:
exec("START C:/path/movefiles_admin.lnk");
START Starts a separate Command Prompt window to run a specified program or command.
You can run nonexecutable files through their file association by typing the name of the file as a command
If your PHP has issues executing shortcut to batch file, try executing simple read and write actions to a test.txt file. (To check whether you have PHP running in safe mode).
If it doesnt do these basic actions then you have some configuration issues.
If a program is started with exec function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends.
Please refer this link for your version of PHP: manual

How to modify IIS permissions to run batch scripts in PHP?

I am trying to have a PHP file call a Windows batch script using
exec("runPy.bat");
I have also tried
exec("cmd /c \\server\somePath\runPy.bat");
It doesn't matter what I try, the batch script doesn't work. And I don't get any errors. After much research I found a post saying that the problem may be due to privaleges: https://stackoverflow.com/a/11613662
Can anyone show me safe/proper way to set up permissions so that I can call batch scripts in IIS? Or is it unsafe in general to do that? Using IIS8 if that helps.
To run batch, you need to use cmd, which you can call via:
system("cmd /c C:[path to file]");

shell_exec do not run php script

I'm trying to run a .php script from a php file (website) and for that I'm using shell_exec function.
I have php-cli installed and the file is executable, but it's still not working.
I have read a lot of tutorials, and also some questions and answers in here but I just can't make this work.
Here is the piece of code:
shell_exec('/usr/bin/php /opt/lampp/htdocs/projects/mix/2/includes/connections/stomp_con.php');
I tried commands like shell_exec('pwd'); and shell_exec('whoami'); and they all work.
I also know that in order to run two commands we should be separated by semi-colon but this is just one. I run it in the terminal and it work.
Thanks in advance for your help
EDIT:
I var_dumped the shell_exec(); and it's showing me string(104) usr/bin/php: /opt/lampp/lib/libxml2.so.2: version LIBXML2_2.9.0' not found (required by /usr/bin/php) because I add 2>&1 in order to output STDERR
You need to provides space at the end of the file.something like this:
shell_exec("/usr/bin/php/opt/lampp/htdocs/projects/mix/2/includes/connections/stomp_con.php ");

PHP exec not working for compiling on Windows 8 through Visual Studio 2012 Command Prompt

Thank you for taking the time to help me today. I have what I hope is a simple question. I have been attempting to use php exec() or any related PHP command to Open up the Developer Command Prompt for Visual Studio 2013 and use it to compile a file and save the output to a file on my local machine. I have it working fine from Run on Windows, but I can't seem to get it to work with PHP exec(). Here is how I have the command set up currently.
$cmd = 'C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Tools\VsDevCmd.bat && cd C:\wamp\www\csc424Try3\app\uploads && cl /EHsc basic.cpp && basic >> C:\wamp\www\csc424Try3\app\outputs\output.txt';
exec($cmd, $result);
As you can see, I am chaining together commands. The first command allows the prompt to open, the second changes to the proper directory, the third runs the command for compiling in the prompt, and the fourth outputs to a text file.
Any ideas where I'm going wrong? I had a feeling it had something to do with formatting, but perhaps chaining the commands together does not work in PHP the way it does on the system.
You're forgetting quotes around most of your arguments in the exec call's string, meaning that things like Program Files will be seen as two separate things, not a single directory.
$cmd = '"C:\Program Files (x86)\....VsDevCmd.bat" && etc...';
^-- ^-- need these
as is, you're trying to execute a program called C:\Program, with some extra arguments like Files (x86)\......
I found the answer to my own question after some research. The way I was trying to do it before just wasn't working. After talking to a colleague, I thought writing a .bat file for the background would work better, and it absolutely did. I wrote the chain of commands in a .bat file and executed it like follows;
system("cmd /c C:\wamp\www\csc424Try3\app\uploads\stuff.bat");
Success! The file can now be successfully compiled and ran from PHP. :) It is a good day.

PHP shell_exec() issue

I am having an issue using the PHP function shell_exec().
I have an application which I can run from the linux command line perfectly fine. The application takes several hours to run, so I am trying to spawn a new instance using shell_exec() to manage better. However, when I run the exact same command (which works on the command line) through shell_exec(), it returns an empty string, and it doesn't look like any new processes were started. Plus it completes almost instantly. shell_exec() is suppose to wait until the command has finished correct?
I have also tried variations of exec() with the same outcome.
Does anyone have any idea what could be going on here?
There are no symbolic links or anything funky in the command: just the path to the application and a few command line parametes.
Some thing with you env
See output of env from cli (command line interface) and php script
Also see what your shell interpreter?
And does script and cli application runs from one user?
If so, se option safe_mode
Make sure the user apache is running on (probably www-data) has access to the files and that they are executable (ls -la). A simple chmod 777 [filename] would fix that.
By default PHP will timeout after 30 sec. You can disable the limit like this:
<?php
set_time_limit(0);
?>
Edit:
Also consider this: http://www.rabbitmq.com/

Categories