How do I run a batch file or Unix Shell Script from a Servlet.
Use the Runtime.exec() call from the appropriate place in your servlet.
One caveat to look out for is that, if you expect to see environment variables that the script would otherwise know if executed from a command line you will be in for a surprise. Env vars wont be visible in your script if you use Runtime.exec() unless you "shell-out" appropriately.
If your script is called myscript.sh then
If in the servlet you have:
Will allow the script to see environment variables that a csh would.
Alternatively, if you have:
Will execute the script, but none of your usual environment variables will be visible in the script.
All this assumes that the script to be executed is on the machine that is running the servlet engine. (I have'nt tried this with rsh)
Hope that helps... As always, be careful with what the script is supposed to do. Make sure that your servlet engine is not running as root. Unbeknownst to you, there may be security holes that you are exposing if you are not careful about launching scripts from servlets.
Thanks for the info. Will try.
Raj, what does it mean when a servlet is running as root?
Some servlet engines like Resin and Orion provide the option to specify the user that the process must run under. In a typical unix system, processes that are to be started automatically when the system boots up are started by init. Typically, all daemons started by init run as root. If the servlet engine is started this way then it will be running as root. One can use the "su -c .... userid" approach to start the process as a non-root user. Some servlet engines also allow for specifying the userid as config parameter in one of its startup files. I am sure your servlet engine has documentation to this effect. Not sure how much of this is applicable to Win9x systems.