Run shell script from web page

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:

# get today's 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.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 9 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
9 comments… add one
  • madhu3437 Apr 4, 2008 @ 9:59

    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

  • JAM Jun 15, 2008 @ 22:51

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


  • mohd Jan 15, 2010 @ 22:44


  • Abhishek Jan 28, 2010 @ 9:53

    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.

  • snehanshu Aug 14, 2012 @ 13:09


  • Pranav Oct 27, 2012 @ 20:00

    Nice article ..thnks

  • jetole Feb 17, 2013 @ 10:01
    You can use PHP or any other server side scripting language (including but not limited to python, perl, ruby, etc) to run bash commands or, preferably in my case, I often write a bash script then have the page run that. Also if it needs sudo and you're running apache, try php-sudo module.
  • rajesh Sep 27, 2013 @ 19:18

    Awesome, this exhibits the power of shell scripting.

    I learnt one new thing today.
    Thanks :)

  • keerthi koneru Mar 31, 2015 @ 12:25

    Hi I am keerthi. I am working on a project where i need to incorporate a bash shell in a website just as a online bash shell that we generally observe.

    If any one have any ideas kindly help me out with this.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum