That will not works, you have to create an action for that:

<?php
    if (isset($_POST['button']))
    {
         exec('test.pl');
    }
?>
<html>
<body>
    <form method="post">
    <p>
        <button name="button">Run Perl</button>
    </p>
    </form>
</body>
Answer from tttony on Stack Overflow
🌐
Stack Overflow
stackoverflow.com › questions › 40778218 › run-php-script-in-html-on-button-click
Run PHP script in html on button click - Stack Overflow
November 24, 2016 - try to trace something in .sh file ... script here ... if(isset($_REQUEST['btn'])) { echo 'Button is clicked'; // you can your shell script here like: // shell_exec("/var/www/html/Camera/CameraScript.sh"); }...
Discussions

Exec a PHP function from a Form button? - PHP - SitePoint Forums | Web Development & Design Community
I need to execute a custom PHP function when I click a button (not a type Submit) in a form. I’d also like this function to reside in the same PHP file as the form. I want to avoid as much JavaScript as it is possible. Could any one point me in the right direction? TIA, Dan More on sitepoint.com
🌐 sitepoint.com
0
January 8, 2004
Running a shell script from an html button - Raspberry Pi Forums
After pressing on the button it redirects me to either a blank page or the source code displayed on the webpage. My script for when it displays the following ... But I've also sort of successfully accomplished the task only that it truncates and doesn't keep ... More on raspberrypi.org
🌐 raspberrypi.org
July 11, 2020
linux - Execute shell commands through an html button(onclick ) - Stack Overflow
I am trying to execute shell commands or shell script through onclick function in HTML button. For eg: Click Me! Is there any ... More on stackoverflow.com
🌐 stackoverflow.com
Run a shell script with an html button - Stack Overflow
I want to launch a bash script when a button is pressed on a website. This is my first attempt: Click Me! But no luck. Any More on stackoverflow.com
🌐 stackoverflow.com
🌐
Unix.com
unix.com › shell programming and scripting
Launch shell script with parameters from a webpage button - Shell Programming and Scripting - Unix Linux Community
October 18, 2017 - I want to create a simple html page that should contain 2 fields in which the user can write the input. Then I want to have a button that should launch a shell script with the parameters inserted by user in the fields from the webpage. This is the code behind my webpage:
🌐
SitePoint
sitepoint.com › php
Exec a PHP function from a Form button? - PHP - SitePoint Forums | Web Development & Design Community
January 8, 2004 - I need to execute a custom PHP function when I click a button (not a type Submit) in a form. I’d also like this function to reside in the same PHP file as the form. I want to avoid as much JavaScript as it is possible. C…
🌐
Raspberry Pi
raspberrypi.org › forums › viewtopic.php
Running a shell script from an html button - Raspberry Pi Forums
July 11, 2020 - After pressing on the button it redirects me to either a blank page or the source code displayed on the webpage. My script for when it displays the following ... <?php shell_exec('qjackctl &'); ?> But I've also sort of successfully accomplished the task only that it truncates and doesn't keep running with the following
🌐
LinuxQuestions.org
linuxquestions.org › questions › programming-9 › run-bash-script-with-html5-button-won't-work-4175614458
[SOLVED] Run bash script with HTML5 button - won't work
September 25, 2017 - Hi I've created an index button that calls a php script that in turn calls a bash script. The bash script won't work'. HTML5: Code:
Find elsewhere
Top answer
1 of 1
2

PHP code is evaluated on the server with each request, so there is no way to call a PHP function from the browser directly.

Instead, a new request is made from the browser via Javascript to the PHP file that contains the function you'd like to run.

web-page.php

<?php 
  // web-page.php
?>

<button id="play">Play</button>
<button id="next">Next</button>
<button id="prev">Prev</button>
<button id="stop">Stop</button>

<script>
  const play = document.getElementById("play");
  const next = document.getElementById("next");
  const prev = document.getElementById("prev");
  const stop = document.getElementById("stop");

  play.addEventListener("click", ()=>{executeCommand('play')});
  next.addEventListener("click", ()=>{executeCommand('next')});
  prev.addEventListener("click", ()=>{executeCommand('prev')});
  stop.addEventListener("click", ()=>{executeCommand('stop')});

  async function executeCommand(action) {
    try {
      const res = await fetch(`http://your-site.com/execute-shell-command.php?action=${action}`);
      console.log(res.json());
    } catch (error) {
      console.error(new Error(error));
    }
  }
</script>

<?php

execute-shell-command.php

<?php

// Get the query params
$action = $_GET["action"];

if (isset($action)) {
  $command = shell_exec("echo {$action} >/tmp/vlc_command");
  
  // Returns FALSE if pipe can't be established
  if ($command === FALSE) {
    $output = 'Pipe could not be established';
  }
  
  // This function can return NULL both when an error occurs or the command produces no output
  if ($command === NULL) {
    $output = 'Command produced no output';
  }
  
  // A string containing the output from the executed command
  if ($command) {
    $output = $command;
  }
}

// Anything echo'd will be the response to the request
// and will get console.logged in the browser
echo json_encode(array("requested action"=>$action, "output" => $output));

This example doesn't include any security measures to prevent unauthorized requests to the PHP file, it just demonstrates how to trigger a snippet of PHP code to run from the browser.

Top answer
1 of 5
12

What you are trying to do is not possible that way.

Note that there are always two sides to that: The client side and the server side. Is the script on the client computer or on the server?


If it's on the client: You as the visitor are only seeing an HTML website. onClickwill only be able to launch JavaScript (or other scripting languages), but not any arbitrary shell script that resides on your computer. HTML scripts only run in the browser and can only do limited things. Most importantly, they can't interact with your computer.

Think about it: How would the browser know how to open the file? Don't you think this would be a security issue as well – a plain website triggering the execution of scripts on a client's computer? What if there was something like onClick('rm -rf /home/user')?

An alternative would be to run a Java applet, if you want code to be executed on the client, but this not exactly the same and it's something really complicated. I don't think it's necessary to explain this in detail.


If the script is on the server: If you want to run a script on the server side and have the user trigger its execution, then you need to use a server side programming language. Just HTML won't do it, because it's more or less a static file. If you want to interact with the server, you could for example use PHP.

It has the exec function to run a command line script that is stored on the web server. So basically, you could write exec('/path/to/name.sh'); and it would run the script on the server.

However, just putting this into onClick is not enough here. If you don't know about PHP and server side web programming yet, you might want to read a few tutorials first and then come back with a more specific question.


If you have a php file with the appropriate exec(...) command, make sure the script has execute permissions set not only for the user but also for the group the web server is in, so in the simplest case just 777.

In case of trouble check for the return value of the script with echo exec(...); to see if there are any errors.

You can also run the script from the command line and not from the browser with php /path/to/file.php.

2 of 5
4

You need some server side intelligence for this. HTML alone is not enough, because it is static. One common way would be php. Many hosted offers do have php installed by default.

You can use an ftp program to put a text file into the root directory of your server.

The textfile could be named "run.php" with the following content:

<h3>Executing /path/to/name.sh</h3>
<?
exec('/path/to/name.sh');
?>

Let's say your domain is "example.com", if you visit this page in your browser:

http://example.com/run.php

then the php file will be executed on the server. It will send an HTML page to the browser with the heading. And the script will be executed on the server.

There are some things to keep in mind and some possible improvements:

1) everybody will be able to hit this page, also robots. You could secure the page with htacces.

2) This page will fire on case of a normal "GET" request from the browser. But it involes an action on the server, and if this action changs data or does something important, it would be better to only fire the script on a POST request.

3) You can insert a form / button statement to be able to reload / execut again the page. Be sure to use the right method (GET or POST) in the method attribute of the HTML form statement.

4) It would be good to get the result of the shell script (rturn code and maybe text output) and write it to the browser. This has enough issues for a separate qustion : )

🌐
PHP
php.net › manual › en › function.shell-exec.php
PHP: shell_exec - Manual
It is not possible to detect execution failures using this function. exec() should be used when access to the program exit code is required. An E_WARNING level error is generated when the pipe cannot be established. ... If you're trying to run a command such as "gunzip -t" in shell_exec and getting an empty result, you might need to add 2>&1 to the end of the command, eg: Won't always work: echo shell_exec("gunzip -c -t $path_to_backup_file"); Should work: echo shell_exec("gunzip -c -t $path_to_backup_file 2>&1"); In the above example, a line break at the beginning of the gunzip output seemed to prevent shell_exec printing anything else.
🌐
Stack Overflow
stackoverflow.com › questions › 71683508 › how-to-make-a-html-button-to-execute-a-shell-script
php - how to make a html button to execute a shell script? - Stack Overflow
my problem is i try to make an button on a Website such as Submit to execute an bash script on server side my first try was with Php an Exec but it dont work html site: <html> <form action="exec.php" method ="get"> <input type="submit" value="Bestätigen"> </form> </html> ... <?php shell_exec("/var/www/html/testsite/sc.sh"); header('Location: http://192.168.2.1/hs.html?success=true'); ?>
🌐
Stack Overflow
stackoverflow.com › questions › 59191195 › html-button-launch-shell-script
php - HTML button launch Shell script - Stack Overflow
html button to call php shell_exec command · 6 · Run PHP File On Button Click · 0 · How to execute a bash file from php on a button click · 0 · html form button run command php exec · 1 · Trying to only use HTML to make a sh script on PC run when a button is clicked · 0 · How to execute linux script via web button onclick?
🌐
Unix.com
unix.com › applications › web development
Cannot execute sh file using button click in php file in apache - Web Development - Unix Linux Community
October 20, 2017 - I the problem that i facing is cannot use button click to execute the sh file that store in the same location. the program file is a php file and running in apache2. the code that i run is show below <button onclick="s…
🌐
Quora
quora.com › How-do-I-create-a-PHP-button-to-run-a-script-on-a-click
How to create a PHP button to run a script on a click - Quora
Answer (1 of 6): I assume as with "PHP button" you mean a button that calls a script on the server that will execute some code. First you need to make sure that the script that you want to execute can be reached via the web server in some ways: - the script IS the page - you ‘include’ the scrip...
🌐
Raspberry Pi
raspberrypi.org › forums › viewtopic.php
Executing a python script through php button - Raspberry Pi Forums
March 31, 2015 - Hi Everyone, I am having an issue with executing my python script through using a button on PHP. I looked through any or all relevant questions regarding my issue, and tried any answers i could find and modifying the file name to my own. This is the PHP code on my website: ... <FORM> <button type="button" onclick="parent.location='action.php'">open</button> </FORM> The PHP code will be executing the python script, which i wrote:
🌐
Experts Exchange
experts-exchange.com › questions › 26020580 › HTML-Button-to-Run-PHP-Script.html
Solved: HTML Button to Run PHP Script | Experts Exchange
April 23, 2010 - ; $command2 = '/usr/bin/sudo /usr/bin/ssh root@192.168.0.1 b pool test1 list'; #$rt = shell_exec(escapeshellcmd( ... Log in or create a free account to see answer. Signing up is free and takes 30 seconds. No credit card required. ... Log in or create a free account to see answer. Signing up is free and takes 30 seconds. No credit card required. ... Hello Thansks for pointing fix on HTML code, My php script is actually logiing to device, executing a command and displaying the same.