Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I want to launch a bash script when a button is pressed on a website. This is my first attempt:

<button type="button" onclick="/path/to/name.sh">Click Me!</button>

But no luck. Any suggestions?

EDIT- Following slhclk's and mit's advice:

I have a php file in /var/www that I point my web-browser to. The contents of the file is as follows:

<?php
exec("/home/aa/scripts/test.sh");
?>

tesh.sh contains this:

screen -S server1 -X quit

If I type /home/aa/scripts/test.sh in shell, the script is able to execute. However, when I point my web-browser to http://example.com/screen.php (which has the code above), I see a blank page and the script hadn't executed. Both have execute permissions. Why doesn't it work?

share|improve this question
    
It would be interesting to know what kind of web server this is. For example it could be a home linux server, a windows machine with XAMPP or a standard website mass hosting offer with a domain name from a big hoster? –  mit Jun 4 '11 at 9:54
    
I am on debian lenny which runs off a vps I rent –  Kevin Duke Jun 4 '11 at 9:58

3 Answers 3

up vote 6 down vote accepted

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.

share|improve this answer
    
Thanks very much for the thorough answer. The shell script resides on my computer. I am trying what you have written, however, I am running into a problem. I have edited my question above –  Kevin Duke Jun 4 '11 at 9:55
    
Okay, this is a different issue then. Do you actually have PHP installed? Try to get the return code of the exec line by writing echo exec(...);. Also note that the script needs execute permissions for the group your webserver runs on, not just your user. –  slhck Jun 4 '11 at 10:04
    
yes I have chmod 777 on both files. And I tried running the php directly from shell with weird results. pastebin.com/eU0xTkp2 However, I do have php installed, confirmed with php -v and the fact that I have phpfilemanager for people to edit files on. Why isn't it using the php interpreter? –  Kevin Duke Jun 4 '11 at 10:12
    
You need to run it with php /var/www/trash/run.php and not just the PHP file name itself. –  slhck Jun 4 '11 at 10:15
1  
Not a thing! I'm not too sure on the specifics, but should be because when you run it from the command line, you run it as the correct user, whereas when run from the browser, the user is missing. You would need to check that within your shell script, I guess. –  slhck Jun 4 '11 at 10:38

It could be a permission issue.

When you triggered it using webapp, it is apache that runs the script. If apache does not have permission to run the script it will not work.

Solution: check the permission and try again.. :)

share|improve this answer

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

share|improve this answer
    
Thanks mit, I haven't been able to get this to work the way you describe. I have edited my question, hopefully to shed some light –  Kevin Duke Jun 4 '11 at 10:02
    
+1 for the additional safety suggestions –  slhck Jun 4 '11 at 10:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.