Solved

PHP Exec() Timing

Posted on 2006-11-24
9
421 Views
Last Modified: 2013-11-18
Howdy

I need to execute a C program in Linux through a PHP program.  The trick is that it needs to execute after the browser has been closed and the session ended because it's an algorithmically intense program that will take a significant amount of time to run.  If the C program is invoked while PHP is running, I believe the C program will be terminated when the PHP session times out and closes.  This would be bad.  

I've heard about something like an exec(at) type of command, where you can specify when the exec() gets invoked after a specific amount of time following the termination of the PHP session (5 seconds, 10 seconds, etc.), but I can't find anything on the subject anywhere.  The output isn't an issue, as I'm creating output files and don't require any browser support.  So, any ideas on how I can exec() this puppy correctly?

Jason
0
Comment
Question by:cdromes
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 29

Assisted Solution

by:TeRReF
TeRReF earned 100 total points
ID: 18010907
I think you should be able to use exec() while the script is still running without the child process being killed after the script dies.
Try this:
exec('full/path/to/c_program &');

Of course, the user the webserver is running under should have exec permissions on that c program for it to work...
0
 
LVL 43

Expert Comment

by:ravenpl
ID: 18011961
If You want to execute it after the connection was ended, create a function with the above exec() then use
http://pl2.php.net/manual/en/function.register-shutdown-function.php
0
 

Author Comment

by:cdromes
ID: 18012494
The register_shutdown_function appears to be just the ticket, but I'm having some trouble getting it to work.  I can't get the program to run using the following as a command and parameter list for the exec() function at the end:

$cmd = "/home/bioinf06/TeamBlack/www-home/sbin/./yeti";
$Syn_args = array($rand, $_FILES["Gene_File"]["name"], $check, $_FILES["Syn_File"]["name"],
                  $counter, $email, $folder);
$args = array($rand, $_FILES["Gene_File"]["name"], $check, $counter, $email, $folder);

if ($check == 1)
   {
      register_shutdown_function(exec($cmd, $Syn_args));
   }
   else
   {
      register_shutdown_function(exec($cmd, $args));
   }

I'm not sure how the command line being executed looks (whether the spacing between parameters is correct) and, therefore, am unsure how to go about debugging this.  Has anyone run into this before?

Jason
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

by:John Kawakami
ID: 18013753
I'm not sure it'll work:

In PHP 4.0.6 and earlier under Apache, the registered shutdown functions are called after the request has been completed (including sending any output buffers), so it is not possible to send output to the browser using echo() or print(), or retrieve the contents of any output buffers using ob_get_contents(). Since PHP 4.1, the shutdown functions are called as the part of the request so that it's possible to send the output from them. There is currently no way to process the data with output buffering functions in the shutdown function. Shutdown function is called after closing all opened output buffers thus, for example, its output will not be compressed if zlib.output_compression is enabled.

------
My inclination is to fire up the C compiler.  Write a command that forks a copy of itself, detatching from the parent process.  This command can be called via exec() and will run after httpd shuts down.

That command could then call the lengthy C command (via a C exec())

http://www.elitehackers.info/files/txt/programming/linux%20daemons.htm

------
Doing it PHP style is via pcntl, looks sloppier, and fork would fork a copy of httpd... which is large:

http://usphp.com/manual/en/ref.pcntl.php

------
I'm not at my unix machine, so I can't write it for you at this moment.
0
 
LVL 43

Accepted Solution

by:
ravenpl earned 200 total points
ID: 18013755
Not that way

finction sh1() {
 exec("/home/bioinf06/TeamBlack/www-home/sbin/./yeti arg1 arg2 &");
}
finction sh2() {
 exec("/home/bioinf06/TeamBlack/www-home/sbin/./yeti arg11 arg22 &");
}

if ($check == 1)
   {
      register_shutdown_function('sh1');
   }
   else
   {
      register_shutdown_function('sh2');
   }
0
 
LVL 29

Expert Comment

by:TeRReF
ID: 18013844
Did you try it my way like this?
exec('full/path/to/c_program &');

The beauty of this setup is, is that you start the process while your script still runs, but the & means that PHP will 'release' the process right away and it will run on it's own. That means that even if your PHP script ends, the C program will continue running till it completes it's task...
0
 

Author Comment

by:cdromes
ID: 18017256
Before I try jk2001's recommendation, I think TeRReF and ravenpl are on the right track, but I'm pretty sure at this point that I'm running into a permissions issue with Linux.  I created a C++ program that accepts arguments then prints them to a dummy file.  I can invoke this program from within the directory, but from nowhere else in the shell (I get the permission denied message).  So, if I can't even call this program remotely from within the same shell, I'm definitely going to be screwed when attempting it from 'Apache'.  

So, I think the exec(... &) will work with the register_shutdown_function, but I'm currently stuck in Unix permission hell.  I'm going to throw this question into the Linux side of this site and see what they say, but if anyone here has any input, I'd welcome it, as well.

Thanks for all the help so far.

Jason
0
 
LVL 43

Expert Comment

by:ravenpl
ID: 18017531
The apache user (or httpd or http) has to be allowed to execute Your application. Please try
chmod +x executable.exe
cp -f executable.exe /tmp
then try exec("/tmp/executable.exe p1 p2 &"); from php.

About permissions: executeable has to be executeable for all(chmod +x file). If it's script it also has to be readable for all (chmod +r file).
Every directory where the executeable sits, has to executable/serchable by anyone (chmod +x the_dir). Eg. if the exec is
/home/users/user1/bin/exec.exe all /home/users/user1/bin /home/users/user1 /home/users /home have to have exec bit set.
Now, php may run in safe mode, which can restrict executing from any dir, but one specified from php.ini. Verify by calling
phpinfo();
from php script.
0
 

Author Comment

by:cdromes
ID: 18017861
It turned out to be something much simpler...I was attempting to execute the program with the normal ./ designation in the path.  No one involved seemed to like this, which is why it didn't work.  When I took the ./ out of the path, it worked like a champ.  So, the working code for the post-script execution:  

if ($check == 1)
                 {
                    register_shutdown_function(exec("/home/bioinf06/TeamBlack/www-home/bin/test $rand $gene_file $check $syn_file $counter $email $folder &"));
                 }
                 else
                 {
                    $check = 0;
                    register_shutdown_function(exec("/home/bioinf06/TeamBlack/www-home/bin/test $rand $gene_file $check $counter $email $folder &"));
                 }

Thanks for the permission info, ravenpl....it's already been a pretty substantial pain as this program has grown, so I'll keep your input close at hand for future reference.  

Jason

0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question