Page 1 of 1
PHP exec()
Posted: Wed Jul 15, 2009 1:24 pm
by manicsurfer
Wonder if some might help with this issue. I am attemtpting to run a script as a background process. The scripts below work, But I can't figure out how to pass a variable from the first page to the second page, so that I can use its value (in the second page.
Explaination;
I am trying to pass a variable from one page to another php page so far this is where I have got to;
Page One: Contains;
Code: Select all
<?
$var1 = "foobar";
exec("/usr/local/bin/php child_script.php $var1> /dev/null 2>&1 &");
if ($fp = fopen("log.txt", "a"))
{
fwrite($fp, "Main process finished at ".date("H:i:s", time())."!\n");
fclose($fp);
}
?>
The above page connect to (in the background);
child_script.php which contains;
Code: Select all
<?php
$thisVar = $var1;
sleep(10);
if ($fp = fopen("log.txt", "a"))
{
fwrite($fp, "$thisVar - Child process finished at ".date("H:i:s", time())."!\n");
fclose($fp);
}
?>
As you can see I am trying to pass $var1 to the child page so that I can use that variable in the child page. Can this be done. If so, can anyone throw me a few hints.
Help much appreciated!
Re: PHP exec()
Posted: Wed Jul 15, 2009 1:42 pm
by s.dot
Why not just include the child.php script? I believe you can pass query strings to the include.. (not sure, but worth a try)
Re: PHP exec()
Posted: Wed Jul 15, 2009 2:31 pm
by manicsurfer
The main reason for not using such an approach is because I would like the child script to run as a background process. Using the include function would make the parent page hang until the child script has finished.
Re: PHP exec()
Posted: Wed Jul 15, 2009 3:39 pm
by spider.nick
manicsurfer wrote:The main reason for not using such an approach is because I would like the child script to run as a background process. Using the include function would make the parent page hang until the child script has finished.
What is the purpose of child_script.php? Is it something that can be handled via an AJAX call?
Nick
Re: PHP exec()
Posted: Thu Jul 16, 2009 12:42 am
by manicsurfer
The child script performs inserts (MySQL) and sends sms for each through a web service and interates until there are no records left. The child script includes the php 'ignore_user_abort' function (being set to 'true') to ensure that users client cannot stop the process.
Re: PHP exec() RESOLVED
Posted: Thu Jul 16, 2009 1:18 am
by manicsurfer
OK
This has been resolved. The way to pass values from the first page to the second page is with $argv[0], $argv[1] etc.
So, page one...
Code: Select all
<?php
$UserID = "aValue";
$PATH=$_SERVER['DOCUMENT_ROOT'];
exec("/usr/local/bin/php ".$PATH."/child_script.php \"".$UserID."\" >/dev/null &");
if ($fp = fopen("log.txt", "a"))
{
fwrite($fp, "Main process finished at ".date("H:i:s", time())."!\n");
fclose($fp);
}
?>
NOTE: In the above code it may be required that use of the php function 'escapeshellcmd' around $UserID variable...for example escapeshellcmd($UserID). This is to escape special characters in user input (if user input applies).
In page two then is;
Code: Select all
<?php
$thisVar = $argv[1];
sleep(10);
if ($fp = fopen("log.txt", "a"))
{
fwrite($fp, "$thisVar - Child process finished at ".date("H:i:s", time())."!\n");
fclose($fp);
}
?>
Just in case anyone else needs the solution!

Re: PHP exec()
Posted: Thu Jul 16, 2009 8:50 am
by s.dot
I believe the way you're using exec() the script will hang until it is finished anyways. I don't believe (i could be wrong) you are forking the scripts here - as php does them in a very linear non multi-threaded way.
You'd have the same results using include/require as using exec.
Re: PHP exec()
Posted: Thu Jul 16, 2009 7:39 pm
by Benjamin
A simple solution would be to use memcache... And