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.
=> For more examples see how to: Run Linux / UNIX commands from a web page.Tweet itFacebook itG+ itDownload PDF versionFound an error/typo on this page?
- 30 Cool Open Source Software I Discovered in 2013
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012
- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop