I want to trigger execution of a server-side script (.sh file) from JavaScript code running in a web browser.
I've searched google and no code that I've found is working for me.
I want to trigger execution of a server-side script (.sh file) from JavaScript code running in a web browser.
I've searched google and no code that I've found is working for me.
You need to first make a file in a server-side language (like PHP, Python, RoR, PERL, ASP.NET, or JSP) that runs the .sh file. Then you have to use Ajax to request that page.
In PHP, something like:
<?= shell_exec('sh /home/user/public_html/scripts/script.sh'); ?>
Then, for JS, something like:
<script src="//code.jquery.com/jquery-1.8.0.min.js"></script>
<script>
var runShellScript = function () {
$.get('/scripts/script.php', function () {
alert('Shell script done!');
});
};
// Some stuff
runShellScript();
</script>
If you don't want to use jQuery or Ajax, you could probably get away with JSONP.
Make sure you .sh file begins with:
#!/bin/sh
echo "Content-type: text/html"
echo ""
and the .htaccess file is set up to execute .sh files:
Options +ExecCGI
AddHandler cgi-script sh
Then you can simply call the file from the url using XMLHttpRequest. http://en.wikipedia.org/wiki/XMLHttpRequest, and maybe even have your script return some json object.
I don't know if Javascript opens .sh files in particular, but you can use the filereader API to open files in general.