0

I have see lots of code sample of exec and system which can be use to run some php code in background.

<?php 
include_once('mail.php');

$response = array();    
            
// This Code Should Execute Without waiting for other code;
$response['status'] = true;
$response['msg'] = "Thank you  Welcome in SAAS Application. We will connect with you soon. :)";// Send response to webpage first then execute below code.   ;
echo json_encode($response);            



//THIS CODE SHOULD EXECUTE IN BACKGROUND WHICH COMES FROM INCLUDE MAIL.PHP
sometimeTakingFunction($conn,$msg)  
    
?>

The exec code i found which can run my function in background but i am confused where to put or how to use my function in exec

exec(sprintf("%s > %s 2>&1 & echo $! >> %s", $cmd, $outputfile, $pidfile));

System can also use to run in background but no clue how to use it.

system("php script2.php &");

sometimeTakingFunction i have needs to work in background in php. Can anyone guide me how to use my sometimeTakingFunction($param1,$param2); in background without blocking the code i have above it.

I see system and exec which can be helpful in running background php code. As a newbie in php i don't understand how to add my sometimeTakingFunction($param1,$param2,$param3) functions in these.

I found a link which shows different ways to run php in background but not as required. Anyone know how can i run my function in background. I am using ubuntu for my php.

13
  • 1
    It can not "block" the code that already executed before it. Your problem here is likely with flushing of the output buffer, stackoverflow.com/questions/3133209/… Commented May 23, 2022 at 11:57
  • @CBroe You are right. But the echo should send result back to front end first, because the function i need to run in background is long running function plus it add some delay in sending result back to front end user. Commented May 23, 2022 at 12:06
  • @Ritu Are you allowed to use a cron job for this? Commented May 23, 2022 at 12:31
  • @nice_dev No cron job because it's not regular event to call. Commented May 23, 2022 at 12:34
  • 1
    @nice_dev Thanks alot for your help. Mail.php just send mail with details in params and it doesn't show and output to user or create any JSON Commented May 23, 2022 at 13:57

1 Answer 1

0

You can split this into 2 different scripts.

  • First script does some operations, echoes the result and runs a terminal command for the second script with the necessary arguments.
  • Second script includes the mail file, gets the params from command line arguments using getopt, calls the sometimeTakingFunction and throws the output to the output file.

http://www.php.net/manual/en/function.exec.php

From the Notes Section

"If a program is started with this 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."

Script1.php:

<?php 

// some code
$response = array();    
            
// This Code Should Execute Without waiting for other code;
$response['status'] = true;
$response['msg'] = "Thank you  Welcome in SAAS Application. We will connect with you soon. :)";// Send response to webpage first then execute below code.   ;
echo json_encode($response);    

$cmd .= ' --username "your_user_name" --email "your_email" --subject "your subject"';        

exec(sprintf("%s > %s 2>&1 & echo $! >> %s", $cmd, $outputfile, $pidfile));

Script2.php:

<?php

include_once('mail.php');

$options = getopt("", ["username:","email:","subject:"]);

/* your code */

sometimeTakingFunction($options['username'], $options['email'], $options['subject']);
Sign up to request clarification or add additional context in comments.

11 Comments

Thank you so much. Let me try this and get back to you. Thanks again you made my day :)
@Ritu Sure. Hope it works as expected.
what's that ` $outputfile, $pidfile` and where to link script 2 in first script ? and can i put variable $msg in $cmd because it would be easy to combine all variable into single one and get that $msg variable in script 2 like this $msg = $yourName.','.$yoursubject.','.$yourEmail.'
@Ritu I picked that $outputfile etc from your first script assuming they are those declared variables. You need to call second script from first script in that exec command I wrote.You are free to change variables as you wish. I just presented you with the template of it.
Yes there were few issues also i faced like correct path, permission, single quote, < , running in background etc. But i really thankful to you for helping me.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.