2

I'm new to web development. I'm trying to execute a shell script using PHP's shell_exec(). Inside the script, I'm trying to invoke a GUI application(Qt). When I executed the PHP script from a terminal the application started as expected. But when I opened it from browser an empty blank page appeared.

I'm using Ubuntu with apache2 server running as service. When I searched in google, the similar problem is solved in the Windows environment by allowing apache service to interact with the desktop.

PHP Script:

<?php
$log = shell_exec('sh testcmd.sh');
?>

testcmd.sh:

./Program1

Any help provided will be highly appreciated.

Pramod
  • 121
  • 2
  • 16
  • That's because shell_exec() issues a command for the server to follow, not the client. – KingRichard Oct 02 '17 at 05:10
  • Your missing the hashbang in testcmd.sh `#!/bin/sh`, also it will never work if you dont have the execute bit on `./Program1` with the correct permissions so www-data can execute it. – Lawrence Cherone Oct 02 '17 at 05:15
  • @LawrenceCherone I tried including #!/bin/sh to the script but no success. The executable has got all permissions(777). I tried with non-GUI applications and they are working fine. I think it has to do something with $DISPLAY. – Pramod Oct 02 '17 at 05:31
  • please refer to this Q&A[external-program-e-g-notepad-using-hyperlink](https://stackoverflow.com/questions/2800081/how-to-run-an-external-program-e-g-notepad-using-hyperlink) – Manoj Singh Oct 02 '17 at 05:37
  • @Pramod You won't get back the GUI in the browser if that's what you're after, I left that out in my previous comment as I thought that was obvious. – Lawrence Cherone Oct 02 '17 at 05:40
  • @ManojSingh I thin the answer provided in your link works only in Windows environment. – Pramod Oct 02 '17 at 05:49
  • @LawrenceCherone In windows we can invoke GUI applications from browser right? If so then there should be something to do it in Linux as well. – Pramod Oct 02 '17 at 05:51
  • Invoke != GUI, **Tried with non-GUI applications and they are working fine. I think it has to do something with $DISPLAY.** – Lawrence Cherone Oct 02 '17 at 05:53
  • @LawrenceCherone I mean to say that, instead of applications that have GUI, ie. for console applications I could see the PID which means it is running in the background. – Pramod Oct 02 '17 at 06:00
  • This is a slightly odd thing to do -- launching a local, graphical app from a browser. It might be possible, if you can overcome the X authentication problems that are bound to arise. But why do you need to do this? Presumably you could just run the GUI app in some other way, if it is on the same host as the browser. If you explained why you needed to this -- what the end result was intended to be -- then perhaps somebody would be able to come up with a less fiddly and non-portable way to solve your problem? – Kevin Boone Oct 02 '17 at 07:06
  • @KevinBoone I need to take information from the user like date, time, etc and with this info I need to filter some files present in the machine and display to user as some kind of list. Later user selects some file from this list. After all these, the final information should be passed as arguments for the GUI application to invoke it. – Pramod Oct 02 '17 at 07:43
  • So you're using a browser as a sort of extra GUI, run in advance of another GUI app? If you want to do this using a browser, you might find that the problems with X are better addressed to to a sysadmin forum. By default, your webserver won't be running in a context where it can launch an X app properly; fixing that depends on the way X is configured, and the desktop, etc. My approach would be to implement the GUI the web browser is currently running using Java or Python instead, which probably won't be difficult, then you'll have one GUI app launch another, which is usually non-problematic. – Kevin Boone Oct 02 '17 at 08:21
  • @Kevin Boone That option is eliminated as many applications are currently web based. So either this application needs to be made web based which will take a lot of time and effort or somehow this application needs to be invoked from browser – Pramod Oct 02 '17 at 09:56
  • Applications are usually web-based because they need to be invoked remotely. What you're proposing will not allow that mode of operation. Using a web browser as a kind of "poor man's user interface" to _local_ applications almost never works out well in the long term, although I understand why people do it. I'm sure a competent Linux sysadmin will be able to advise on your X problems, but I fear that the approach you are adopting will cause you pain eventually. – Kevin Boone Oct 02 '17 at 10:46

1 Answers1

0

It is somewhat unclear what you're asking.

If you wish that browsing to a certain web site will run a PHP script that will open a GUI app for the client to interact with, the answer is "you can't". The reason is that the way the setup works is that the server and the client run on different machines, and your PHP runs on the server machine. As such, the client never gets to see the running program.

The above is true also for Windows. The answer you quote in your question does not apply to a server running on a different machine than the client.

If, for whatever reason, you want something that works only when the server and client run on the same machine (or there is someone watching the server's display), then you need to do the equivalent of the Windows answer.

The graphics display on Linux (assuming you're not running wayland) is using a protocol called X11. In order for the display to appear, your GUI program needs two things. The first is to know which display it needs to use. This is supplied with an environment variable called DISPLAY. The second is an authorization to actually use that display.

So in order for your PHP script to run a GUI app that will show its GUI, you will need to first do the following steps:

  1. Set the DISPLAY variable to the correct value (copy from your desktop environment).
  2. Run xauth add something, where you can get what something is by running xauth list on your desktop environment.

If you do these two things (in this order), your GUI should show up.

Shachar Shemesh
  • 8,193
  • 6
  • 25
  • 57
  • yes, my client and server run on the same machine. I tried like you said but still no success. – Pramod Oct 02 '17 at 06:53
  • I got this message in the browser when I added 2>&1 at the end of command. Message: `xauth: unable to generate an authority file name No protocol specified QXcbConnection: Could not connect to display :0 Aborted (core dumped)` – Pramod Oct 02 '17 at 06:55
  • Then it is possible that your apache process is insulated in some way. If the `/tmp` directory for your apache and your X server are not the same, that could happen. – Shachar Shemesh Oct 02 '17 at 07:38