Run shell script from web page

by Vivek Gite on January 29, 2006 · 4 comments

Q. How do I run a shell script from a web server or a web page under Apache or Lighttpd websever?

A. In order to run a shell script from a web page you need Apache web server configured with cgi access. Apache CGI allows documents/files in cgi-bin directory treated as application and run by server when requested rather than as documents sent to the client.. It means if you put shell script in cgi-bin directory then you are able to execute them from a web page. However, you cannot simply run shell script from a web. You need to send or print the MIME type before outputting data to the web browser from shell script. You need to add following line to script before you get output back to browser:

echo "Content-type: text/html"
echo ""

Here is the script that can displays today's date and other information related to your shell script:

#!/bin/bash
# get today's date
OUTPUT="$(date)"
# You must add following two lines before
# outputting data to the web browser from shell
# script
 echo "Content-type: text/html"
 echo ""
 echo "<html><head><title>Demo</title></head><body>"
 echo "Today is $OUTPUT <br>"
 echo "Current directory is $(pwd) <br>"
 echo "Shell Script name is $0"
 echo "</body></html>"

Save the script in your cgi-bin directory and execute it from web page.

See also:

=> For more examples see how to: Run Linux / UNIX commands from a web page.

Featured Articles:

Share this with other sys admins!
Facebook it - Tweet it - Print it -

{ 4 comments… read them below or add one }

1 madhu3437 April 4, 2008

using shell script
all servers are running or not in a particular path,if running servers and not running servers result shown in web page (html)

please help

Reply

2 JAM June 15, 2008

Ahh, thanks for this!
I’ve been looking to do something like this for a while now

JAM

Reply

3 mohd January 15, 2010

thanks

Reply

4 Abhishek January 28, 2010

i have a shell script which stops the JBoss server in my application.
please help me out in invoking the script from a JSP page.
The requirement is to provide a tab or button which would invoke the script and stop the server.
thanks in advance.

Reply

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">
What is 10 + 4 ?
Please leave these two fields as-is:
Solve the simple math so we know that you are a human and not a bot.




Previous post:

Next post: