PHP - system command - in the background

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ianripping
Forum Newbie
Posts: 17
Joined: Thu Feb 05, 2004 3:20 am
Location: Leeds, UK

PHP - system command - in the background

Post by ianripping »

Hi,

I have a PHP page that calls a python script from the command line using the PHP system command:

system($command);

The python script takes about 60 secs to run, I would like to display a 'Please Wait' message on the page whilst the script is running
Does anyone have any suggestions for the best way to do this?

Thanks
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: PHP - system command - in the background

Post by Chris Corbyn »

ianripping wrote:Hi,

I have a PHP page that calls a python script from the command line using the PHP system command:

system($command);

The python script takes about 60 secs to run, I would like to display a 'Please Wait' message on the page whilst the script is running
Does anyone have any suggestions for the best way to do this?

Thanks
Execute it using AJAX so it's asyncronous. Or just use output buffering to flush the message to the screen. The browser will hang though without ajax.
ianripping
Forum Newbie
Posts: 17
Joined: Thu Feb 05, 2004 3:20 am
Location: Leeds, UK

Post by ianripping »

Could you give me an example of how to use AJAX in this context?
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post by impulse() »

Why wouldn't this work:

Code: Select all

echo "Please wait";
system("python script.ph");
ianripping
Forum Newbie
Posts: 17
Joined: Thu Feb 05, 2004 3:20 am
Location: Leeds, UK

Post by ianripping »

The browser not display anything until the command has been executed and has returned an exit code - it will then display the echo line.

This is my problem - I want to see the echo line first - then the process executes.
Then when the exit code is returned - I want to redirect to another page.
ianripping
Forum Newbie
Posts: 17
Joined: Thu Feb 05, 2004 3:20 am
Location: Leeds, UK

Post by ianripping »

I could use exec('start /B "command',$output,$return);

but this will not tell me when the command has finished executing - which I need to know before redirecting to another page where another script will be executed!
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post by impulse() »

Code: Select all

echo "Please wait";
system("python script.py");
header("Location: http://redirect.com");
That should work, I think
ianripping
Forum Newbie
Posts: 17
Joined: Thu Feb 05, 2004 3:20 am
Location: Leeds, UK

Post by ianripping »

My solution:

system ('start /low

Code: Select all

')
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post by ok »

AJAX, as said before. You can read about it at: http://developer.mozilla.org/en/docs/AJAX.

Basically, you need to open your PHP page and each time tell PHP to execute another program (exec.php?prog=1, exec.php?prog=2 etc...).
When PHP is done, print (with PHP) message that indicates whether the execute did well or not - AJAX will display it to the user.

In my opinion, Java is the best language to execute programs (it gives more control...).
Post Reply