PHP Error[2]: exec(): Unable to fork [pwd]
PHP Error[2]: system(): Unable to fork [pwd]
PHP Error[2]: shell_exec(): Unable to execute 'pwd'
PHP Error[2]: system(): Unable to fork [pwd]
PHP Error[2]: shell_exec(): Unable to execute 'pwd'
How do you fix these errors? How do you run Unix commands inside a PHP script on a Windows machine?
I am using Windows 7.
Possible solutions
Many articles on Google suggest that this issue is due to incorrect permissions. For examples, you need to grant the user IUSER read and write permission if you use IIS server. Or you need to run cmd.exe as an admin. In my situation, none of these fixes worked. Read on to see how I fixed this issue.
The REAL solution
The solution is simply not run Unix commands inside a PHP script. Instead, you run a shell script which embeds the Unix commands as well as the rest of the PHP script you want to run. To achieve this goal, you cannot use Windows command prompt, or cmd.exe. You must use a Unix-like environment such as Cygwin or MobaXterm. I use MobaXterm Personal Edition v7.0 and it is very lightweight and has everything I need.
MobaXterm runs Bash shell.
Suppose you want to run a PHP scriped called phpScript.php, followed by an scp command. Create a new shell script called my-script.sh and put the following in it:
#!/bin/bash # my-script.sh read -p "This script will run phpScript.php. (press Enter to continue)" userInput php phpScript.php read -p "This script will run an scp command. (press Enter to continue)" userInput `scp -i /drives/c/my_rsa_private_key.pem ubuntu@1.2.3.4:/tmp/some-file.txt /drives/d/some-file.txt`That's it! You can even run one or more commands on a remote Unix box via SSH like the following:
ssh -i /drives/c/my_rsa_private_key.pem ubuntu@1.2.3.4 "cd my-script-directory; ./some-script.sh"
This line connects to a remote machine 1.2.3.4 via SSH and runs the following commands on 1.2.3.4:
cd my-script-directory
./some-script.sh
./some-script.sh
So in a nutshell, you put everything you need inside my-script.sh, and run my-script.sh in a Unix-like command line terminal.
Questions? Let me know!