Solved

Warning: exec(): Unable to fork...when calling batch file from PHP

Posted on 2011-09-26
42
1,623 Views
Last Modified: 2012-05-12
I am trying to call a batch file from PHP.  

The batch file is just a confirmation message.  The batch file does run from from a command line.

I keep getting the error: Warning: exec(): Unable to fork
I posted this question before and got it to work.  I needed to restore the server to a previous state.  Now, I cannot get exec statements to work again.  

I gave Read and Read & Write permissons to user IUSR_COMPUTERNAME on CMD.EXE. Giving full permission did not help.  I even gave all domain users full permission to see if that would help.  It didn't



>echo %COMSPEC%
C:\WINDOWS\system32\cmd.exe

>cacls %COMSPEC%
C:\WINDOWS\system32\cmd.exe BUILTIN\Administrators:F
                            NT AUTHORITY\BATCH:R
                            NT AUTHORITY\INTERACTIVE:R
                            GAMAY\IUSR_GAMAY-SRV1:R
                            NT AUTHORITY\SERVICE:R
                            NT AUTHORITY\SYSTEM:F
                            GAMAY\TelnetClients:R


I tried to copy the CMD.EXE to my C:\PHP folder.


I tried the follwing commands

exec('ConfirmRecipe.bat');
Error:
Warning: exec(): Unable to fork [ConfirmRecipe.bat] in C:\Inetpub\wwwroot\CallFile6.php on line 2

shell_exec('ConfirmRecipe.bat');
Error:
Warning: shell_exec(): Unable to execute 'ConfirmRecipe.bat' in C:\Inetpub\wwwroot\CallFile6.php on line 2

shell_exec("C:\Inetpub\wwwroot\RecipeConfirm.bat");
Error:
Warning: shell_exec(): Unable to execute 'C:\Inetpub\wwwroot\RecipeConfirm.bat' in C:\Inetpub\wwwroot\CallFile6.php on line 2

Is there anything in the php.ini that could be causing this?  
0
Comment
Question by:Scamquist
  • 23
  • 17
42 Comments
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
Comment Utility
what about using the COM


$WshShell = new COM("WScript.Shell");
$oExec = $WshShell->Run("ConfirmRecipe.bat", 7, false);

Open in new window

0
 
LVL 1

Author Comment

by:Scamquist
Comment Utility
I entered:
<?php
$WshShell = new COM("WScript.Shell");
$oExec = $WshShell->Run("ConfirmRecipe.bat", 7, false);
?>

threw the error:

Fatal error: Uncaught exception 'com_exception' with message 'Source: Unknown Description: Unknown' in C:\Inetpub\wwwroot\CallFile.php:3 Stack trace: #0 C:\Inetpub\wwwroot\CallFile.php(3): com->Run('ConfirmRecipe.b...', 7, false) #1 {main} thrown in C:\Inetpub\wwwroot\CallFile.php on line 3
0
 
LVL 42

Accepted Solution

by:
Rob Jurd, EE MVE earned 500 total points
Comment Utility
Use Exec instead of Run, sorry my bad.  Exec gives you access to the input, output and error streams

<?php
$WshShell = new COM("WScript.Shell");
$oExec = $WshShell->Exec("cmd /c dir");

echo ReadAllFromAny($oExec);


Function ReadAllFromAny($oExec) {

     If (!$oExec->StdOut->AtEndOfStream) {
          return $oExec->StdOut->ReadAll;
 	}

     If (!$oExec->StdErr->AtEndOfStream) {
          return "STDERR: " . $oExec->StdErr->ReadAll;
 	}

     return -1;
}

?>

Open in new window

0
 
LVL 42

Assisted Solution

by:Rob Jurd, EE MVE
Rob Jurd, EE MVE earned 500 total points
Comment Utility
The example i've given above will show you the working directory, so you will have to specify relative to that
0
 
LVL 1

Author Comment

by:Scamquist
Comment Utility
I replaced cmd /c dir with the full path to the batch file.  

When I opened the page, the batch file did not run, but the page retruned -1


fyi, the contents of the batch file are:
Msg * "Recipe has been copied to MAS"
<?php
$WshShell = new COM("WScript.Shell");
$oExec = $WshShell->Exec("cmd /c C:\Inetpub\wwwroot\RecipeConfirm.bat");

echo ReadAllFromAny($oExec);


Function ReadAllFromAny($oExec) {

     If (!$oExec->StdOut->AtEndOfStream) {
          return $oExec->StdOut->ReadAll;
 	}

     If (!$oExec->StdErr->AtEndOfStream) {
          return "STDERR: " . $oExec->StdErr->ReadAll;
 	}

     return -1;
}

?>

Open in new window

0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
Comment Utility
What does the batch file do?  There may be another way of achieving it...
0
 
LVL 1

Author Comment

by:Scamquist
Comment Utility
the RecipeConfirm.bat was a test.  I thought if this batch file would work, all would work.

Ultimately, I need to
stop sql server 2005
start sql server 2005
run a stored procedure
dhange drive and invoke a program
run a stored procedure

Below is a sample of the full code I need to run

net stop "MSSQLSERVER"
net start "MSSQLSERVER"

osql -E -d Iris -S Gamay-srv1 -Q "Exec spRecipeUpload"
cd\
d:
cd\Iris\htdocs
start GAMimportrecipelnk
osql -E -d Iris -S Gamay-srv1 -Q "Exec RunRefresh"



to run the stored procedure, I tried

system('osql -E -d Iris -S Gamay-srv1 -Q "Exec RunRefresh"');

this produced the error:
Warning: system(): Unable to fork [osql -E -d Iris -S Gamay-srv1 -Q "Exec RunRefresh"] in C:\Inetpub\wwwroot\MyTest.php on line 2
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
Comment Utility
PHP has built in support for SQL Server
http://au.php.net/manual/en/function.mssql-execute.php

I haven't tested this but it's worth a shot seeing as it's not that much code.  You could wrap it in a function for simplicity
<?php
$server = 'Gamay-srv1\Iris';

// Connect to MSSQL
$link = mssql_connect($server);

$WshShell = new COM("WScript.Shell");
$oExec = $WshShell->Exec('cmd /c net stop "MSSQLSERVER"');
$oExec = $WshShell->Exec('cmd /c net start "MSSQLSERVER"');

$stmt = mssql_init('spRecipeUpload');
mssql_execute($stmt);
mssql_free_statement($stmt);

$oExec = $WshShell->Exec('cmd /c d:\Iris\htdocs\GAMimportrecipelnk');

$stmt = mssql_init('RunRefresh');
mssql_execute($stmt);
mssql_free_statement($stmt);

?>

Open in new window

0
 
LVL 1

Author Comment

by:Scamquist
Comment Utility
My server crashed tonight, so I cannot test it right now.  

Should I be able to swap mssql with sqlsrv in the statements?

I will let you know how it works as soon as I get my server back up.

Thank you for you help.
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
Comment Utility
That's no good about the server... good luck :)

Not sure what you mean by swapping mssql with sqlsrv?

0
 
LVL 1

Author Comment

by:Scamquist
Comment Utility
I am using PHP 5.3.6.  To connect to SQL, I use sql_connect(.....

___________________________________________________________________________

from
http://social.msdn.microsoft.com/Forums/en-US/sqldriverforphp/thread/b64321c9-658e-49f2-9fdd-168ff5031863/
___________________________________________________________________________

The SQL Server driver for PHP developed and supported by Microsoft is “sqlsrv” and not “mssql”. The two drivers are similar, but not compatible. IA quick glance through the APIs list should reveal the differences. For instance, mssql has a “bind” function, while “sqlsrv” has not (as it implements implicit binding with prepared statements). In summary, there is no (backward) compatibility between the two drivers.

Additionally, “mssql” was dropped from PHP 5.3 distribution. Since the “php_mssql.dll” is no longer deployed with PHP 5.3, all calls to “mssql_” API are expected to fail.
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
Comment Utility
ok sure :)  i don't use sql server so pardon the ignorance.

is your server back up?
0
 
LVL 1

Author Comment

by:Scamquist
Comment Utility
Not yet.  I think I need a bigger hammer.  :)

I need to take it to another level and have some esle look at it.  Hope to have it back up today...unless the drive controller is bad.

Once I am back up, I will try your suggestion and hope for success.

Thank you for you patience.

Steve
0
 
LVL 1

Author Comment

by:Scamquist
Comment Utility
I expect to have a replacement server by the end of the week.  I hope we can solve this issue at that time.  

Thank you.
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
Comment Utility
I'll wait to hear from you :)
0
 
LVL 1

Author Comment

by:Scamquist
Comment Utility
Still here.  Need to change the syntax for sqlsrv_connnect.   Lemme fix and see if it works.
0
 
LVL 1

Author Comment

by:Scamquist
Comment Utility
I made the following changes.  php.5.3.8 uses sqlsrv instead of mssql

the mssql_init appears to have been replaced with

$stmt =  "{call StoredProcedureName(RunRefresh)}";
   
(for a list of sqlsrv commands, see
http://blogs.msdn.com/b/brian_swan/archive/2010/03/10/mssql-vs-sqlsrv-what-s-the-difference-part-2.aspx  )

I connect to the database, but now I get the following errors:

Connection established.
Warning: sqlsrv_execute() expects parameter 1 to be resource, string given in C:\Inetpub\wwwroot\MyTest.php on line 21
Warning: sqlsrv_free_stmt() expects parameter 1 to be resource, string given in C:\Inetpub\wwwroot\MyTest.php on line 22
Warning: sqlsrv_execute() expects parameter 1 to be resource, string given in C:\Inetpub\wwwroot\MyTest.php on line 27
Warning: sqlsrv_free_stmt() expects parameter 1 to be resource, string given in C:\Inetpub\wwwroot\MyTest.php on line 28


<?php
$serverName = "(local)";
$connectionInfo = array("UID" => "UserName", "PWD" => "Password", "Database"=>"DatabaseName");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn )
{
     echo "Connection established.<br>";
}
else
{
     echo "Connection could not be established.<br>";
     die( print_r( sqlsrv_errors(), true));
}

$WshShell = new COM("WScript.Shell");
$oExec = $WshShell->Exec('cmd /c net stop "MSSQLSERVER"');
$oExec = $WshShell->Exec('cmd /c net start "MSSQLSERVER"');

$stmt =  "{call StoredProcedureName(spRecipeUpload)}";

sqlsrv_execute($stmt);
sqlsrv_free_stmt($stmt);

$oExec = $WshShell->Exec('cmd /c d:\Iris\htdocs\GAMimportrecipelnk');

$stmt =  "{call StoredProcedureName(RunRefresh)}";
sqlsrv_execute($stmt);
sqlsrv_free_stmt($stmt);

?>

Open in new window

0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
Comment Utility
You need to "prepare" the statement, that's all your missing

add this at line 20:

$prep_stmt = sqlsrv_prepare($conn, $stmt);

Open in new window

change line 21:
sqlsrv_execute($prep_stmt);

Open in new window

0
 
LVL 1

Author Comment

by:Scamquist
Comment Utility
Do I need to change the other lines with $stmt to $prep_stmt  ?

Do I need to add

$prep_stmt = sqlsrv_prepare($conn, $stmt);

after line 26?
0
 
LVL 1

Author Comment

by:Scamquist
Comment Utility
tagit - you are the BEST.  I spent too may days searching for a solution.  

The code below works.

Thank you so much
<?php
$serverName = "(local)";
$connectionInfo = array("UID" => "UserName", "PWD" => "Password", "Database"=>"Database");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn )
{
     echo "Connection established.<br>";
}
else
{
     echo "Connection could not be established.<br>";
     die( print_r( sqlsrv_errors(), true));
}

$WshShell = new COM("WScript.Shell");
$oExec = $WshShell->Exec('cmd /c net stop "MSSQLSERVER"');
$oExec = $WshShell->Exec('cmd /c net start "MSSQLSERVER"');

$stmt =  "{call StoredProcedureName(spRecipeUpload)}";
$prep_stmt = sqlsrv_prepare($conn, $stmt); 
sqlsrv_execute($prep_stmt); 
sqlsrv_free_stmt($prep_stmt);

$oExec = $WshShell->Exec('cmd /c d:\Iris\htdocs\GAMimportrecipelnk');

$stmt =  "{call StoredProcedureName(RunRefresh)}";
$prep_stmt = sqlsrv_prepare($conn, $stmt); 
sqlsrv_execute($prep_stmt);
sqlsrv_free_stmt($prep_stmt);
?>

Open in new window

0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 1

Author Comment

by:Scamquist
Comment Utility
Too quick.    I thought it ran the stored procedures, but they did not run.  

The only difference is the code did not produce an error.
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
Comment Utility
I suggest looking at the example on this site: http://php.net/manual/en/function.sqlsrv-prepare.php

Do you need the curly brackets? {}  try removing them

$stmt =  "call StoredProcedureName(spRecipeUpload)";
$prep_stmt = sqlsrv_prepare($conn, $stmt); 
sqlsrv_execute($prep_stmt); 
sqlsrv_free_stmt($prep_stmt);

Open in new window

0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
Comment Utility
also where does spRecipeUpload come from?
0
 
LVL 1

Author Comment

by:Scamquist
Comment Utility
spRecipeUpload is a stored procedure in SQL 2005
0
 
LVL 1

Author Comment

by:Scamquist
Comment Utility
Removing the curly brackets { } had no effect
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
Comment Utility
ok not too sure of your setup but can't you call the stored procedure directly like this?

$stmt =  "exec spRecipeUpload";
$prep_stmt = sqlsrv_prepare($conn, $stmt); 
sqlsrv_execute($prep_stmt); 
sqlsrv_free_stmt($prep_stmt);

Open in new window

0
 
LVL 1

Author Comment

by:Scamquist
Comment Utility
This change did not run the stored procedure.

It is strange.  The first 13 lines of my codes establish and confirms the connection to the SQL server.

However, nothing happens after that.  None of the stored procedures run, nor does the GAMimportrecipelnk command.

0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
Comment Utility
are you able to run the stored procedure (and confirm it ran) from the command line?

osql -E -d Iris -S Gamay-srv1 -Q "Exec spRecipeUpload"

Open in new window

0
 
LVL 1

Author Comment

by:Scamquist
Comment Utility
Yes, when run from a command line, the stored procedure runs properly.
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
Comment Utility
ok then I would try the query version of sqlsrv rather than the execute (as it is just the one query)

eg.

$sql = "Exec spRecipeUpload";
$stmt = sqlsrv_query( $conn, $sql);
if( $stmt === false ) {
     die( print_r( sqlsrv_errors(), true));
}

Open in new window

0
 
LVL 1

Author Comment

by:Scamquist
Comment Utility
new error message.  Have not seen this before.

 
Connection established.
Array ( [0] => Array ( [0] => 42000 [SQLSTATE] => 42000 [1] => 229
 [code] => 229 [2] => [Microsoft][SQL Server Native Client 10.0]
[SQL Server]EXECUTE permission denied on object 'spRecipeUpload',
 database 'Iris', schema 'dbo'. [message] => [Microsoft][SQL Server
 Native Client 10.0][SQL Server]EXECUTE permission denied on 
object 'spRecipeUpload', database 'Iris', schema 'dbo'. ) )

Open in new window

0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
Comment Utility
0
 
LVL 1

Author Comment

by:Scamquist
Comment Utility
Thanks for the link.  I ran the

GRANT EXECUTE ON [storedProcName] TO [userName]

and threw another error.  

I will search this new error and post back.


0
 
LVL 1

Author Comment

by:Scamquist
Comment Utility
For others that may view this thread in the future, here is the latest error.


I will post back if I find a solution


Array ( [0] => Array ( [0] => 42000 [SQLSTATE] => 42000 [1] => 4629 [code] => 4629 [2] => [Microsoft][SQL Server Native Client 10.0][SQL Server]Permissions on server scoped catalog views or
 system stored procedures or extended stored procedures can be
 granted only when the current database is master. [message] =>
 [Microsoft][SQL Server Native Client 10.0][SQL Server]Permissions
 on server scoped catalog views or system stored procedures or
 extended stored procedures can be granted only when the current
 database is master. ) )

Open in new window

0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
Comment Utility
I've requested this to be looked at by other experts in MSSQL so hopefully you'll get someone that knows about that error
0
 
LVL 1

Author Comment

by:Scamquist
Comment Utility
I appreciate the amount it time you put into this.  It feels like we are getting close.  I guess if this was easy, they wouldn't call it work.

Thanks for the notice to the other experts.
0
 
LVL 1

Author Comment

by:Scamquist
Comment Utility
I've requested that this question be closed as follows:

Accepted answer: 0 points for Scamquist's comment http:/Q_27327634.html#36709127

for the following reason:

Your use of function got me in the right direction. &nbsp;I posted what finally worked at the bottom of this thread. &nbsp;While it wasn't all the way, it got me there. &nbsp;I really appreciate your help. &nbsp;Above and beyond what I would expect.
0
 
LVL 1

Author Comment

by:Scamquist
Comment Utility
I need to cancel this.  I did not mean to accept my solution.  Can you help.

I meant to select tagit's answer 09/27/11 02:37 AM, ID: 36707924

Steve lIndquist
0
 
LVL 1

Author Closing Comment

by:Scamquist
Comment Utility
Your use of function got me in the right direction.  I posted what finally worked at the bottom of this thread.  While it wasn't all the way, it got me there.  I really appreciate your help.  Above and beyond what I would expect.
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
Comment Utility
Thanks and glad you got there in the end. I can't see where you've posted the solution though...
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Button Click 11 23
How can I expand text with explaination 5 18
Apostophes in PHP generated form 6 12
sql calculate averages 18 24
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now