Link to home
Start Free TrialLog in
Avatar of KCTeam
KCTeam

asked on

Problem running external program with php

Hello everyone!!

Have a little problem. I'm trying to run a program by clicking a buttonion a php web page. When i run it in a shell (I work with Suse linux 9.1), it works perfect . But the same call with php functions(exec(), passthru(), shell_exec...) won't work. Why?
Standard linux shell commands like whoami or pwd work when called throught php. But none of my personal program or commercial ones runs when called by php functions. Any idea? (safe_mode is off in php.ini).
Avatar of Richard Quadling
Richard Quadling
Flag of United Kingdom of Great Britain and Northern Ireland image

Where are the programs installed? PHP runs on the server, so the programs need to be installed on the server.

And I would make sure you supply the FULL path to the program rather than relying on any shell path parsing.
Avatar of KCTeam
KCTeam

ASKER

Thx for the fast answers. Actually PHP and the programs are on the same server. And i do use absolute path when calling the programs.
Ok, turn on full error reporting in your PHP script.

<?php
error_reporting(E_ALL);
?>

and capture the output of your command ...

<?php
// Full debugging.
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Run an external program.
$s_command = '/usr/bin/my_prog param1 param2'; // Completely made up this line. Give us an example.
$s_output = shell_exec($s_command);

// What was reported.
echo "<pre>$s_output</pre>";
?>

Can you now tell us the command and the output.
Avatar of KCTeam

ASKER

i tryed first with the "whoami" command and the output was "wwwrun" which is totaly normal.
then i try with $s_command = '/home/$mylogin/bin/Project1' : Project1 is the name of my program, and it's located in /home.$mylogin/bin which is a valid directory. This time, nothing. No output at all. What do you think?
Avatar of KCTeam

ASKER

another try with the commercial kylix program  $s_command = '/usr/local/bin/startdelphi &'.
No output and the program won't launch.
Are you sure the user wwwrun has the proper permissions to run these binaries?
Delphi is an X-Windows program isn't it? That will never work since there will not be a display available. That might be the problem with Project1 as well?
Avatar of KCTeam

ASKER

about wwwrun, it's the apache daemon. Startdelphi and Project1 are linux programs. StartDelphi is the launcher of kylis which is the linux version of delphi. Both program run perfect when launched from a shell or by clicking on their icons. I can't even launch standard programs like firefox, or the gimp with this method.
Like I said, of course not. wwwrun does not have a display available. A display is needed for GUI programs to run since they are X-Windows programs. It could only work if you would login to X-Windows as wwwrun. That will not work either since that user is not allowed to. In short: X-Windows programs can NEVER be started from PHP when you initiate it from browser.
Avatar of KCTeam

ASKER

OOOOOOOOOOHH MY!!!!
you're damn right. What was i thinking!!! I think i have my answer. I'll try with no Gui Programs and i'll let you know if it finally works.
Should be working alright ;)
Avatar of KCTeam

ASKER

Let's start over. Now i have a program with no gui that access mysql DB and does a few thing in it like "create table..", "select...", "insert"....
howdo i launch it via php? i tryed to use :
<?php
// Full debugging.
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Run an external program.
$s_command = '/usr/bin/my_prog param1 param2'; // Completely made up this line. Give us an example.
$s_output = shell_exec($s_command);

// What was reported.
echo "<pre>$s_output</pre>";
?>
replacing command's path with full path of my new program. how come i still can't launch my program and i stiil don't get any output?
What is your program written in? Even though it may not USE a gui, it may fail to load if the GUI is unavailable. Maybe try logging in as the wwwrun user (don't know if that is possible). Then manually run the program from the command line/shell.

Does that work? If not, then it pretty much points to the permissions of prog_x not allowing wwwrun to execute it.
Avatar of KCTeam

ASKER

no way to log in as wwwrun because it's apache daemon's login as a virtual user, i think.
but the prog runs when launched in my shell and logged as a standard user.
My program 's been  written with kylix(linux version of delphi)..

Like I said, you cannot login as wwwuser indeed. Have a look at the system logs. They will tell you what went wrong.
Avatar of KCTeam

ASKER

Once you had linux installed, did you configure something to be able to call programs from php pages? (user permissions, apache config..... whateva...)
I use FreeBSD, but in this case it works the same.
There is 2 things that you need to do in order to run an external app via PHP.
1. Make sure the executable  is runnable by the webserver process by setting the proper permissions with chmod (755)
2. Make sure it does not use any X-Windows libraries that need a display available (In *nix programs that are run under X-Windows use a 'display' to make themselves visible (hope that explanation makes sense))

Of course it might be possible to run the programs if you use the php-cli version (Command line interface) That process runs under the currently logged in user...

Any luck yet?
Avatar of KCTeam

ASKER

hello guys. I'm back with my php issues. I tryed to use php-cli as you advised me. I created a php executable file in which i launch a program with exec, shell_exec,system and passthru(i tryed all four). When used with php in a console, everything's ok. But once called from the web ( with full path), program won't start. I precise that my programs have no gui. one access mysql db, creates new db, create table and insert values, the sleep for 5 seconds. The other one just writes "program launched", sleeps 5 seconds ,write "prog will exit " and finally exits.

Questions: - Is php-cli bug-free? - are there any config to add for cli to work? - I read a few stuff about suphp and how to get permissions to execute external programs. do you know annything about that? if so and if it can help resolving my problem, can you tell what i have to do ...

Thx!!
 
Can you try your code on another server or workstation?
Avatar of KCTeam

ASKER

i already did! my programs work perfect from the shell, as they're very simple ones. But, once called from the web ... :-(. Do you have a little program that you call from the web using php? If you do, can i try it on my workstation to make it clear that my program are not the ones making trooouble...
You can download a hello world program I wrote quickly in C from here:
http://www.xs4all.nl/~cits/hello.tar

Untar it, chmod it to 777 (just to be sure :)
And try to run it from PHP
ASKER CERTIFIED SOLUTION
Avatar of TeRReF
TeRReF
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of KCTeam

ASKER

this is what i did with the hello you sent me:
s_command = "/home/eh/hello";
system($s_command) or die("NO");

The output is "NO";
I think we are now sure that the problem doesn't come from my programs.  Right?
It is runnable from command line?

And is it runnable when you run it with full path:
/home/eh/hello
Avatar of KCTeam

ASKER

oh!!! apparently there's a missing shared library for hello.
"/home/eh/hello: error while loading shared libraries: libstdc++.so.6: cannot open shared object file: No such file or directory".
Oh darn...
You will have to compile it yourself then..
Put this in hello.c

#include<stdio.h>

main()
{
    printf("Hello World");
}

And compile it with a c or c++ compiler

Try this from command line:
c++ hello.c

If all went well, there should be an executable called a.out in the same dir. Rename that to hello and try again
(both from command line and PHP)
Avatar of KCTeam

ASKER

TeRReF, you are the man!!!! Great advice about the unused units in my program. All the php executioon functions works know. The fact is i had some unit weren't needed to execut e my program. So THX a lot. By the way, how did you know about thaht weird "useless units" thing? I mean why doesn't my program work because of some units that are not even used to make the call?
If one of the units includes a compiler directive that says only run within a GUI, then when you try to load it, it will fail to load.
I used to program in Delphi a lot. Not one of my favorite languages I must say. Although the compiler is very fast, it doesn't see some crucial errors you can make while programming causing access violations that were VERY hard to debug.

But who am I :))
Yeah. You'd release the code and a user would phone with the details.

Unless you had the debug version of EXACTLY the same release (i.e. not a single line of code change and not even a recompile), then the debugger would completely lose it.

But that's the problem with optimization. The code that executes can quite often bear no relationship to your code.

e.g.

A loop from 0 to 10 which has no impact outside of the loop will sometimes execute 10 to 0 as doing a decrement and compare to 0 is quicker than incrementing and comparing an integer!

Took me a while to work out WTF as I was stepping through the code.

I liked Delphi for the IDE and a pretty solid Object model. Not perfect in all regards, but a lot better than VB at the time (Delphi 3). But as Borland is selling off the IDE, I don't know what future Delphi has. Kylix has more of less gone now too I think. Shame. I cut my teeth with Borland Turbo C in the 80's 90's.
Avatar of KCTeam

ASKER

Ok. Sounds good! Anyway, thx for the big help. C ya!
You're welcome!