User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 423,460 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,807 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 3916 | Replies: 9
Reply
Join Date: Jun 2004
Posts: 36
Reputation: timhysniu is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
timhysniu timhysniu is offline Offline
Light Poster

Running PHP scripts in backround

  #1  
Jul 21st, 2006
Hi there,

I am wondering does anyone have any idea how to execute PHP scripts (a maintenance script in my case) in the background. The reason for this is because I don't want visitors to be slowed down by the maintenance execution, so having it run as a separate process would help a lot.

I have thought about running cron jobs but I want to enforce a maintenance if cron job stops working for some reason.

Any help or ideas would be greatly appreciated.

tim
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jan 2005
Location: Sheffield, UK
Posts: 294
Reputation: zippee is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 6
zippee's Avatar
zippee zippee is offline Offline
Posting Whiz in Training

Re: Running PHP scripts in backround

  #2  
Jul 22nd, 2006
PHP script always executed at the background, if not mistaken.

I run my maintenance routine everytime I log in to my admin area. It will not affect my clients' site and yet keep my database clean. I log in at least once a week and did not notice any delay or slow down. I think you can do the same.
Ecommerce-Web-Store.com Building Your e-Business.
Reply With Quote  
Join Date: May 2005
Posts: 19
Reputation: Rotak is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
Rotak Rotak is offline Offline
Newbie Poster

Re: Running PHP scripts in backround

  #3  
Jul 22nd, 2006
If you are working in a webserver you have full access to, you can also setup a cronjob!

But be aware, that your code should log errors and message to logfiles instead of the console.
Reply With Quote  
Join Date: Jun 2004
Posts: 36
Reputation: timhysniu is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
timhysniu timhysniu is offline Offline
Light Poster

Re: Running PHP scripts in backround

  #4  
Jul 24th, 2006
Thanks guys

I was just hoping of an idea to run a php script initiated by a visitor where a user wouldnt have to wait for the whole maintenance execution. I guess kind of like branching a single process into two. Thats something I havent done before; Im not sure if its possible.
Reply With Quote  
Join Date: Jul 2006
Location: England
Posts: 1
Reputation: sheepcentral is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
sheepcentral sheepcentral is offline Offline
Newbie Poster

Re: Running PHP scripts in backround

  #5  
Jul 24th, 2006
How about you have a cron job but you also have it so that it can be run from the browser or from the shell. Do you need it running all the time?
Reply With Quote  
Join Date: Jul 2006
Location: Remunj
Posts: 200
Reputation: pritaeas is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 24
pritaeas's Avatar
pritaeas pritaeas is offline Offline
Posting Whiz in Training

Re: Running PHP scripts in backround

  #6  
Jul 25th, 2006
Originally Posted by timhysniu
Thanks guys

I was just hoping of an idea to run a php script initiated by a visitor where a user wouldnt have to wait for the whole maintenance execution. I guess kind of like branching a single process into two. Thats something I havent done before; Im not sure if its possible.


Have a look at the PsExec tools (google it). With it you can run an external program, and choose to wait for it. This could do what you need.
"Premature optimization is the root of all evil."
Donald Knuth / Tony Hoare
Reply With Quote  
Join Date: Sep 2005
Posts: 689
Reputation: digital-ether has a spectacular aura about digital-ether has a spectacular aura about 
Rep Power: 6
Solved Threads: 41
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Practically a Master Poster

Help Re: Running PHP scripts in backround

  #7  
Jul 26th, 2006
Originally Posted by timhysniu
Thanks guys

I was just hoping of an idea to run a php script initiated by a visitor where a user wouldnt have to wait for the whole maintenance execution. I guess kind of like branching a single process into two. Thats something I havent done before; Im not sure if its possible.


Hi,

What you could do is use the exec() function in PHP that always to to execute shell commands. (Also see: popen, and proc_open() )

You can use the & option to run the command in the background.

Example:

[PHP]
<?php
exec( 'wget http://www.example.com/file.php > /dev/null &' );
?>
[/PHP]

The > /dev/null sends your php output to nowhere.
The & makes the wget run in the background.

You can use other commands rather than wget, but thats one Im sure of the syntax for.

If you cant use exec() then you could try using the HTTP protocol to your advantage to fork your php script for you.

I havent tested but I believe if you could do something like:

[PHP]
header( 'Location : http://www.example.com/new_file.php' );
echo str_pad(' ', 256); // make IE start rendering
flush();

// Do your other stuff here, in the background
// The browser is now fetching http://www.example.com/new_file.php
// Make sure you dont output anything

run_my_maintanence();

[/PHP]

I believe this will work.
For IE to actually start following the HTTP headers, it has to receive at least 256 bytes of content.. (now why?)

See if either of those work for you.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote  
Join Date: May 2004
Location: Boston,MA
Posts: 1,362
Reputation: mikeandike22 is an unknown quantity at this point 
Rep Power: 7
Solved Threads: 17
Featured Blogger
mikeandike22's Avatar
mikeandike22 mikeandike22 is offline Offline
Nearly a Posting Virtuoso

Re: Running PHP scripts in backround

  #8  
Jul 26th, 2006
PHP does always run in the background it is server side scripting. something like javascript which is client side would run in the foreground.
My Daniweb Blog: This,That, and Everything Else (Blog contest winner)

GetFirefox!
GetOpera!






Reply With Quote  
Join Date: Sep 2005
Posts: 689
Reputation: digital-ether has a spectacular aura about digital-ether has a spectacular aura about 
Rep Power: 6
Solved Threads: 41
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Practically a Master Poster

Re: Running PHP scripts in backround

  #9  
Jul 27th, 2006
I've posted an entry about this in my blog. if you are interested check out:
http://fijiwebdesign.com/content/view/86/77/
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote  
Join Date: Jun 2004
Posts: 36
Reputation: timhysniu is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
timhysniu timhysniu is offline Offline
Light Poster

Re: Running PHP scripts in backround

  #10  
Jul 31st, 2006
impressive wget option. I wasn't aware of that

Sorry for not clarifying: run in the background I meant run as a separate process which will not be a bottleneck for the main process.

Thanks very much
tim
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb PHP Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the PHP Forum

All times are GMT -4. The time now is 3:01 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC