943,724 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 10203
  • PHP RSS
Jul 21st, 2006
0

Running PHP scripts in backround

Expand Post »
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
Reputation Points: 10
Solved Threads: 1
Light Poster
timhysniu is offline Offline
46 posts
since Jun 2004
Jul 22nd, 2006
0

Re: Running PHP scripts in backround

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.
Reputation Points: 10
Solved Threads: 7
Posting Whiz in Training
zippee is offline Offline
294 posts
since Jan 2005
Jul 22nd, 2006
0

Re: Running PHP scripts in backround

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.
Reputation Points: 11
Solved Threads: 0
Newbie Poster
Rotak is offline Offline
19 posts
since May 2005
Jul 24th, 2006
0

Re: Running PHP scripts in backround

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.
Reputation Points: 10
Solved Threads: 1
Light Poster
timhysniu is offline Offline
46 posts
since Jun 2004
Jul 24th, 2006
0

Re: Running PHP scripts in backround

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sheepcentral is offline Offline
1 posts
since Jul 2006
Jul 25th, 2006
0

Re: Running PHP scripts in backround

Quote 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.
Sponsor
Featured Poster
Reputation Points: 550
Solved Threads: 728
Bite my shiny metal ass!
pritaeas is offline Offline
4,166 posts
since Jul 2006
Jul 26th, 2006
0

Re: Running PHP scripts in backround

Quote 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.
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005
Jul 26th, 2006
0

Re: Running PHP scripts in backround

PHP does always run in the background it is server side scripting. something like javascript which is client side would run in the foreground.
Reputation Points: 33
Solved Threads: 19
Nearly a Posting Virtuoso
mikeandike22 is offline Offline
1,496 posts
since May 2004
Jul 27th, 2006
0

Re: Running PHP scripts in backround

I've posted an entry about this in my blog. if you are interested check out:
http://fijiwebdesign.com/content/view/86/77/
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005
Jul 31st, 2006
0

Re: Running PHP scripts in backround

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
Reputation Points: 10
Solved Threads: 1
Light Poster
timhysniu is offline Offline
46 posts
since Jun 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in
Next Thread in PHP Forum Timeline: Paasing the values of Combo Box





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC