Environment
Development: Amazon EC2 instance.
Operational: Unknown at this time

Scenario
This is a *nix question but things start out in php, so please bear with me.

I'm trying to get php script (php_script_1) to to run another php script (php_script_2) in the background. This will allow php_script_1 to complete and return (with a suitable message to the user), having kicked off the lengthy php_script_2. The output of php_script_2 will be cached (in a database) for future use.

Progress to date
I have identified php's shell_exec() and a neat little "run_in_background" function wrapper to issue commands.

function run_in_background($Command, $Priority = 0){
	if($Priority){
		$PID = shell_exec("nohup nice -n $Priority $Command 2> /dev/null & echo $!");
	}
	else{
		$PID = shell_exec("nohup $Command 2> /dev/null & echo $!");
	}
	return($PID);
}

With this, I can successfully call my php_script_2 (from php_script_1) with :-

...
$command = "php /var/www/make_objects.php";
$priority = 0;
$pid = run_in_background($command, $priority);//kick off background process
...

make_objects.php is the main target of my current development effort so it isn't yet fully working, but at least I can call it.

What I need
I need to pass arguments to php_script_2. The equivalent href (HTTP request) would be .../make_objects.php?dt=data99&z=4 so these arguments become available in php as $_REQUEST and $_REQUEST.

What I have tried
I tried the following:

...
$command = "php /var/www/make_objects.php?dt=data99&z=4";
$priority = 0;
$pid = run_in_background($command, $priority);//kick off background process
...

but get the error: Could not open input file: /var/www/make_objects.php?dt=data99&z=4 . The *nix command parser clearly doesn't allow that.

Research on the web has failed to identify the correct approach.

My Question
How do I construct $command to call /var/www/make_objects.php and pass named arguments dt=data99 and z=4 ?

Many thanks in advance to the *nix guru(s) who understand this stuff.

Airshow

Look at cURL. You'll need the first PHP script to be running on a web server.

If you're not running a server, look at $argv and/or getopt.

Thanks Petteyg,

I have heard of cURL before but never needed to use it.

About to do some reading ....

Airshow

OK, I have cracked it.

The *nix command is of the form (simplified) :

php /var/www/make_objects.php --aa --bb 0 --label myLabel >> /var/www/make_objects.log 2>&1

The trick is to use php's getOpt() in the called script to read the command options:

$shortopts = '';
$longopts = array();
$longopts[] = REQUEST_AA;
$longopts[] = REQUEST_BB.":";
$longopts[] = REQUEST_LABEL.":";
$_OPTIONS = getopt($shortopts, $longopts);

where REQUEST_AA, REQUEST_BB and REQUEST_LABEL are defined (in my common.php file) as follows :

define('REQUEST_AA','aa');
define('REQUEST_BB','bb');
define('REQUEST_LABEL','label');

I chose the variable name $_OPTIONS to be like $_REQUEST, $_GET and $_POST, as it fulfills the same purpose.

Maybe it's just me but I thought getOpt() in the PHP manual could be better described, in particular the distinction between $shortopts and $longopts.

Thanks for the pointer petteyg.

Airshow

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.