I am trying to do a simple project to start a VirtualBox VM using a PHP script. My server is win 7 ultimate and running php5.3 I am using the vboxmanage.exe to start the server. cmd works great in a batch file or typed right into cmd line.
When I use:
exec("path to vboxmange.exe" startvm "vm name");
it doesnt work. If I change the code to:
exec(dir);
it works fine. I did some searching and everyone seemed to mention permissions for IIS_IUSRS. I set it to have read and execute on the vboxmanage.exe and tried to do it for cmd.exe but cant seem to get it to allow me to do this, though it doesnt seem necessary as it can run other cmds like dir.
another theory I have is that exec wraps the entire cmd in "" and this doesnt work when I try to type it in to cmd line manually. I have tried to trim it off but that doesnt work cause exec adds it on so nothing I do before can stop this.
any suggestions for another way to do this or what it might be?
From exec in the PHP Manual:
Note: When safe mode is enabled, you can only execute files within the
safe_mode_exec_dir. For practical reasons, it is currently not allowed
to have .. components in the path to the executable.
Check the value of your safe_mode_exec_dir in your php.ini. I would guess that it would be fairly restrictive by default.
If that doesn't work, take a look in the comments. Particularly, this comment might be of help to you.
Related
I'm trying to execute a php script from a php file, but it seems that i can't get the correct bin path of the php.exe. Its not doing anything as far as i can tell. Lets assume service-oauth.php is a simple echo.
EDIT3:
I fixed it using php-cli instead of php or the php bin path, i must admit that i tried this before but it seems something else was off when i tried this (one of the first things i tried). The answer provided by Keith in Can't execute PHP script using PHP exec , so it ended being a duplicate :S, thanks for the help to those who commented.
EDIT2:
I tried calling the script directly from the server console as #Dagon suggested and it works, both using the php env variable and the php path to the bin, its clear that the path is correct, but something is preveting to get the output or to run the script using the php exec() function
service-oauth.php
<?php echo "Hello there"; ?>
And this is my script:
$basePath = plugin_dir_path( __FILE__ ); # Wordpress function, asume it works.
$fileToExc = $basePath . 'service-oauth.php';
# PHP_BINDIR: /usr/local/bin
# PHP_BINARY: /usr/bin/php
# exec("which php") /usr/bin/php
$phpPath = exec("which php");
$output = exec("$phpPath $fileToExc");
print_r($output);
There are a lot of answers on stackoverflow that recommend any of those 3 options i commenented in the code, but none of them seems to work, not sure if it is the path or something else that is not working. I've tested this script on my localmachine (windows) and it works (even though i had to use a hardcoded path to the bin since i wans't getting the correct path), but i'm testing on my production server (linux) and is not working.
Note: Let me be clear, that this is not a duplicate, i've tried the following answers in these posts and many others, and it didn't work for me:
How to get path of the php binary on server where it is located
Can't execute PHP script using PHP exec
PHP exec to run a file
How to call shell script from another shell script?
Execute a PHP script from another PHP script
I've also tried using .exe at the end of the binary(windows localmachine), using php-cli instead of php, and i've tested the excec function and it works, but not in this case. Also tried with a shebang in the called script.
It's propably something simple that i'm not aware of but i've been spending a lot of hours searching and testing and nothing so far. Any help is appreciated.
EDIT:
Using scandir on the bin folder shows
scandir("/usr/bin/")
Array(
...
[632] => php
[633] => php-cgi
[634] => php-cli
...
)
Tested if in safe mode using ini_get('safe_mode'), but it seems off.
I fixed it using php-cli instead of php or the php bin path, i must admit that i tried this before but it seems something else was off when i tried this (one of the first things i tried). The answer provided by Keith in Can't execute PHP script using PHP exec , so it ended being a duplicate :S, thanks for the help to those who commented. – Zagen
I am having issues executing a VBScript through Apache (WAMP) on Windows Server 2012. I am attempting to convert a Docx to PDF, and the script runs perfectly from the command line, but fails when running through PHP. Rather than posting the vbscript, I will provide a link to it: http://bit.ly/1gngYAn
When executed through PHP as follows, WINWORD.exe starts, as does the VBScript, and it hangs there and nothing happens. No PDF is generated (and I never see the ~temporary.docx hidden file pop in the directory).
I have tried just about every iteration of exec, system, passthru and COM ( 'WScript.Shell' ), and all have the same outcome.
To avoid "escaping" issues, I also tried executing the script though a .bat file so no arguments needed to be passed, and the outcome was the same.
Here is my current php code (convert.vbs is the code from the link above):
$obj = new COM ( 'WScript.Shell' );
$obj->Run ( 'cmd /C wscript.exe //B C:\Users\Administrator\Desktop\convert.vbs c:\wamp\www\fileconv\temp_store\52fa8272bf84f.docx', 1, false );
//I have tried different "window styles" too, and it doesn't make a difference
I also tried modifying the apache service user to run as administrator (this is not a production server), and enabled "Allow service to interact with the desktop", and it had the same outcome.
I have also made sure the directories had "full control" by everyone (reading, writing, executing, etc).
It runs perfectly if I run from the command line or with my ".bat" file.
Since it hangs (the script and word, not apache), I have looked at the event viewer in the control panel, but there are no events that pertain.
My questions is firstly, why is this happening, and secondly, if the first cannot be answered, is there a way that I can get a more in depth look at what is happening when the process is executed, as to further troubleshoot it? As of now, I have no data to review or output to see to help me troubleshoot.
Please feel free to ask for any details. I have tried many, many iterations to try to get this to work, searched high and low, and can't seem to come up with any answers.
I appreciate your assistance,
Louis
It took me a couple of days, but here is the solution I found:
I used PsExec - http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx
The following flags are required: -h -i -accepteula -u -p
(I tried without the -h, -accepteula and -i, but no dice. This is running on Windows Server 2012 under WAMP)
Here is an example:
exec('c:\psexec\PsExec -h -i -accepteula -u Administrator -p '.$password.' C:\Windows\System32\CScript.exe //Nologo //B c:\wamp\www\fileconv\convert.vbs '.$filename)
Now it executes properly and as intended.
I hope this helps someone in the same situation!
PS The WScript.Shell method of execution I used in my question works just as well as exec(), except exec() waits until the process exits.
You should use exec() function
this is the url http://php.net/manual/fr/function.exec.php
I am installing InfiniteWP on a local environment using MAMP. Everything works perfectly except for my inability to figure out how to setup a cronjob properly. I did find some information (dated?).
I found this: http://www.freewaytalk.net/thread/view/114906 . But am a bit worried that I may be implementing something dated.
The instruction given by InfiniteWP in order to start the cronjob say this:"php -q -d safe_mode=Off /Applications/MAMP/htdocs/IWP/cron.php >/dev/null 2>&1" So I need instruction on how to implement this php command on my Mac. Terminal?
I'd really appreciate advice and validation on the best course of action.
You could always use a GUI such as : http://code.google.com/p/cronnix/
This is simply a front end to the crontab tool, then simply set the time you want it to run and the URL to:
/Applications/MAMP/htdocs/IWP/cron.php
I didn't have any luck with that google code option but Mac has cron natively and your InfiniteWP code can be added in a few lines. See instructions here: http://www.maclife.com/article/columns/terminal_101_creating_cron_jobs
I have a program that returns a comma-separated string of numbers after doing some background processing. I intended to run this in symfony using shell_exec; however, all I get is NULL (revealed through a var_dump(). I tried the following debugging steps.
I ran the file (it's a PHP class) through a command-line lime unit test in Symfony - it works and gives the correct result there.
Just to check, I tried a simple command ls -l at the same place to see whether I would get anything. Again, I had the same problem - the var_dump in the browser showed NULL, but it worked through the command line.
What could be the problem? Are there restrictions on running shell_exec() in a browser?
EDIT: Just to clarify, shell_exec() commands work when I run them as standalone php scripts on the web server (for example, by putting them in my document root. They don't seem to be working under the symfony framework, for some reason.
I finally solved it, and it turned out to be something quite simple, and quite unrelated.
The shell command I was running was in this format: face_query -D args. I didn't realize that Apache would be executing PHP as user www-data and thus the program face_query wouldn't be in the PATH (the directory is actually ~/bin). Changing the program name to the full path of the program solved it.
I also gather from this that only commands which www-data has permission to execute can be run. In this case, www-data is in the same group as my user, but it might be a problem otherwise.
Have you tried using exec? Or one of the other variants. I am never sure of which one to use and always lump with exec.
http://uk.php.net/manual/en/function.exec.php
Is your web server running php in safe mode?
Note: This function is disabled when PHP is running in safe mode.
From: http://php.net/manual/en/function.shell-exec.php
I am using this php code:
exec("unrar e file.rar",$ret,$code);
and getting an error code of illegal command ie 127 ... but when I am using this command through ssh its working ... because unrar is installed on the server ... so can anyone guess why exec is not doing the right stuff?
Try using the direct path of the application (/usr/bin/unrar of whatever), it sounds like php can't find the application.
If you have chrooted apache and php, you will also want to put /bin/sh into the chrooted environment. Otherwise, the exec() or passthru() will not function properly, and will produce error code 127, file not found.
Since this comes up as a top answer in google, I wanted to share my fix:
The simple fix I had was to disable safe_mode in the php.ini file
; Safe Mode
; http://www.php.net/manual/en/ini.sect.safe-mode.php#ini.safe-mode
safe_mode = Off
thanx all for your response!!
I tried this
//somedir is inside the directory where php file is
chdir("somedir");
exec("/home/username/bin/unrar e /home/path/to/dir/file.rar");
and now it returned no exit code ... oher commands are doing file .. i tried mkdir etc .. :s
Just in case somebody else still gets this problem, take a look at the post here:
http://gallery.menalto.com/node/2639#comment-8638
Quote:
I found the problem. The problem was my security-paranoid OpenBSD. When upgrading from 3.1 to 3.2 they added:
Apache runs chroot'd by default. To disable this, see the new -u option.
The chroot prevented Apache from accessing anything outside of a directory, so I moved everything into the apache directory including netpbm. Everything was accessible and executable, but I guess it was still in some sort of "safe mode" because the exec() always returned 127.
Anyway, running httpd with the -u option went back to the less secure non chroot'd apache startup, which allowed the exec() to work again.
ohkiee guyz thanx ... and yes there might be some errors with $PATH ... but with given full path its working :)
exec("/home/user/bin/unrar e /home/user/xxx/yyy/file.rar");
I did not find the solution for my problem of the same type so sharing what was the cause of it in my Linux setup.
For whatever reason I needed an apache module loaded before other modules and so apache was started with LD_PRELOAD. As exec inherits the environment of the parent process LD_PRELOAD was used for starting the exec-ed program (through sh). Now the preloaded module uses some bindings to apache functions and of course they are not to be present in sh. The result of the php exec was an exit status of 127. The solution was to have in my php file a putenv("LD_PRELOAD") that gets executed before any exec calls.