views:

400

answers:

3

Is it good idea to run linux process in multi user environment.

Ex: Runtime rt = Runtime.getRuntime(); Process proc = rt.exec("linux command"); int exitVal = proc.waitFor(); // may wait till 5 to 10 mins

+3  A: 

You need to be aware of some things:

  • If the process uses files, you must make sure that each process gets its own set or they will overwrite each others input/output. Use File.createTempFile().

  • The processes will run with the user rights of the server.

  • If the process gets data which the user can change in the browser, make sure that he can't send something like "; rm -rf /".

  • If the process runs more than a few seconds, you must run it in the background and parse the output to see when it completes. This can get hairy quickly because of error handling, you'll need to keeping a reference to the running process in the session, etc.

Other than that, there is no reason to be afraid of this. Unix is a server OS which is designed to run many, many processes at the same time :)

Aaron Digulla
I'd add that you should consider how long the process is likely to run, and whether the connection might time out before it completes. If you're talking about some short, quick process, I agree with Aaron. If it's a non-trivial job, you might want to consider spinning off another thread or some such.
Jay
Thanks, I've updated my answer.
Aaron Digulla
A: 

It's very bad idea.

  1. Tomcat must be called under restricted account. So you could headache with permissions
  2. User will get timeout, so HTTP content never will be delivered
  3. No grants that servlet wouldn't be terminated - so you process will be zombie

Try to start process, but monitor them by watch-dog pattern with periodical ping from client browser

Dewfy
A: 

You haven't said enough about your run-time environment; though the question title mentions servlets and the question is tagged with servlet, you don't specify any specific run-time environment.

The answer to your question depends on very specific circumstances relating to your use case. For example, if you had a high-traffic site where access to any page would potentially spawn a new process which would run for some minutes, this does not sound like a good design, as any server might be easily brought down to a crawl in such a scenario. If, on the other hand, you have some specific long-running tasks which need to be run in a separate process and triggered via access to a web page where such access and process creation is controlled and deterministic, then it might be OK to do this. There is no actual technical obstacle.

Note that if you are using recent versions of Java, use of ProcessBuilder is regarded as preferable to using the older Runtime.exec.

Vinay Sajip