61

I am getting a memory error in a php cron job:

Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 71 bytes) in /opt/matrix/core/lib/DAL/DAL.inc on line 830

The applicable parts of the crontab are:

$ sudo crontab -u www-data -l
MAILTO=root
# m h  dom mon dow   command
*/15 * * * * php /opt/matrix/core/cron/run.php /opt/matrix

I am running on Debian Squeeze, fully updated.

The obvious solution would be that the cli has a low memory limit (of 64MB). However, /etc/php5/cli/php.ini says it's unlimited.

$ cat /etc/php5/cli/php.ini | grep memory_limit
memory_limit = -1

I read somewhere that it could be different for different users, and since the process is running as www-data, i ran:

$ sudo -u www-data -s
$ php -i | grep memory_limit
memory_limit => -1 => -1
suhosin.memory_limit => 0 => 0

Even the apache/php.ini has a higher limit than the error is claiming:

$ sudo cat /etc/php5/apache2/php.ini | grep memory_limit
memory_limit = 128M

What am I missing? Where is this memory limit?

Ryan H
  • 1,398
  • 3
  • 14
  • 18

3 Answers3

127

IIRC, an unlimited memory_limit isn't supported by the CLI (I'll try to find a source for this) but for now, try passing it into the command:

php -d memory_limit=128M my_script.php

UPDATE

Apparently I was dreaming about the unlimited memory_limit not being supported for php cli. Regardless, it looks like the value from the ini is ignored. The simplest solution should then be to specifically set it in the php command calling the script.

UPDATE2

To answer the question of where the memory limit is coming from, it's most likely being set in the script itself using 'ini_set'.

Derek Downey
  • 3,765
  • 4
  • 25
  • 29
  • That conf is the debian default configuration, nothing special from me. Strangely, the comment above it say that it makes the memory limit 128 MB. – Ryan H Jul 29 '11 at 17:39
  • Hrm, the question of WHERE the value is coming from (which is the actual question apparently /fail reading comprehension) do you have any ini_set memory_limit in the code? – Derek Downey Jul 29 '11 at 17:47
  • run php --ini to print a list of the configuration files it is reading. In a FreeBSD configuration it will look for files in /usr/local/etc/php/*.ini after reading the main /usr/local/etc/php.ini – Allan Jude Jul 29 '11 at 21:11
  • it seems that the script is setting the memory internally to this value. Thanks for helping me rule out everything else. I really didn't want it to be that! Thanks for the -d tip. – Ryan H Aug 01 '11 at 16:04
  • 1
    The script sets its own memory limit internally to the application. It was set to 64 MB. Thanks for helping me find it. – Ryan H Aug 01 '11 at 18:26
  • Glad you found it! – Derek Downey Aug 01 '11 at 18:32
  • You can also use `php -dmemory_limit=-1 your_script.php` for unlimited memory usage – Aleksandr Ryabov Nov 02 '18 at 23:48
  • Do note that it is `M` not `MB`. Using `MB` does nothing at all. – zed Jan 28 '19 at 11:24
2

While testing a CLI php version 5.5.9 it appears that in cli it has unlmited memory limit by default, and specifying php -d memory_limit=4G my_script.php will set a limitation to that.

adrianTNT
  • 1,007
  • 5
  • 21
  • 41
  • 2
    That's not correct, CLI will just follow the memory_limit setting specified in the php.ini file – Tim Oct 31 '19 at 01:17
-4

If you install PHP as an Apache module (check 'Server API' in phpinfo()), you should calling it via command line web browser (wget, curl, lynx, ...) in cron job, something like this:

*/15 * * * * lynx -dump http://localhost/script.php >> /var/log/script.log 2>&1
quanta
  • 50,327
  • 19
  • 152
  • 213
  • That just wastes more memory on Apache. Both the SAPI and CLI are installed, so it is best to use the CLI in this case. – Allan Jude Jul 29 '11 at 21:10