Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

running processes in background php

Tags:

php

cron

I have few cronjob that summarize data and validate data for my site. Some of them have processes that needs to be run in background.

Example:

cronjob1.php execute cronjob2.php using exec

This cronjob2.php runs another cronjob3.php using exec and cronjob3 needs to be completed then cronjob2 and then cronjob finish.

I currently have an issue where the cronjob1.php takes 2 hours to finish.

is there a better way to run this so it run faster?

Thank You

like image 456
Gilbert Kakaz Avatar asked Dec 22 '11 14:12

Gilbert Kakaz


People also ask

How can I run a PHP script in the background after a form is submitted?

You can put a task (such as command or script) in a background by appending a & at the end of the command line. The & operator puts command in the background and free up your terminal. The command which runs in background is called a job. You can type other command while background command is running.

What is background process example?

A background process is a computer process that runs behind the scenes (i.e., in the background) and without user intervention. Typical tasks for these processes include logging, system monitoring, scheduling, and user notification.

How do you end a PHP process?

The exit() function in PHP is an inbuilt function which is used to output a message and terminate the current script. The exit() function only terminates the execution of the script. The shutdown functions and object destructors will always be executed even if exit() function is called.


1 Answers

There is few things that you can do:

  • make sure that your script use permanent connection, this way you won't loose time connecting and disconnecting from the database server.
  • implement a logging mechanism, so you can identify which part of the script run slowly, logging the time spent on each database queries would be a good idea
  • try to optimize your database as much as possible, you should use explain on slow queries and create the needed indexes.
like image 75
RageZ Avatar answered Oct 29 '22 08:10

RageZ