If you like DNray Forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...

 

Execution of potentially long php script running via cron

Started by haimen, Feb 14, 2023, 03:07 AM

Previous topic - Next topic

haimenTopic starter

Although I am not proficient in php programming, I need to run a potentially lengthy php script on virtual web hosting to update a [potentially] large number of b/d records. To accomplish it, I intend to establish a cron task. However, I have a concern: Are there no limitations on execution time and memory for scripts that run using cron-type pieces (apart from the considerable physical limits on memory and processor time for my tariff)?

 In case the script functions sluggishly in the background, is there any assurance that it will complete its task correctly and not collapse silently after running for, say, five minutes? Additionally, am I following the correct approach by pairing php with a cron job for the aforementioned chore?
  •  

ella.boswell

It's impossible to give a positive answer to this odd question. While there may be a guarantee for the departure time, other reasons for departure - from script errors to server restarts - are still a risk. In some cases, the script may run for so long that a second copy is launched by the crown, compromising the first copy's results.

One solution for ensuring continuous service operation is to create a custom daemon, such as a shell script that runs a command in a loop. However, it ultimately depends on the specific task at hand - which, incidentally, is not mentioned in the original question.
  •  

DavidBloom

When launching the PHP interpreter, two options are typically used: from the HTTP server and from the command line (e.g. from a cron job). These methods usually use different configuration files (php.ini), potentially resulting in varying interpreter configurations.

To view these configurations, you can utilize the phpinfo() PHP function. Note that the path to the php.ini file used will be featured in the output.

For an HTTP server call: create a PHP file in the site's docroot directory with a custom name and .php suffix (for example, info.php) containing the following code:

    <?php phpinfo();

Access this file from an HTTP browser by going to http://your.site/info.php.

For a command line call: use the following code:

    $ php -r 'phpinfo();'

Or even shorter:

    $ php -i

When comparing configurations, pay particular attention to the max_execution_time and memory_limit parameters.

If any of the configurations aren't satisfactory and you're starting from the command line, you can override them with the -d option:

$ php -d 'option=value'

Alternatively, create a new php.ini file with desired parameters and specify its use:

$ php -c /path/to/your/php.ini
  •  

perelman

Updating a database through a script that may crash afterwards can be an unreliable task. It is better to manually update the database in situations such as when there are users present who need to add or delete information. If there are complex or sensitive tasks, it is best to do them manually rather than relying on a script that could stop working at any time.
  •  

semidot

Regarding limitations on execution time and memory, the usual restrictions for running PHP scripts apply, just as they would for any other method of execution. Your virtual web hosting service may have its own set of limitations on memory and processor time, which could impact the script's performance. However, as you mentioned, these are typically tied to the specific tariff you've chosen.

In terms of the script's performance, it's important to address the potential for sluggish behavior when running in the background. While PHP scripts executed via cron do not have direct user interaction and can thus run for longer periods, it's crucial to ensure that the script is optimized for efficiency. This includes employing best practices for database operations, utilizing caching mechanisms where appropriate, and handling errors gracefully to prevent silent failures.

To mitigate the risk of an incomplete or failed task, you can implement logging within the PHP script to track its progress and identify any issues that may arise during execution. This will provide insight into the script's behavior and help in troubleshooting any unexpected issues.

Now, as for the pairing of PHP with a cron job for this particular task, it's a common and valid approach for automating periodic tasks such as updating database records. However, it's essential to thoroughly test the script under conditions that simulate the production environment to ensure it behaves as expected and can handle the workload effectively.
While using a cron job to run a PHP script for updating database records is a suitable method, attention to optimization, error handling, and monitoring is key to ensuring reliable execution. By addressing these aspects, you can increase the likelihood of the script completing its task correctly and avoid potential pitfalls associated with long-running background processes.
  •  


If you like DNray forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...