简体   繁体   中英

running bash file with “xte command” in php

For my project I need to run a bash script located on a ubuntu server through php.

This is my sh script: test1.sh

#!/bin/bash
xte 'keydown Control_L' 'keydown Alt_L' 'key l' 'keyup Alt_L' 'keyup Control_L' 

my php file is: bashTest1.php

<?php
$message = shell_exec("bash test1.sh 2>&1");
echo $message;
?>

But when I open the php page in browser, I get this error:

Unable to open display 'default'

the 'xte' command in 'test1.sh' basically performs Ctlr+Alt+L action virtually. I have checked the file permissions for 'test1.sh'. it is ok. And I have run the bash file in terminal it is working. I have also checked 'ls' command in the same 'test1.sh' file and it shows all files in that directory in my browser. Can you please help me to run this command in my php script.

Thank you all in advance!

Finally I solved the problem. In Ubuntu every user is given a DISPLAY. In my bash file first I had to specify on which DISPLAY I needed to execute the command. At last I needed to grant the php user permission to execute the command. finally my bash file looks like this:

#!/bin/bash
export DISPLAY=":0"
xhost +
xte 'keydown Control_L' 'keydown Alt_L' 'key l' 'keyup Alt_L' 'keyup Control_L'

NOTE: Bear in mind to check your user's display first. because different users will be given different DISPLAYS. You can check your user display by this command in terminal

who

You are missing 2 things:

  1. The address of the X display needs to be either passed to xte with the -x parameter, or set to the DISPLAY environment variable.
  2. The XAUTHORITY environment variable needs to be set with the magic cookie of your X display.

This answer has much more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM