homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Gold Sponsor 2015!
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

get php processes by $ SERVER[UNIQUE ID]?

5+ Year Member

Msg#: 4077259 posted 10:33 pm on Feb 9, 2010 (gmt 0)

I would like to be able to run a script once in the background that will take quite some time to complete. I only want to ever run it once, so I would like to grab the process's UNIQUE_ID so i can test whether it is still running or not. This script will be updating a database, so i can watch the progress of it. but i'll never know for sure if it is still running or has error-ed or timed out. If it did error out and didn't finish updating the database i want to restart it. I would also want to be able to accomplish this from any browser on any PC. so using session id's wouldn't work.




WebmasterWorld Senior Member 10+ Year Member

Msg#: 4077259 posted 8:31 am on Feb 10, 2010 (gmt 0)

Have it do it's work, and then update a flag in a database field after it finishes.

If the flag doesn't get turned on, the script timed out, run it again. If you won't have access to the database directly, write a second script to query the flag, and rerun the first script if necessary.

Although, you should probably consider rewriting the original script so that it doesn't have such a high chance of not finishing?

You mention you are only going to run it once, but then say you want to be able to run it from any browser, so it sounds like you'll be running it periodically? If so you don't want to run something that will monopolize hard drive bandwidth and cause web requests to stack up very often, or eventually it's going to coincide with high traffic and crash the server.

One solution might be to break the work up into discrete chunks and run it at intervals using a cron job.


5+ Year Member

Msg#: 4077259 posted 3:29 pm on Feb 10, 2010 (gmt 0)

I probably could have explained this better... the script will do two things. One, the exe part that will do the work updating the database. Two, check its own progress and if the script failed out, restart itself. I only want to run the exe part once. then be able to check the progress from anywhere.

I'm pretty confident that i can write the script so that it doesn't error out, i'm more worried about the server being restarted or something like that.

The script is going to send me an email when its done running so i'll know when its done. but if its not done, the script could never know if its not done because it was killed early or if its simply not done running. Thats the crux of my issue.


WebmasterWorld Senior Member rocknbil us a WebmasterWorld Top Contributor of All Time 10+ Year Member

Msg#: 4077259 posted 9:08 pm on Feb 10, 2010 (gmt 0)

I've written these before in Perl, in different contexts and for another purpose. I'll lay out the rough logic, should be easy to duplicate in PHP.

You would use fork (or if installed, pcntl_fork) to fork a child process. The process id of the child is what is returned by fork. This is where you do your time intensive process. Perl-ese, PHP is similar:

if ($pid = fork()) {
// child process
else {

So the parent now has a process ID as a "Handle" on the running process. The parent outputs an iframe that has a src to a second script; you pass the $pid to this second script (or, call a function in the same script, doesn't matter.)

There are easier methods, but I just do a system exec on px aux. This returns a set of rows in an array of all current processes. We're only looking for THIS process. So,

$status = system_exec('ps aux');
$lines = preg_split('/\n+\r*/',$status);
foreach ($lines as $line) {
$line = preg_replace('/\s+/',' ',$line); // tabs, multiple spaces for single spaces
list($user,$process_id,$cpu,$mem,$vsz,$rss,$tty,$stat,$start,$time,$cmd) = preg_split('/\s+/',$line);
$cmd is an ARRAY
if ($process_id eq $current_p) {
$this_status = preg_match("/$this_script/",$cmd[count($cmd-1)])?'Processing ok':$cmd[count($cmd-1)];

Some stuff may be lost in the PHP translation, but that's the gist. The code of the running process will exist in the last member of the $cmd array. As long as the process exists, it will have a result code to return. When it's gone, it has no value to return.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved