Run a shell script with an html button
As stated by Luke you need to use a server side language, like php.
This is a really simple php example:
<?php
if ($_GET['run']) {
# This code will run if ?run=true is set.
exec("/path/to/name.sh");
}
?>
<!-- This link will add ?run=true to your URL, myfilename.php?run=true -->
<a href="?run=true">Click Me!</a>
Save this as myfilename.php
and place it on a machine with a web server with php installed. The same thing can be accomplished with asp, java, ruby, python, ...
Run bash file from web page
If you're running PHP on the web server, then you can post your web form to a PHP script, then use PHP's shell_exec()
function to call your bash script and pass the inputs. As paulsm4 mentions above, any time you call a shell script and pass inputs to a shell script from the web, you need to be very careful to sanitize the inputs to prevent injection type of attacks. One way to do this is to do all the validation and sanitization in the PHP script, before calling the shell script by way of shell_exec() to ensure that you are passing only clean input to the shell script.
See http://php.net/manual/en/function.shell-exec.php
Execute bash script from URL
source <(curl -s http://mywebsite.example/myscript.txt)
ought to do it. Alternately, leave off the initial redirection on yours, which is redirecting standard input; bash
takes a filename to execute just fine without redirection, and <(command)
syntax provides a path.
bash <(curl -s http://mywebsite.example/myscript.txt)
It may be clearer if you look at the output of echo <(cat /dev/null)
Run a Bash Shell Script from HTML Hyperlink?
I think that could enable CGI scripts in your webserver, send the script to /cgi-bin/ folder, rename it to myscript.cgi (remember to put the appropiate Shebang), set 0755 permissions, correct owner/group and put in the very beginning of the script:
#!/bin/bash
echo "Content-Type: text/html"
... more code...
The first echo line tells browser that the output of CGI script must be rendered as HTML (you could modify MIME type to your specific needs). There's a functional example:
#!/bin/bash
echo -e "Content-type: text/html\r\n\r\n"
echo "<html>"
echo "<head>"
echo "<title>TEST</title>"
echo "</head>"
echo "<body>"
echo "<h1>It Works!, Bash script running as CGI</h1>"
echo "</body>"
echo "</html>"
Execute a shell script from HTML
You can use a server side language. This is fairly easy with PHP
<?php
if(isset($_POST['submit']))
{
$output=shell_exec('sh /somePATH/cgi-bin/script.sh');
echo $output;
}
?>
<form action="" method="post">
<input type="submit" name="submit" value="Call my Shell Script">
</form>
Include all your other HTML and save the file with an extension .php
Any way to execute a bash Script from Webpage and Catching the Output while it executes?
I am not sure if you mean to require PHP in your solution, but if not…
In the past, I have used Jenkins for this kind of thing. You can use it as both a cron replacement and as a way to manually trigger scripts, all while keeping logs of what happens.
Jenkins can be set up such that some users only have permission to trigger jobs, as opposed to the Jenkins administrator who can do stuff like set up and edit jobs, etc.
And if you like getting email, Jenkins can do that too. If anything, the biggest downside to Jenkins is that it is super-flexible / configurable, and so not always obvious how best to set it up for one's particular needs.
Related Topics
Recursively Cat All the Files into Single File
What Is a Reasonable Amount of Inotify Watches with Linux
How to Export Symbol from Linux Kernel Module in This Case
How to Assign a Name for a Screen
Is There a Limit on Number of Tcp/Ip Connections Between MAChines on Linux
How to Remove All White Spaces from a Given Text File
What's an Alternative for Dtrace on Linux
Portable Way to Get File Size (In Bytes) in the Shell
Get Specific Line from Text File Using Just Shell Script
Find the Ip Address of the Client in an Ssh Session
Symbolic Link: Find All Files That Link to This File
Remove Odd or Even Lines from a Text File
Linux Command History with Date and Time
Realuid, Saved Uid, Effective Uid. What's Going On
How to Paste Multi-Line Bash Codes into Terminal and Run It All at Once
Manage Source Under Git and Svn Simultanously - Does It Make Sense