<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>MV Associati Tech Gems &#187; timezone</title>
	<atom:link href="http://www.mvassociati.it/en/gems/topic/timezone/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mvassociati.it/en/gems</link>
	<description>Technical Article from MV Associati experience</description>
	<lastBuildDate>Fri, 04 Aug 2017 10:31:17 +0000</lastBuildDate>
	<language>en-EN</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4.2</generator>
		<item>
		<title>PHP error: file size limit exceeded</title>
		<link>http://www.mvassociati.it/en/gems/devops/php-error-file-size-limit-exceeded/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=php-error-file-size-limit-exceeded</link>
		<comments>http://www.mvassociati.it/en/gems/devops/php-error-file-size-limit-exceeded/#comments</comments>
		<pubDate>Fri, 04 Jan 2013 14:12:26 +0000</pubDate>
		<dc:creator>maraspin</dc:creator>
				<category><![CDATA[DevOps]]></category>
		<category><![CDATA[file size]]></category>
		<category><![CDATA[include]]></category>
		<category><![CDATA[issue]]></category>
		<category><![CDATA[logging]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[php.ini]]></category>
		<category><![CDATA[settings]]></category>
		<category><![CDATA[timezone]]></category>

		<guid isPermaLink="false">http://www.mvassociati.it/en/gems/?p=454</guid>
		<description><![CDATA[Troubleshooting PHP interpreter issues can be tricky. A few days ago a client reported that one of his cron job scripts wasn&#8217;t being executed on his x86_64 CentOS 5.8 server, running Zend Server CE PHP 5.3.14. Troubleshooting such situations is usually straightforward, but this time PHP error messages I got were so ambiguous, and issues [...]]]></description>
			<content:encoded><![CDATA[<p>Troubleshooting PHP interpreter issues can be tricky. A few days ago a client reported that one of his cron job scripts wasn&#8217;t being executed on his x86_64 CentOS 5.8 server, running Zend Server CE PHP 5.3.14. Troubleshooting such situations is usually straightforward, but this time PHP error messages I got were so ambiguous, and issues were so many at the same time, that I decided to write a blog post, hoping that it can be of some help for someone else.<span id="more-454"></span></p>
<h3>The Problem</h3>
<p>Script wasn&#8217;t outputting nor logging anything, so in order to find out more about the problem, I thought about logging last script execution datetime myself. I wanted to find out whether the problem was related to cron execution, or rather to some issue within the script itself. So I added following two lines (on top to the script):</p><pre class="crayon-plain-tag">$s_content = 'Script last run: '. date(&quot;d/m/Y h:i:s&quot; . &quot;\n&quot;);
file_put_contents('/tmp/cert_import.log', $s_content);</pre><p>And executed it:</p><pre class="crayon-plain-tag">php ./importa_certificati.php</pre><p>All I got was a nasty error message:</p><pre class="crayon-plain-tag">File size limit exceeded</pre><p>At first I thought the problem might&#8217;ve been related to the specific file name I had picked (IE /tmp/cert_import.log). Did it exist already and was it already too big? I was sure I had checked things out. And actually, file wasn&#8217;t there upon a second check. So what was PHP ranting about? Could have it been a permission issue? Nope, script was being executed by root and was executable. So, where did the problem lie? I started to search for the problem by reverting the script to its original form, commenting out my additions:</p><pre class="crayon-plain-tag">// $s_content = 'Script last run: '. date(&quot;d/m/Y h:i:s&quot; . &quot;\n&quot;);
// file_put_contents('/tmp/cert_import.log', $s_content);</pre><p>Problem magically disappeared. Weird, I thought. &#8220;Let&#8217;s see what happens if I prepare my string, but don&#8217;t it write to the file, I said to myself. I&#8217;m sure the problem lies there. Let&#8217;s check&#8221;:</p><pre class="crayon-plain-tag">$s_content = 'Script last run: '. date(&quot;d/m/Y h:i:s&quot; . &quot;\n&quot;);
// file_put_contents('/tmp/cert_import.log', $s_content);</pre><p></p><pre class="crayon-plain-tag">File size limit exceeded</pre><p>Whaat?!? Problem still there, despite no attempt to write to a file is even made. Things start to look awkward.</p>
<h3>The Cause</h3>
<p>What I had just discovered was that problem didn&#8217;t have anything to do with the file I was trying to write, but was related instead to the PHP process itself. In fact, wondering what other file size limit might&#8217;ve been reached, I came to think about logging. So, I opened php.ini and checked things out. Logging was configured indeed, pointing at /usr/local/zend/var/log/php.log through following directives:</p><pre class="crayon-plain-tag">; http://php.net/log-errors
log_errors = On

; Set maximum length of log_errors. [...]
log_errors_max_len = 1024

; Log errors to specified file. PHPs default behavior is to leave this value
; empty.
error_log = &quot;/usr/local/zend/var/log/php.log&quot;</pre><p>I changed my working directory to /usr/local/zend/var/log/ and checked things out with ls:</p><pre class="crayon-plain-tag">[root@XXX ~]# cd /usr/local/zend/var/log/
[root@XXX log]# ls -alh
-rw-rw---- 1 apache zend 2.0G Dec 27 17:30 php.log</pre><p>That exact 2Gb size looked suspicious, to say the least. Especially because the php.ini log_errors_max_len directive above had a 1024 value, not a 2048 one. So, I moved the log file out of its way, and gzipped it (so to save some space, without losing anything).</p><pre class="crayon-plain-tag">mv php.log
gzip php.log</pre><p>I then run the script again. All went well this time, with no error being reported. Problem was caused by log file size indeed. Most likely no one cared about rotating it, believing that log_errors_max_len would do the job. As it&#8217;s explained on <a title="PHP log_errors_max_len" href="http://stackoverflow.com/questions/1966540/log-errors-max-len-1024-in-php-ini-but-php-log-keeps-growing">stackoverflow</a> though, despite its possibly ambiguous name, log_errors_max_len only deals with the size of a single log message, not with the size of the log file itself.</p>
<p></p>
<p>Newly introduced problem was solved. But I was still wondering what might&#8217;ve caused the problem from the lines I had added in first place. Luckily I now had logs to check.</p>
<h3>The timezone not set and include underlying issues</h3>
<p>I opened the new php.log file and found a few entries like the following:</p><pre class="crayon-plain-tag">[05-Dec-2012 23:00:04 UTC] PHP Warning:  strtotime(): It is not safe to rely on the system's timezone settings. 
You are *required* to use the date.timezone setting or the date_default_timezone_set() function. 
In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. 
We selected 'Europe/Berlin' for 'CET/1.0/no DST' instead in /[...]/importa_certificati.php on line 14</pre><p>Easy problem to fix. And also found the reason why log file filled up so quickly. Such message was written for every run of another script where the date function was used.</p>
<p></p>
<p>Still, I hadn&#8217;t solved my problem yet. Script would run from console, but wouldn&#8217;t from cron. Again, logs proved to be my friends, with entries like the following:</p><pre class="crayon-plain-tag">[27-Dec-2012 23:09:02 UTC] PHP Warning:  include_once(../classi/dbconnect.php): failed to open stream: No such file or directory in /[...]/importa_certificati.php on line 3
[27-Dec-2012 23:09:02 UTC] PHP Warning:  include_once(): Failed opening '../classi/dbconnect.php' for inclusion (include_path='.:/[...]') in /[...]/importa_certificati.php on line 3
[27-Dec-2012 23:09:02 UTC] PHP Warning:  include_once(../classi/utils.php): failed to open stream: No such file or directory in /[...]/importa_certificati.php on line 4
[27-Dec-2012 23:09:02 UTC] PHP Warning:  include_once(): Failed opening '../classi/utils.php' for inclusion (include_path='.:[...]') in /[...]/importa_certificati.php on line 4
[27-Dec-2012 23:09:02 UTC] PHP Fatal error:  Class 'utils' not found in /[...]/importa_certificati.php on line 10</pre><p>I had just found out that problem was due to the script having relative includes in the following form:</p><pre class="crayon-plain-tag">&lt;!--?php

include_once('../classes/dbconnect.php');
include_once('../classes/utils.php');

$s_pathCerts = __DIR__ . '/../rsync/data';

&lt;/pre--&gt;</pre><p>So everything worked when the script was tested by customer from local directory:</p><pre class="crayon-plain-tag">php ./importa_certificati.php</pre><p>But once it was invoked by cron, it stopped working, since path wasn&#8217;t quite the same.<br />
And, because no error messages related to the issue were collected in php.log, no one ever discovered about the problem with the script, and blamed cron settings instead.<br />
Changing lines above tothe following solved the problem:</p><pre class="crayon-plain-tag">&lt;!--?php

include_once(__DIR__ . '/../classes/dbconnect.php');
include_once(__DIR__ . '/../classes/utils.php');

$s_pathCerts = __DIR__ . '/../rsync/data';

&lt;/pre--&gt;</pre><p>&nbsp;</p>
<h3>Things to take home from this experience</h3>
<ol>
<li>If php can not write to its log file it will stop script execution, without giving a clear explanation of what&#8217;s going on</li>
<li>The log_errors_max_len directive is a bit ambiguous. It deals with each log record, not with the whole file size</li>
<li>You always need to set your timezone, if you don&#8217;t want to have your logs filled with crap</li>
<li>When including stuff, it&#8217;s wise to rely on the php __DIR__ or dirname(__FILE__) constructs rather than on relative paths. You can&#8217;t be sure the scritpt will be invoked from where you expect.</li>
<li>When troubleshooting it&#8217;s important to think &#8220;outside&#8221; the box. In this case the problem was related to the script itself, as much as it was related to its environment.</li>
<li>If you do write to logs, you need to check&#8217;em out every once in a while. Having their content e-mailed to you periodically is also a good idea</li>
</ol>
<p>Elephant picture courtesy of http://www.flickr.com/photos/laughingsquid/2218075860/</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mvassociati.it/en/gems/devops/php-error-file-size-limit-exceeded/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
