Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 310
  • Last Modified:

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).
0
KCTeam
Asked:
KCTeam
  • 14
  • 13
  • 7
1 Solution
 
Richard QuadlingSenior Software DeverloperCommented:
Where are the programs installed? PHP runs on the server, so the programs need to be installed on the server.

0
 
Richard QuadlingSenior Software DeverloperCommented:
And I would make sure you supply the FULL path to the program rather than relying on any shell path parsing.
0
 
KCTeamAuthor Commented:
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.
0
Technology Partners: 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!

 
Richard QuadlingSenior Software DeverloperCommented:
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.
0
 
KCTeamAuthor Commented:
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?
0
 
KCTeamAuthor Commented:
another try with the commercial kylix program  $s_command = '/usr/local/bin/startdelphi &'.
No output and the program won't launch.
0
 
TeRReFCommented:
Are you sure the user wwwrun has the proper permissions to run these binaries?
0
 
TeRReFCommented:
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?
0
 
KCTeamAuthor Commented:
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.
0
 
TeRReFCommented:
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.
0
 
KCTeamAuthor Commented:
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.
0
 
TeRReFCommented:
Should be working alright ;)
0
 
KCTeamAuthor Commented:
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?
0
 
Richard QuadlingSenior Software DeverloperCommented:
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.
0
 
KCTeamAuthor Commented:
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)..

0
 
TeRReFCommented:
Like I said, you cannot login as wwwuser indeed. Have a look at the system logs. They will tell you what went wrong.
0
 
KCTeamAuthor Commented:
Once you had linux installed, did you configure something to be able to call programs from php pages? (user permissions, apache config..... whateva...)
0
 
TeRReFCommented:
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...

0
 
TeRReFCommented:
Any luck yet?
0
 
KCTeamAuthor Commented:
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!!
 
0
 
Richard QuadlingSenior Software DeverloperCommented:
Can you try your code on another server or workstation?
0
 
KCTeamAuthor Commented:
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...
0
 
TeRReFCommented:
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
0
 
TeRReFCommented:
If you look into your Kylix code, you'll see that there is a 'uses' line somewhere on top. Make sure you remove and units you don't need from there!
0
 
KCTeamAuthor Commented:
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?
0
 
TeRReFCommented:
It is runnable from command line?

And is it runnable when you run it with full path:
/home/eh/hello
0
 
KCTeamAuthor Commented:
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".
0
 
TeRReFCommented:
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)
0
 
KCTeamAuthor Commented:
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?
0
 
Richard QuadlingSenior Software DeverloperCommented:
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.
0
 
TeRReFCommented:
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 :))
0
 
Richard QuadlingSenior Software DeverloperCommented:
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.
0
 
KCTeamAuthor Commented:
Ok. Sounds good! Anyway, thx for the big help. C ya!
0
 
TeRReFCommented:
You're welcome!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 14
  • 13
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now