Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-15-2006, 08:19 PM   PM User | #1
roypython
New Coder

 
Join Date: Dec 2005
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
roypython is an unknown quantity at this point
running another script from current script

Hi.
Your help is very much appreciated.
I want to be able to run script 2 from script 1.
I'm using (in 1.php):
Code:
exec  ("c:\php4win\php c:\2.php");
However nothing happens.
Script 2 doesn't run and no error is being thrown.
what am I doing wrong??
(I tried run the "c:\php4win\php c:\2.php" through the windows command line - and it runs fine).
Thanks a lot
Roy
roypython is offline   Reply With Quote
Old 02-15-2006, 09:19 PM   PM User | #2
ralph l mayo
Regular Coder

 
ralph l mayo's Avatar
 
Join Date: Nov 2005
Posts: 951
Thanks: 1
Thanked 31 Times in 29 Posts
ralph l mayo is on a distinguished road
including a script executes it
ralph l mayo is offline   Reply With Quote
Old 02-15-2006, 09:38 PM   PM User | #3
roypython
New Coder

 
Join Date: Dec 2005
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
roypython is an unknown quantity at this point
thanks for the quick reply.
I did forget to mention that the second script (2.php), takes very long to execute (5-10 minutes) as it does image processing.
Therefore I want script 1.php not to wait until script 2.php finishes (when 2.php it will send email to the user notifying him that it finished running).
That's why I'm trying to use exec()
roypython is offline   Reply With Quote
Old 02-16-2006, 12:19 AM   PM User | #4
slushy77
New Coder

 
Join Date: Jan 2006
Posts: 98
Thanks: 0
Thanked 0 Times in 0 Posts
slushy77 is an unknown quantity at this point
hi Roy,

This is what i understand is happenning - script 1 takes input from the user which it then passes to script 2 for processing, but because of the time taken by script 2, script 1 must send some information back to the browser saying something like "we are processing, the results will be emailed".

so essentially you want script 1 to start a background process and then finish so the user can do something else.

Does script 2 send anything back to the user's browser?
Is script 2 actually doing the image processing work or is it calling exec to run some image processing commands on the server ?
what operating system is the server ?
what web server are you using - apache, IIS ?
__________________
live every day as if it was your first
slushy77 is offline   Reply With Quote
Old 02-16-2006, 04:18 AM   PM User | #5
roypython
New Coder

 
Join Date: Dec 2005
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
roypython is an unknown quantity at this point
Thanks for the help.
You understand correctly - Script 1 starts a background process(script 2).
Script 1 returns to the browser "we are processing your request, email will be sent to you when finished".
Script 2 is running in the background, when finished processing, it sends email to the user with the results.

Does script 2 send anything back to the user's browser?
Yes. When finished, sends email.

Is script 2 actually doing the image processing work or is it calling exec to run some image processing commands on the server ?

It is calling an executable (image magick) inside a loop.
Basically the loop iterates all the images in a folder, and sends it to imagmagick through exec().
Script 2 waits until image magick finished running and examine the results(sync call)


what operating system is the server ?
Developing on Win, production is Linux(Debian)

what web server are you using - apache, IIS ?
apache

THanks
Roy
roypython is offline   Reply With Quote
Old 02-16-2006, 05:52 PM   PM User | #6
slushy77
New Coder

 
Join Date: Jan 2006
Posts: 98
Thanks: 0
Thanked 0 Times in 0 Posts
slushy77 is an unknown quantity at this point
Hi Roy,

first up you need to make sure that any executable (php,imagemagik, mail, xyz etc) you want to exec() has permission to run within your apache/php environment. Its a security feature.
chapter 42 (safe mode) of the php manual provides details of this, and it might be useful to look at the relevant apache docs as well.

It might be an idea to use batch/sh files to do the work of script2 but thats up to you.

the next task is to get the script to run in the background.
on linux it is easy, on windows its a bit tricky. This routine should do the trick. substitute mycommand for the executable you are calling
PHP Code:
//start a background process and return immediatley to php
if (PHP_OS=="WINNT"){
   
exec ("start cmd /c mycommand myoptions");
   
// start cmd starts a new process , /c makes it exit on completion
}
else {
   
exec("mycommand myoptions &");
   
// & makes program run in background, termination is automatic

__________________
live every day as if it was your first
slushy77 is offline   Reply With Quote
Old 02-16-2006, 07:49 PM   PM User | #7
roypython
New Coder

 
Join Date: Dec 2005
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
roypython is an unknown quantity at this point
Thanks for your reply and example.
1. I can't get it to work (when trying to run the exec() line, it sometimes hangs and sometimes just skips the exec() line).
2. It doesn't throw ANY error (not to the browser when I run it with IE and not to the output window when running it with MAGUMA IDE)
3.I tried to run (just to eliminate other possible errors in my 2.php script) the win calculator, and again, it did nothing(skpipped the line).
Code:
<?php
	echo "start";
	exec ("start cmd /c C:\WINNT\system32\calc.exe");
?>
( I noticed, that php is like "egnoring" this line [even when I try to run executable that doesn't exist, it still, just skips the exec line without reporting any error).
i.e.
Code:
exec ("start cmd /c C:\WINNT\system32\calcsssssssss.exe");
As you suggested, I set the safe mode vars in c:\WINNT\php.ini (I use WIN 2000) to:

Code:
safe_mode = off
safe_mode_gid = Off
safe_mode_include_dir =				
safe_mode_exec_dir = 
safe_mode_allowed_env_vars = PHP_
safe_mode_protected_env_vars = LD_LIBRARY_PATH
;open_basedir =
disable_functions =
disable_classes =

and the error reporting to:
error_reporting  =  E_ALL
display_errors = On
display_startup_errors = On
log_errors = On
log_errors_max_len = 1024
ignore_repeated_errors = Off
ignore_repeated_source = Off
report_memleaks = On
track_errors = On
;html_errors = Off
;docref_root = "/phpmanual/"
;docref_ext = .html
;error_prepend_string = "<font color=ff0000>"
;error_append_string = "</font>"
;error_log = filename
;error_log = syslog

Thanks
Roy

Last edited by roypython; 02-16-2006 at 07:51 PM..
roypython is offline   Reply With Quote
Old 02-17-2006, 02:22 AM   PM User | #8
slushy77
New Coder

 
Join Date: Jan 2006
Posts: 98
Thanks: 0
Thanked 0 Times in 0 Posts
slushy77 is an unknown quantity at this point
Hi Roy,

its not a good idea to switch safe-mode off, instead set the safe_mode_exec_dir to point at the directories which you want to run executables inside your webserver session.
Code:
 
safe-mode = on
safe_mode_exec_dir = "c:\imagemagick;c:\php\bin"
for exec() to run background processes you just need to redirect the ouput, non of the previous command line code i sent is needed (aplologies) instead do the following
PHP Code:
//start a background process and return immediatley to php
//$keepLog determines whether to keep the output
$keepLog true;
if (
PHP_OS=="WINNT"){
   
exec ("mycommand myoptions > logfile.txt");
   
// start cmd starts a new process , /c makes it exit on completion
}
else {
   if (
$keepLog){
       
exec("mycommand myoptions > logfile.txt")
   }
   else {
       
exec("mycommand myoptions > /dev/null");   
   }
   
// & makes program run in background, termination is automatic

__________________
live every day as if it was your first
slushy77 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:47 PM.


Advertisement
Log in to turn off these ads.