Forking a process

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
fatherlyons
Forum Newbie
Posts: 2
Joined: Thu Jun 16, 2005 4:45 am

Forking a process

Post by fatherlyons »

Hi All,

I am relatively new to the world of PHP and I'm having a bit of difficulty trying to fork a perl process in a php script. The perl process which I need to fork takes a couple of minutes to complete so I want the user to be able to press a button on the web page which executes the perl script then leave that page and have the perl script running in the background as an independent process.
The basic structure of the code I'm writing is:

Code: Select all

//Perform some database queries here to gather required data.
  
  //Call the perl script..Not really sure if this is the correct way to use the system() function??
  system('perl D:\dir1\Test.pl', $retval);

  //Then fork this perl process using pcntl_fork()
  ????
If anyone can give me a hand here I'd be eternally grateful!!

Thanks in advance,
Denis
Syranide
Forum Contributor
Posts: 281
Joined: Fri May 20, 2005 3:16 pm
Location: Sweden

Post by Syranide »

forking is not internally possible, so, on windows, I believe you need to run "start <what you want to run>" instead, as start executes the process and then exits.

(know that this will likely pop up a in a command-console if it is such an application (which it should be)

otherwise, there is the possibility of creating an instance of "W32Shell" (or something like that) and use the windows API for doing this, this also allows you to hide the window I think. But, I'm not so certain this forks the process, or if it possible by using that, as it is pretty much a longer way to reach exec.

See if you have any luck with that.
fatherlyons
Forum Newbie
Posts: 2
Joined: Thu Jun 16, 2005 4:45 am

Post by fatherlyons »

Thanks for that. I'm not sure how to use this start() method though as I cant find it in the online documentation. Could you give me a quick example??

Thanks again,
Denis
Syranide
Forum Contributor
Posts: 281
Joined: Fri May 20, 2005 3:16 pm
Location: Sweden

Post by Syranide »

it's a "command-prompt construct", which in words, is like starting the application through running another application. start in other words, takes parameters, and executes it as a program ("start /?" in "cmd" will give you some more info)

so

Code: Select all

system('start perl "D:\\dir1\\Test.pl" > output.txt', $retval);
that should do it, unless the redirection doesn't work, which I think it won't then I guess running it in a .bat(ch)-file would solve it.
however, remember that you don't want to run this on your workstation as it would create up a commandprompt each time the script is run.

trying googling, I bet there are some good examples of this, the "W32Shell" should have some functionality however, don't got time/will to look things up right now, perhaps someone else here knows?
nikosft
Forum Newbie
Posts: 2
Joined: Thu Jun 16, 2005 5:47 am

Post by nikosft »

what about unix systems? is there sth similar to start command?
Revan
Forum Commoner
Posts: 83
Joined: Fri Jul 02, 2004 12:37 am
Location: New Mexico, USA
Contact:

Post by Revan »

nikosft wrote:what about unix systems? is there sth similar to start command?
You could probably just add "&" to the end of the call.
Post Reply