从Java触发shell脚本的最佳方法 [英] Best method of triggering a shell script from Java

查看:383
本文介绍了从Java触发shell脚本的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个shell脚本,我想从J2EE Web应用程序触发。

I have a shell script which I'd like to trigger from a J2EE web app.

脚本做了很多事情 - 处理,FTP等等 - 它是遗留物。

The script does lots of things - processing, FTPing, etc - it's a legacy thing.

需要很长时间才能运行。

It takes a long time to run.

我想知道什么是最好的接近这个。我希望用户能够单击链接,触发脚本,并向用户显示一条消息,说明脚本已启动。我希望HTTP请求/响应周期是即时的,不管我的脚本需要很长时间才能运行。

I'm wondering what is the best approach to this. I want a user to be able to click on a link, trigger the script, and display a message to the user saying that the script has started. I'd like the HTTP request/response cycle to be instantaneous, irrespective of the fact that my script takes a long time to run.

我可以想到三个选项:

I can think of three options:


  • 在处理用户点击期间产生一个新线程。但是,我不认为这符合J2EE规范。

  • 在HTTP响应流中发送一些输出并在触发脚本之前提交它。这给出了HTTP请求/响应周期已经完成的错觉,但实际上处理请求的线程仍然在那里等待shell脚本完成。所以我基本上为了我自己的目的劫持了容器HTTP处理线程。

  • 创建一个包装脚本,在后台启动我的主脚本。这将使请求/响应周期在容器中正常完成。

以上所有内容都将使用servlet和Runtime.getRuntime ()。exec()。

All the above would be using a servlet and Runtime.getRuntime().exec().

这是在Solaris上使用Oracle的OC4J应用服务器在Java 1.4.2上运行。

This is running on Solaris using Oracle's OC4J app server, on Java 1.4.2.

请问有没有人对哪个是最不具备解决方案的解决方案有什么看法?为什么?

Please does anyone have any opinions on which is the least hacky solution and why?

或者有人有更好的方法吗?我们已经提供了Quartz,但我们不希望将shell脚本重新实现为Java进程。

Or does anyone have a better approach? We've got Quartz available, but we don't want to have to reimplement the shell script as a Java process.

谢谢。

推荐答案

我会选择选项3,特别是如果你实际上不需要知道脚本何时完成(或者有其他方法可以找到除了等待进程结束)。

I'd go with option 3, especially if you don't actually need to know when the script finishes (or have some other way of finding out other than waiting for the process to end).

选项1浪费了一个线程,它正在等待脚本完成。选项2似乎是一个坏主意。我不会劫持servlet容器线程。

Option 1 wastes a thread that's just going to be sitting around waiting for the script to finish. Option 2 seems like a bad idea. I wouldn't hijack servlet container threads.

这篇关于从Java触发shell脚本的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆