Configuration file (php.ini) when running Cron (cli) changing parameters (memory_limit) on hosting

If you encounter restrictions in the console when running a php script or with a restriction when setting a php task on cron, you will most likely look for the ability to change the settings of the php.ini file with your host. The probability that such settings will be in the hosting panel is very low.

Task:

Let's say you need to run a script that is in the path /var/www/script.php and it needs 1 GB of RAM to process some information. It is not possible to launch it from the browser due to long processing or you need to put the script in the scheduler.

cron (cli):

php /var/www/script.php

When running the script, we have:

Fatal error: Allowed memory size of 25165824 bytes exhausted (tried to allocate 20480 bytes) in /var/www/script.php on line NUMBER_LINE

Solution:

If the hosting allows, then the cli or cron task can be run with its own configurations.

1. Let's create a php.ini file (for example, in the /var/www/ directory next to our script.php)

2. Let's add the parameters we need to it (in this case, 1 GB memory_limit)

cli php ini

3. Run from console or put on cron

php -c /var/www/php.ini -f /var/www/script.php

-c - key after which the path to php.ini with our parameters is set

-f - key after which the path to the script to be run is set

Tested on hosting: 100% supported on hosting ukraine.com.ua

1060 0

Comments

Be the first to review this item. Share your rating and review so that other customers can decide if this is the right item for them.
You have to log in to leave a comment. Sign in

Similar articles