简体   繁体   English

在PHP中使用“ xte命令”运行bash文件

[英]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. 对于我的项目,我需要通过php运行位于ubuntu服务器上的bash脚本。

This is my sh script: test1.sh 这是我的sh脚本: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文件是: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: 但是,当我在浏览器中打开php页面时,出现此错误:

Unable to open display 'default'

the 'xte' command in 'test1.sh' basically performs Ctlr+Alt+L action virtually. “ test1.sh”中的“ xte”命令基本上实际上执行Ctlr + Alt + L操作。 I have checked the file permissions for 'test1.sh'. 我已经检查了“ test1.sh”的文件许可权。 it is ok. 没关系。 And I have run the bash file in terminal it is working. 我已经在正在运行的终端中运行了bash文件。 I have also checked 'ls' command in the same 'test1.sh' file and it shows all files in that directory in my browser. 我还检查了同一“ test1.sh”文件中的“ ls”命令,它在我的浏览器中显示了该目录中的所有文件。 Can you please help me to run this command in my php script. 您能帮我在我的PHP脚本中运行此命令吗?

Thank you all in advance! 谢谢大家!

Finally I solved the problem. 最后我解决了这个问题。 In Ubuntu every user is given a DISPLAY. 在Ubuntu中,每个用户都有一个DISPLAY。 In my bash file first I had to specify on which DISPLAY I needed to execute the command. 首先,我必须在bash文件中指定执行命令所需的DISPLAY。 At last I needed to grant the php user permission to execute the command. 最后,我需要授予php用户执行命令的权限。 finally my bash file looks like this: 最后我的bash文件看起来像这样:

#!/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: 您缺少2件事:

  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. X显示的地址需要通过-x参数传递给xte ,或设置为DISPLAY环境变量。
  2. The XAUTHORITY environment variable needs to be set with the magic cookie of your X display. XAUTHORITY环境变量需要使用X显示器的魔术cookie进行设置。

This answer has much more information. 此答案有更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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