PHP exec()

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
manicsurfer
Forum Newbie
Posts: 4
Joined: Wed Jul 15, 2009 1:17 pm

PHP exec()

Post 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!
Last edited by Benjamin on Thu Jul 16, 2009 7:37 pm, edited 2 times in total.
Reason: Added [code=php] tags.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: PHP exec()

Post 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)

Code: Select all

include 'child.php?var=1';
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
manicsurfer
Forum Newbie
Posts: 4
Joined: Wed Jul 15, 2009 1:17 pm

Re: PHP exec()

Post 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.
spider.nick
Forum Commoner
Posts: 72
Joined: Wed Jul 15, 2009 12:22 pm
Location: Overland Park, KS

Re: PHP exec()

Post 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
manicsurfer
Forum Newbie
Posts: 4
Joined: Wed Jul 15, 2009 1:17 pm

Re: PHP exec()

Post 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.
manicsurfer
Forum Newbie
Posts: 4
Joined: Wed Jul 15, 2009 1:17 pm

Re: PHP exec() RESOLVED

Post 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!

:roll:
Last edited by Benjamin on Thu Jul 16, 2009 7:38 pm, edited 1 time in total.
Reason: Added [code=php] tags.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: PHP exec()

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: PHP exec()

Post by Benjamin »

A simple solution would be to use memcache... And

Code: Select all

tags.
Post Reply