Page 1 of 1

launching a process from php

Posted: Fri Feb 03, 2006 7:29 pm
by userjs8448
I found a way to launch a separate process from php. It's fairly straight forward and simple, but it has come in handy. It will not work in windows.

the process is launched with launch_process. More code can be added to store the processID (which will be needed for process kill). I usually store it in a database field for ease of use. I used this code to launch a process in the background and update someting on a timed interval. It worked well for me, but there might be some incompatibilities in certain system combinations.

Code: Select all

<?php

//*********************************************************************************************************
//* Function Name: launch_process
//* Parameters: $process_path EX: /usr/bin/php test.php
//* Purpose: launches a process in the background
//* Note:The empty array for the output specs prevents opening any pipes to the new 
//*       process, so proc_open doesn't wait for the execution of script.php to 
//*       finish - the last parameter is just there because it has to be. 
//*       proc_close() closes the process immediately, so your PHP script doesn't Stop. 
//*
//*********************************************************************************************************

function launch_process($process_path) {
 
	$process_path .= " &"; //this will launch it into the background 	
	
    //this does the actual launching
	proc_close(proc_open($process_path,array(),$fvar));
}

//*********************************************************************************************************
//* File Name: kill_process()
//* Purpose: kills a process with the given process ID
//*********************************************************************************************************

function kill_process($proc_id) {
   //stop the proccess that has the id $proc_id
   //also, send signal 9
   posix_kill($proc_id,9);	
}

?>