Avatar of mamuscia
mamuscia
Flag for United States of America asked on

PHP Start Background Scripts in either Linux or Windows without waiting for completion

How do I start a PHP script to run in the background so my foreground process in a web browser doesn't freeze waiting for it to end.

Secnario:
User uploads a file
After saving the file, I kick off a background PHP script to process it
When the background script ends, it sends the user an email to notify them.

I've seen similar questions, but no answers or sample code to make these calls.
ProgrammingPHP

Avatar of undefined
Last Comment
ahoffmann

8/22/2022 - Mon
ahoffmann

on *ix it's as simple as
  exec("whatever &");
( also possible system(), passthru(), shell_exec() ...)
or more elegant using pcntl_fork(), see http://php.net/manual/en/function.pcntl-fork.php
or probably simply using fork()

on windows ... have fun :-/  (example never tested):
pclose(popen("start whatever", "r"));
ASKER CERTIFIED SOLUTION
Ray Paseur

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
mamuscia

ASKER
Ray,
Not sure how this is working, but it does. My calling script continues without holding up the user and the script I call with CURL completes.  I know this is not asynchronous really, but it is doing what I want in both Windows and Linux environments.  Thanks for the technique.
Ray Paseur

What was wrong with the answer?
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
mamuscia

ASKER
Nothing wrong per say, but it's not really starting a parallel asynch task as I wanted. It works, but that's because we don't care what the output is and don't do anything if we don't get it.
Ray Paseur

OK, then tell me what this means:  this is not asynchronous really.  Perhaps you and I have different definitions of "asynchronous."

As I understand the issue, your file upload completes very fast, but the processing of the file takes a very long time, so much so, that the client is made to wait much longer for the processing than for the upload.  So you want to decouple the upload from the long, slow processing step.  In that sense it's not a description of an asynchronous task - it's a description of a sequential process involving two tasks.  There is nothing at all standing in the way of sending an email from the second script when it is completed.  You would want to be very careful about debugging this script because the client cannot see any browser output, but it's certainly not a case of "don't care."  In MY application, I don't care.  But that is not the same as saying the process is flawed in some way.
mamuscia

ASKER
Well, you are correct. I apologize if it sounded like I didn't think the solution was good.  I do and I'm using it.  I understand what you're saying about sequential versus asynch processing. I just really wanted to understand how I start a PHP script from either LAMP or WAMP environment by calling PHP and passing it the script to run.  Kind of like we run CRON jobs. It starts a PHP instance and passes it the script name.  It runs completely standalone from any other script.  From what I understand of your solution, it essentially does the same thing and won't tie anything to the users login session, so that's really the equivalent of what I'm after.  Thanks for your knowledge, I've been trying all kinds of things to get it to work in Windows to no avail. The solution hoffmann gave about works for Linux.  Your solution works in both.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Ray Paseur

OK, good.  I thought maybe something was wrong because of the marked-down grade.  Glad it's OK, ~Ray
ahoffmann

ok, it works, but not as you asked for (and not as a computer should do it, IMHO;-)