0

This might be a very stupid question so please bear with me.

I have a a php script that makes API calls to Shopify.

The entire point of this php script is to print out statements for each customer.

Now it has to run through about 200 customers. This entire process takes about 15 minutes.

Ordinarily this runs on a monthly basis with a cron job.

But I need to be able to run it manually as well. I just want the page to execute and do everything in the background with my browser or internet connection playing NO role as to whether the complete execution completes.

The cron job runs header_php.php?run=monthly

Is there anyway I can run it manually, make sure it gets a 200 response from the page, and then close my browser tab and ensure that apache does the rest?

I would be executing it via an AJAX call as well.

Another thing, once each statement is done being processed, the script outputs it to pdf and emails it to the customer. So there's no feedback required from the page when it runs.

StuyvesantBlue
  • 135
  • 1
  • 15
  • 200 customers taking 15 minutes? I know a granny who can do that faster. Oh, she doesn't have any arms and legs. Oh, btw. Proof (show code) or it didn't happen. – Xorifelse Mar 03 '17 at 23:04
  • 2
    Your granny takes on 200 customers quicker than 15 minutes? Explains your constructive response. – StuyvesantBlue Mar 03 '17 at 23:06
  • Yes, a granny. Because `mysql` should be easily capable of handing a million rows of data without breaking a sweat. Explain your constructive question with code, *now*. – Xorifelse Mar 03 '17 at 23:09
  • Another solution is, to run the cron every minute, and have a schedule MySQL table... – Dan Mar 03 '17 at 23:13
  • as @Xorifelse said, you should probably focus on optimization. Alternatively, you can implement task runner/monitor. http://stackoverflow.com/questions/42512692/how-to-check-if-there-is-a-there-is-a-wget-instance-running/42513196#42513196 – Dimi Mar 03 '17 at 23:13
  • You probably shouldn't run this through a web browser. Consider implementing a job system instead, i.e. beanstalkd. Your cron job and web process would queue jobs in here instead. – Jonathan Mar 03 '17 at 23:17
  • I think this is a legit question. +1. – The Onin Mar 03 '17 at 23:22
  • @NinoŠkopac Sorry, without proof nothing happened. – Xorifelse Mar 03 '17 at 23:25

3 Answers3

1

Easily doable with simple HTTP headers.

  1. Start output buffering
  2. Output the response, if any
  3. Send Content-length and Connection: close headers
  4. Flush and end output buffers
  5. The browser receives HTTP response
  6. Continue time consuming processing

This SO anwer nails it (the comments are helpful as well).

Community
  • 1
  • 1
The Onin
  • 5,068
  • 2
  • 38
  • 55
-1

on linux

<?php 
exec(''.$command.' > /dev/null 2>&1 &');
?>

on windows

<?php
  $shell = new COM("WScript.Shell");
  $shell->run($command, 0, false); 
?>

where command would be something like

php -f $path/to/filename

if you put that in a page, you can then call it whenever you want and it will spawn a thread that will call apache, but not require the browser to wait for any response.

  • Is the tag [tag:shell] apart of the question? No. It doesn't answer it, nothing can without proof or code. I suggest deletion because you will get downvotes. – Xorifelse Mar 03 '17 at 23:12
  • what are you talking about? what is "tag shell"? He is asking how to run a script that is independent of any browser issues or network issues, and this will do exactly that – guesterator_php Mar 03 '17 at 23:16
  • Apparently you are unaware that @NinoŠkopac is already using cron jobs. This happens in a separate thread already. So this code will not fix it, it will show the same results. And btw, a cron job is Linux related. – Xorifelse Mar 03 '17 at 23:24
  • he says he want to call it from the browser and not cron, and wants to ensure that it completes regardless of network issues, or browser issues .. this code does exactly that ... i don't understand how you cant see that or what issue you are trying to point out. – guesterator_php Mar 03 '17 at 23:27
  • @guesterator_php For cross-OS interoperability, I'd suggest using `Symfony/Process` alongside with `Symfony/Console` instead. – The Onin Mar 03 '17 at 23:28
  • This is generic PHP, if you leave the page while the page is still loading it will still finish executing it if you dont use `die()` or `exit()`. If this is useful, no because in those 15 minutes you still cannot depend on that data. Which is BTW a very, very, very, very, very long time. The prime issue of his question. – Xorifelse Mar 03 '17 at 23:29
  • @guesterator_php This looks promising. I asked a buddy of mine before asking the question here and he suggested the use of exec(). I'm going to give it a whirl. – StuyvesantBlue Mar 03 '17 at 23:29
  • @Xorifelse, you are wrong on every account of what you are saying . He is not asking how to make the data work faster, he is asking how to make the script load even if he closes the browser ... This code does exactly that ... whether or not his code that he runs with this method is good or not is not the point ... – guesterator_php Mar 03 '17 at 23:57
  • @guesterator_php Didn't I state the page will execute unless you're using `die()` or `exit()`. regardless, in a way you are correct in questioning this. However the question in 'question' is not abiding by the community guidelines by reading [ask] and add a [mcve] so that we can take a look on what the real issue is. This is to broad to answer because 1 question should have a couple of specific resolutions. – Xorifelse Mar 04 '17 at 00:11
-1

You can call your script with other script that runs in background the job.

shell_exec('nohup /usr/bin/php /dir/to/your/script.php > /dev/null 2>/dev/null &');

Then you don't need to wait to finish the job