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.

Ritu
  • 518
  • 2
  • 12
  • 35
  • 1
    It can not "block" the code that already executed before it. Your problem here is likely with flushing of the output buffer, https://stackoverflow.com/questions/3133209/how-to-flush-output-after-each-echo-call – CBroe May 23 '22 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. – Ritu May 23 '22 at 12:06
  • @Ritu Are you allowed to use a cron job for this? – nice_dev May 23 '22 at 12:31
  • @nice_dev No cron job because it's not regular event to call. – Ritu May 23 '22 at 12:34
  • Fair enough. Put that exec below your echo statement and make sure the output stream for that command is mentioned for it to continue running in the background even after the result is sent to the browser. https://stackoverflow.com/a/5103561/4964822 – nice_dev May 23 '22 at 13:03
  • @nice_dev you hit at the right point, That's my question also. I can use exec or system, but how to put my function `sometimeTakingFunction($param1,$param2)` with parameters in `exec` or `system` to run in background. Please help me out – Ritu May 23 '22 at 13:36
  • Put that function in another PHP file and call that script via command line. Pass arguments from your command line to this new PHP file and in that use this below function to get all arguments passed for execution. https://www.php.net/manual/en/function.getopt.php – nice_dev May 23 '22 at 13:42
  • @nice_dev In code above i include the `mail.php` which has my function `sometimeTakingFunction($param1,$param2)` . Can you show me code example by answering my question by taking reference of my code in question, i will be very thankful to you – Ritu May 23 '22 at 13:50
  • Ok, I am not at my desk right now but I shall try although I have never done this before. Can you let me know if mail.php plays any role in that json result you are echoing? – nice_dev May 23 '22 at 13:54
  • 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 – Ritu May 23 '22 at 13:57
  • @nice_dev any update? I also mentioned links in my question for the reference. – Ritu May 23 '22 at 23:06
  • Can you let me know whether there are any actual values of `$param1`, `$param2` that you wish to pass to the script? Or are they dynamic based on the current json result you are echoing? – nice_dev May 24 '22 at 03:48
  • @nice_dev it's contains email address user name and subject. – Ritu May 24 '22 at 09:57

1 Answers1

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']);
nice_dev
  • 17,053
  • 2
  • 21
  • 35
  • 1
    Thank you so much. Let me try this and get back to you. Thanks again you made my day :) – Ritu May 24 '22 at 11:53
  • @Ritu Sure. Hope it works as expected. – nice_dev May 24 '22 at 11:53
  • 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 May 24 '22 at 12:05
  • 1
    @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. – nice_dev May 24 '22 at 12:18
  • @it's not working but i will get back to you with the solution. I tried this code with different modifications but no luck yet. – Ritu May 25 '22 at 11:46
  • @Ritu I will replicate your issue sometime soon on my machine and see if it still persists. – nice_dev May 25 '22 at 12:06
  • please this this link https://stackoverflow.com/questions/72380236/php-running-background-with-exec-not-running-with-parameters i asked new question with more clear direction for exec. – Ritu May 25 '22 at 20:23
  • Thanks for your help. I solved it here is final code`exec ("/php /email.php $cmd $action > /dev/null &");` and getting in email.php with argv[x];. Please update your answer so i can accept it – Ritu May 25 '22 at 22:43
  • @Ritu Glad it worked but I presume you have made decent modifications. You can answer your question and accept :) – nice_dev May 26 '22 at 04:52
  • 1
    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. – Ritu May 26 '22 at 13:54
  • @Ritu Feel free to edit my answer of you wish to. – nice_dev May 26 '22 at 14:08