<?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>lemmingzshadow.net &#187; Prozesse</title>
	<atom:link href="http://lemmingzshadow.net/tag/prozesse/feed/" rel="self" type="application/rss+xml" />
	<link>http://lemmingzshadow.net</link>
	<description>a webdevel&#039;s weblog</description>
	<lastBuildDate>Fri, 13 Aug 2010 09:10:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>PHP-Scripte im Hintergrund ausführen</title>
		<link>http://lemmingzshadow.net/6/php-scripte-im-hintergrund-ausfuhren/</link>
		<comments>http://lemmingzshadow.net/6/php-scripte-im-hintergrund-ausfuhren/#comments</comments>
		<pubDate>Tue, 20 May 2008 10:38:43 +0000</pubDate>
		<dc:creator>Lemmingz Shadow</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Prozesse]]></category>

		<guid isPermaLink="false">http://lemmingzshadow.net/?p=6</guid>
		<description><![CDATA[Gelegentlich kommt es vor, dass man ein PHP-Script aus einem anderen heraus aufrufen möchte. Das ist zunächst kein Problem. Etwas trickreicher wird es jedoch wenn das Script welches aufgerufen wird eine sehr lange Laufzeit hat. Es ergibt sich folgendes Problem: Entweder Script A ruft Script B auf, muss nun warten bis Script B beendet ist, [...]]]></description>
			<content:encoded><![CDATA[<p>Gelegentlich kommt es vor, dass man ein PHP-Script aus einem anderen heraus aufrufen möchte. Das ist zunächst kein Problem. Etwas trickreicher wird es jedoch wenn das Script welches aufgerufen wird eine sehr lange Laufzeit hat. Es ergibt sich folgendes Problem: Entweder Script A ruft Script B auf, muss nun warten bis Script B beendet ist, und arbeitet dann den nächsten Befehl ab. Ein typisches Beispiel wäre:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'http://url_zu_script_b.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'ich muss auf script b warten...'</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><span id="more-6"></span><br />
Eine zweite Möglichkeit ist Script B aufzurufen ohne das Ergebnis abzuwarten und in Script A direkt mit dem nächsten Befehl weiter zu machen. Das könnte z.B. so aussehen:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$fp</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fsockopen</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'www.host_b.com'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">80</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span> <span style="color: #339933;">!==</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$out</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;GET /path/to/script_b.php HTTP/1.1<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$out</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;Host: www.host_b.com<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$out</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;Connection: Close<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #990000;">fwrite</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span> <span style="color: #000088;">$out</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'ich muss nicht auf script b warten...'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Der Nachteil im ersten Fall ist klar. Das aufrufende Script A muss immer warten bis Script B abgerbeitet ist. Bei einer langen Laufzeit von Script B kann es hier schnell zu Timeouts kommen. Der Nachteil von Variante 2 ist weniger offensichtlich und daher schnell Quelle von Fehlern. Sobald das aufrufende Script beendet ist, wird auch der entsprechende Prozess auf dem Server beendet und somit nach kurzer Zeit auch das aufgerufene Script B. Wenn also die Laufzeit von Script B wesentlich länger ist als die des aufrufenden Scripts A kommt es bei dieser Lösung ebenfalls zu Problemen.</p>
<p>Um das Problem zu lösen habe ich nun folgenden Weg gewählt. Das Script B wird von Script A nicht direkt aufgerufen, sondern es wird mittels exec und curl ein neuer Prozess auf dem Server gestartet welcher Script B aufruft.</p>
<p>Auf einem Linux Server kann das dann z.B. so aussehen:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$cmd</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'curl -s &quot;http://url_to/script_b.php&quot;'</span><span style="color: #339933;">;</span>
<span style="color: #990000;">exec</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cmd</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; &gt; /dev/null &amp;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>&#8220;> /dev/null&#8221; leitet hierbei sämtliche Ausgaben direkt ins Nirvana und das &#038; sorgt dafür dass der Prozess im Hintergrund läuft.</p>
<p>Auf einem Windows-System könnte das Ganze in etwas so aussehen:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$cmd</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'start /B C:\Programme\curl\curl.exe -s &quot;http://url_to/script_b.php&quot;'</span><span style="color: #339933;">;</span>
<span style="color: #990000;">pclose</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">popen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cmd</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;r&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><strong>Hinweis zum Schluss:</strong> Die beispielhaften Codeschnipsel dienen lediglich der Erklärung. Sie sind nicht schön und auch <strong>nicht sicherheitstechnisch optimal</strong>, also bitte nicht eins zu eins übernehmen. </p>
]]></content:encoded>
			<wfw:commentRss>http://lemmingzshadow.net/6/php-scripte-im-hintergrund-ausfuhren/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
