| PHP CLI: Simple Process Control in Linux with PCNTL_EXEC |
|
|
|
| Development - PHP |
| Written by Rick |
| Friday, 15 May 2009 00:00 |
|
I've been working on a project using php executed from the command line. The project downloads files via FTP from several sources, renames the files and tosses the files into a new directory structure. I never thought I'd find a legitmate reason to create a "file scrounger". Throughout development I've been executing the script from the command line to test and debug. Happily building the app step by step. Once it was complete I thought I'd just be able to execute it from a cron job on my Linux box and be done with it. Boy, was I wrong!!! Unbeknownst to me, php run from the command line in a Linux environment from cron spawns processes like salmon produce eggs. It bogged-down the machine causing problems as you might imagine. The obvious question arose, "How do I stop that?". Process Control, of course. I can say, "of course" now because I've been "down that road". My first thought was OMG. I wondered how many more lines of code I needed to accomplish the task. After days of research and testing it came down to a very simple script using PCNTL_EXEC that calls a class. The file I was trying to execute is named retrieve.php (yeah, I know not very original). To run the app in a single process, I created another file name start.php. Inside start.php, not including comments, there are 13 lines of code. It looks like this;
The class;
<?php
That's it!. The program runs with 2 processes. 1 shell plus retrieve.php in it's own process. Now I can sleep at night!!!
|




