如何通过web按钮onclick执行linux脚本?

时间:2017-05-12 11:23:01

标签: javascript php shell

我正在处理与通过网络按钮执行脚本相关的问题?当用户单击该按钮时,必须使用特定的脚本。我的脚本文件是auto.sh,它位于home/username/auto.sh脚本文件包括:

#!/bin/bash
cd $home
source /opt/ros/indigo/setup.bash 
roslaunch rosbridge_server rosbridge_websocket.launch
exit 0

如何在html onclick按钮上运行此脚本?你能逐步解释解决方案吗?

注意:在我的计算机上已经安装了Apache Web服务器和php。

2 个答案:

答案 0 :(得分:2)

FMashiro为您提供正确的答案。 根据这个答案,这里有一个例子。

首先,您必须确保拥有正确的权限。 确保您的用户/组与您的Web文件相同。如果不通过以下方式更改:

chown 

你的HTML看起来像这样:

<div class="example">
      <button type="button" name="button" onclick="myscriptcaller()">Click</button>      
    </div>
    <script type="text/javascript">
    function myscriptcaller() {
      $.$.ajax({
        url: '/path/to/yourphpfile.php',
        type: 'GET',
        dataType: 'default: Intelligent Guess (Other values: xml, json, script, or html)',
        data: {param1: 'value1'}
      })
      .done(function() {
        console.log("success");
      })
      .fail(function() {
        console.log("error");
      })
      .always(function() {
        console.log("complete");
      });

    }
    </script>

请不要将您的文件放在家庭/用户名中,您的脚本可能位于可从www但不能从公共区域访问的文件夹中。 讨论并在Executing a Bash script from a PHP script

中进行了描述

之后,您应该可以按照帖子中的说明访问文件夹。

e.g。的/ var / WWW / yourdesireddestination

你的php文件:

    <?php
      $message=shell_exec("/var/www/yourdesireddestination/auto.sh");
      print_r($message);
    ?>  

答案 1 :(得分:0)

你需要调用一个没有脚本的PHP脚本,例如ajax,在那个脚本中你可以使用php的shell_exec命令

在此处阅读更多内容:http://php.net/manual/en/function.shell-exec.php