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

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?  
LVL 1
ScamquistAsked:
Who is Participating?
 
RobConnect With a Mentor Owner (Aidellio)Commented:
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
 
RobOwner (Aidellio)Commented:
what about using the COM


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

Open in new window

0
 
ScamquistAuthor Commented:
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
RobConnect With a Mentor Owner (Aidellio)Commented:
The example i've given above will show you the working directory, so you will have to specify relative to that
0
 
ScamquistAuthor Commented:
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
 
RobOwner (Aidellio)Commented:
What does the batch file do?  There may be another way of achieving it...
0
 
ScamquistAuthor Commented:
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
 
RobOwner (Aidellio)Commented:
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
 
ScamquistAuthor Commented:
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
 
RobOwner (Aidellio)Commented:
That's no good about the server... good luck :)

Not sure what you mean by swapping mssql with sqlsrv?

0
 
ScamquistAuthor Commented:
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
 
RobOwner (Aidellio)Commented:
ok sure :)  i don't use sql server so pardon the ignorance.

is your server back up?
0
 
ScamquistAuthor Commented:
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
 
ScamquistAuthor Commented:
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
 
RobOwner (Aidellio)Commented:
I'll wait to hear from you :)
0
 
ScamquistAuthor Commented:
Still here.  Need to change the syntax for sqlsrv_connnect.   Lemme fix and see if it works.
0
 
ScamquistAuthor Commented:
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
 
RobOwner (Aidellio)Commented:
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
 
ScamquistAuthor Commented:
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
 
ScamquistAuthor Commented:
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
 
ScamquistAuthor Commented:
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
 
RobOwner (Aidellio)Commented:
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
 
RobOwner (Aidellio)Commented:
also where does spRecipeUpload come from?
0
 
ScamquistAuthor Commented:
spRecipeUpload is a stored procedure in SQL 2005
0
 
ScamquistAuthor Commented:
Removing the curly brackets { } had no effect
0
 
RobOwner (Aidellio)Commented:
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
 
ScamquistAuthor Commented:
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
 
RobOwner (Aidellio)Commented:
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
 
ScamquistAuthor Commented:
Yes, when run from a command line, the stored procedure runs properly.
0
 
RobOwner (Aidellio)Commented:
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
 
ScamquistAuthor Commented:
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
 
RobOwner (Aidellio)Commented:
0
 
ScamquistAuthor Commented:
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
 
ScamquistAuthor Commented:
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
 
RobOwner (Aidellio)Commented:
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
 
ScamquistAuthor Commented:
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
 
ScamquistAuthor Commented:
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
 
ScamquistAuthor Commented:
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
 
ScamquistAuthor Commented:
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
 
RobOwner (Aidellio)Commented:
Thanks and glad you got there in the end. I can't see where you've posted the solution though...
0
All Courses

From novice to tech pro — start learning today.