When your PHP CLI script fails with a 64MB memory limit despite having memory_limit = -1 in php.ini, there are several potential causes to investigate:
$ php -i | grep memory_limit
$ sudo -u www-data php -i | grep memory_limit
$ sudo -u www-data php -r "echo ini_get('memory_limit');"
Suhosin Extension: Even though suhosin.memory_limit shows 0, it might still be enforcing limits in some configurations.
; Check for active restrictions
$ php -m | grep -i suhosin
PHP-CGI vs CLI: Some systems run cron jobs through PHP-CGI with different ini files:
$ locate php.ini | grep cgi
The script itself might contain memory limit directives:
// Check these potential locations in your code:
ini_set('memory_limit', '64M');
set_time_limit(0);
Here's how to override the limit at runtime:
#!/usr/bin/env php
<?php
ini_set('memory_limit', '256M');
// Your script logic here
Alternative cron entry approach:
*/15 * * * * php -d memory_limit=256M /path/to/script.php
To identify where the limit is actually being set:
$ strace -eopen php your_script.php 2>&1 | grep php.ini
$ php --ini
$ sudo -u www-data php --ini
PHP loads multiple ini files in order. Check which settings take precedence:
$ php -i | grep "Loaded Configuration File"
$ php -i | grep "Additional .ini files parsed"
When your PHP CLI script fails with a 64MB memory limit error despite having memory_limit = -1
in php.ini, several potential culprits could be at play. Let's examine the most common scenarios:
// Example of checking current memory limit programmatically
echo "Current memory limit: " . ini_get('memory_limit') . "\n";
Your output shows Suhosin is installed, which often enforces its own memory limits:
$ php -i | grep suhosin
suhosin.memory_limit => 0 => 0
Despite showing 0 (unlimited), Suhosin might still impose restrictions. Try temporarily disabling it:
// In your CLI script
if (extension_loaded('suhosin')) {
ini_set('suhosin.memory_limit', -1);
}
Cron jobs often run with minimal environment variables. The php.ini being loaded might not be what you expect:
// Add this to your cron script to debug
echo "Loaded php.ini: " . php_ini_loaded_file() . "\n";
echo "Additional ini files: " . implode("\n", php_ini_scanned_files()) . "\n";
Some systems have per-user PHP configurations that override system defaults:
$ ls -la ~www-data/.php/ # Check for user-specific config
$ sudo -u www-data php --ini # Show actual loaded configuration
The application itself might enforce memory limits:
// Check if the script contains memory_limit settings
grep -r "memory_limit" /opt/matrix/core/
If you can't find the source of the limit, you can work around it:
#!/usr/bin/env php
<?php
ini_set('memory_limit', '256M');
// Rest of your cron script
Or modify your crontab to explicitly set the limit:
*/15 * * * * php -d memory_limit=256M /opt/matrix/core/cron/run.php /opt/matrix
Check for system-wide limits that might affect PHP:
$ ulimit -a
$ grep -r "memory" /etc/security/limits.d/