3

I have a python script which is needed to be run by php:

     <?php
     $command = escapeshellcmd('/home/Desktop/test.py');
     $output = shell_exec($command);
     echo $output;

     ?>

The out put of the python script is a binary file but I get the following error in the log: Unable to access the X Display, is $DISPLAY set properly?

The php code works fine from the terminal but no luck when I try to run it from the browser. Any idea what is going on? Ideally, I don't want to change my program. I want to know how you can rectify the X Display error. Is there a way to check if $DISPLAY is set properly? ( I am working with Ubuntu)

I tried this : pidof X && echo "yup X server is running" on my terminal and it is saying yup x server is running!

3
  • 1
    Sounds like $DISPLAY isn't set. Why are you trying to run a GUI app from a webserver? Commented Apr 28, 2015 at 19:20
  • echo prints out strings, si shell_exec should return a string. I think that you have to modify your python script to return only string and it should not use any GUI. Commented Apr 29, 2015 at 17:41
  • Your app uses something that uses the X server. The terminal user probably has the X server running, so it works. The PHP running user does not have the X server running, so it doesn't work. My suggestion would be to modify the script to not use any X requiring methods or start X and access it somehow. Commented Apr 29, 2015 at 18:30

3 Answers 3

1

Add the following text as the first line of your Python script:

#!/usr/bin/python

Without this, the kernel doesn't know what interpreter to run your script with, and may end up trying to launch it using /usr/bin/import (because that word probably appears on the first line of the script). The import utility requires access to the X11 display (it's a screenshot utility, basically), which is why you're getting this error.

Sign up to request clarification or add additional context in comments.

5 Comments

What about if the server is Windows?
@sємsєм If they're using something like Cygwin, it'll work! If not, we'd need to know a lot more about their configuration.
This is my first line of python script: #!/usr/bin/env python so should I change it to #!/usr/bin/python ?
Hmm, no, that should be fine then. Can you answer some of the comments on your post asking what your script is doing?
The output is saved in a C++ format ....self.blocks_file_meta_sink_0 = blocks.file_meta_sink( blocks.GR_FILE_FLOAT, True, int(header_every*250), self.extras_str, True)..It reads the data from another device
0

The python file you need may need to open a window to run. You say you saw it run in terminal though? What's test.py? Is it propitiatory? If you try using this as a command in your PHP: (not 100% that the shell escape won't strip this so may need to comment that out)

python -c \'print \"Test\"\' 

and see if you get the output text back. If so it's a python issue not PHP and the test.py file may be instantiating something that needs the $DISPLAY var set. PHP doesn't set the $DISPLAY var as it is shell commands not GUI.

1 Comment

Sorry I don't understand what you mean by asking "Is it propitiatory?"..the program reads the data from a receiver.
0

try popen

$command = "/usr/bin/python /home/Desktop/test.py";
$handle = popen($command, "r");

"r" for read

$read = fread($handle, 10);

10 is the size of output you want to take,

echo $read ;

hope it helps,

2 Comments

This won't solve the problem. (In fact, it won't work at all, because the path to python is missing a slash.)
sorry that was a typo mistake,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.