Can anyone please share their experience of invoking unix scripts from J2EE env, either servlet or EJBs? Note that these scripts are to be invoked for real time processing and not offline processing.
The biggest problem you will have is if your app server memory image is large, when you fork to run the script you may well run out of memory and have the fork fail. When you fork, the system needs to make a complete copy of the executable image. It doesn't make a physical copy, but it does need to make a virtual one. So, if you have a large JEE heap, like 4G of real memory (i.e. not just Java heap, total process size), then you need an extra "free" 4G of real RAM and/or Swap for the fork to have enough virtual space to happen.
Yes, you're going to immediately exec sh or some other command that isn't going to suck up a gazillion resources. But the system can't know that, and so it needs to act as if it's going to have to run two copies of your JEE container at once, even for a nanosecond.
If you don't have the resources for the fork, the fork fails.
If you're strapped for space, then what you can do is create a little mini exec launcher daemon. Then instead of your JEE app forking the process, you just open a socket to your daemon, and IT forks the process. Obviously the expectation is that this little daemon is consuming much fewer resources than your container, so it's cheap to fork.
The daemon can be as simple taking the command line to execute over the socket, and just execing what it gets (potentially unsafe, naturally, but...), or simple rpc with a command code and some arguments. Whatever is appropriate for your project. You can write it in Java, a scripting language (Python, Perl, Ruby), whatever. Lots of ways to do that.
Spawning processes from a JEE container is probably not the right way to this. If these are shell scripts they will not be portable. If you want to use transaction support, the scripts could be rewritten as Jobs using Quartz Scheduler. This is more likely the JEE way to do things like that.
EDIT:With your requirements added in the commented this should work
Process process = new ProcessBuilder(command).start();
More details here
please note that if you use scripts and/or pipes (no native executables) you must include the shell to invoke the command (and setup pipes)
One possibility would be to write a small application that listens to a JMS queue and invokes the scripts. That way, the script execution is separated from the app server, so doesn't run into any spec limitations.