Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlets calling unix scripts

 
san patil
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a unix script (san.sh). I want to call this script through a servlet.
Please suggest!
Thanks,
san
 
Dominik Ratajski
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try:
String[] cmdString = { scriptname, parm0, parm1...};
Process process = Runtime.getRuntime().exec(cmdString);
try {
process.waitFor()...
} catch (InterruptedException)
if you're returning a value from the script the process.waitFor() can be used as a condition statement ie:
if (process.waitFor() == 0) {
DSR
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13056
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And if the script does output to standard out or err, you better consume the streams - see java.lang.Process - getErrorStream() etc.
If you don't consume the streams you get very odd results.
Bill
 
Kaustubh Patil
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you might be able to use the Runtime. But make sure u have all the permissions. I was once trying touse runtime to execute a simple "dir" command on windows. But it returned me an Exception that I do not hv enough permissions to do so.
Dont know why..
-Kaustubh.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic