0

I'm working on the issue which is related to execution of script via web button ? When user click the button, specific script must be worked. My script file is auto.sh and it is located at home/username/auto.sh Script file includes :

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

How can I run this script on html onclick button ? Can you explain solution step by step ?

Note: In my computer Apache web server and php is already installed.

moguztas
  • 91
  • 2
  • 5

2 Answers2

2

FMashiro gives you the right answer. According to this answer here is an example.

Firstly you have to ensure to have the correct permissions. Be sure your user/group has the same as your web files. If not change it via:

chown 

Your html looks like this:

<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>

Please don't put your files in home/username, your scripts might be located in a folder which is reachable from www but not from the public area. Discussed and described in Executing a Bash script from a PHP script

After that, you should have a folder reachable as described in the post.

e.g. /var/www/yourdesireddestination

And your php file:

    <?php
      $message=shell_exec("/var/www/yourdesireddestination/auto.sh");
      print_r($message);
    ?>  
Max
  • 1,203
  • 1
  • 14
  • 28
  • I'm very new at php platform and linux. "Firstly you have to ensure to have the correct permissions. Be sure your user/group has the same as your web files." How can I understand this? – moguztas May 12 '17 at 12:05
  • @moguztas If you are new to this stuff you might be beginning with a finished lamp installation like XAMPP, if you can run your file you can move to set up your server by yourself. If you setup the server from scratch your apache server has an own user and your Linux system has one. In the default case, it's www-data for your apache. If you want to display a PHP page apache must have the rights to display the PHP file. Please read these two posts https://code.tutsplus.com/tutorials/an-introduction-to-apache--net-25786 http://fideloper.com/user-group-permissions-chmod-apache – Max May 12 '17 at 12:21
  • @moguztas a site which must be in your bookmarks is mdn and you migth be starting with this article https://developer.mozilla.org/en-US/docs/Learn/Getting_started_with_the_web – Max May 12 '17 at 12:28
  • In this site http://fideloper.com/user-group-permissions-chmod-apache is very good but second step did not work. I can not do. I'm so sorry to take your time but if you want help me please can you give me a detailed information? Because I'm very new at ubuntu. – moguztas May 12 '17 at 12:39
  • @moguztas where do your stuck? Have you used any tutorial or example to setup your unix? If yes please provide the links – Max May 12 '17 at 13:08
  • No, I did not use any tutorial. Firstly I did `$ sudo chown -R www-data:www-data /var/www` I think it must be correct. After, I write the `chmod go-rwx /var/www` I take the this : chmod: changing permissions of'/var/www': operation not permitted I want to save my script inside the `/var/www` as you said, but I can not do. I followed the these steps in following page: https://kuldeeparya.wordpress.com/2014/07/20/demo-how-to-invoke-a-shell-script-on-a-html-page-being-served-by-apache-on-linux-machine/ – moguztas May 12 '17 at 13:15
  • @moguztas try to run this command `sudo su` afterward you run as root the whole session. Then you can't get the operation permitted message – Max May 12 '17 at 13:38
  • I solved the operation permission problem. But I run the code as you write it, it did not work. php code modified as: ` ` javascript code same as yours except this line : `url: '/opt/lampp/htdocs/easyphp/phpCode.php'` What am i doing wrong ? – moguztas May 12 '17 at 14:08
  • @moguztas What happens if you open phpCode.php in your browser by calling http://localhost/easyphp/phpCode.php? Please move all your comments above to your post. This will help others when having the same problem as you got and don't see a messy post. – Max May 12 '17 at 16:25
0

You need to call a php script unsing for example ajax, and in that script you can use the shell_execcommand from php

Read more here: http://php.net/manual/en/function.shell-exec.php

Florian Humblot
  • 1,121
  • 11
  • 29