简体   繁体   English

在Java中将Shell脚本作为线程执行

[英]Executing a shell script in java as a thread

I need to execute a shell script in a java program. 我需要在Java程序中执行Shell脚本。 I figured out that i can use processbuilder and runtime.exec.. but my webserver times out every 180 sec but my script execution takes more than that..i do not want to use process for this approach.. is there any other way where i can use thread for this execution. 我发现我可以使用processbuilder和runtime.exec ..但我的Web服务器每180秒超时一次,但是我的脚本执行花费的时间更多。.我不想使用此方法的过程..还有其他方法我可以使用线程执行此操作。

thanks. 谢谢。

I'm assuming that the response from the script is intended for humans to read. 我假设脚本的响应旨在供人类阅读。

Good interface design, and human nature, suggests that if your script is taking over 180 seconds to run, then it should be run separately from the web server. 良好的界面设计和人性暗示,如果您的脚本运行需要超过180秒的时间,则应与Web服务器分开运行。 On linux, I would suggest putting it into 'cron', and letting it run on a regular basis. 在Linux上,我建议将其放入“ cron”,并使其定期运行。 You would only serve the results of the script via the web server, with a response time in seconds instead of minutes. 您只能通过Web服务器提供脚本的结果,而响应时间以秒为单位,而不是几分钟。

If your script depends on parameters from the http request, or other information that is only available from within the web server's environment, you have the following choices. 如果您的脚本依赖于http请求中的参数或仅在Web服务器环境中可用的其他信息,则可以选择以下选项。

  1. If you can figure out the likely combinations of parameters, run the script automatically for each combination of parameters, again only serving the results through the web. 如果您可以找出可能的参数组合,请为每个参数组合自动运行脚本,再次仅通过网络提供结果。
  2. If the majority of the time is spent in a single command, and the results of that command don't change much between runs, move that command into a separate script that runs automatically, and use the results of that separate script to build the web response. 如果大部分时间都花在单个命令上,并且该命令的结果在两次运行之间变化不大,则将该命令移至自动运行的单独脚本中,然后使用该单独脚本的结果构建网络响应。
  3. Break the response up into segments, only showing a portion of the data for each request, allowing the user to page through the response. 将响应分为几部分,仅显示每个请求的一部分数据,从而允许用户翻阅响应。 The script would be rewritten to only request the necessary data for the current page, reducing the amount of time needed to obtain that data. 脚本将被重写为仅请求当前页面的必要数据,从而减少了获取该数据所需的时间。
  4. Rewrite the script in a compilable language, which might gain you enough time to make running it for every request reasonable. 用可编译的语言重写脚本,这可能使您有足够的时间使每个请求的运行合理。 However, if the problem is a database query, this won't do you any good. 但是,如果问题是数据库查询,那么这对您没有任何好处。 You'd have to go with option (3), whether you rewrote it in a compilable language or not. 无论是否以可编译的语言重写了选项(3),您都必须使用它。

Without additional information, like an example of the script, or a description of where you're getting the results from, that's the best I can do. 如果没有其他信息,例如脚本示例或描述从何处获得结果的描述,那是我所能做的最好的。

A process can run several threads, but they still are parts of the process. 一个进程可以运行多个线程,但是它们仍然是该进程的一部分。

So, all threads inside a java program are the threads of the java process, and a thread cannot run another program's threads. 因此,Java程序中的所有线程都是Java进程的线程,并且一个线程无法运行另一个程序的线程。

A shell script is ran by a program : the shell program ! Shell脚本由程序运行:shell程序! (/bin/bash or /bin/sh) (/ bin / bash或/ bin / sh)

Anyway a shell script will mostly ran other programs inside several other processes. 无论如何,shell脚本通常会在其他几个进程中运行其他程序。

No, you cannot run a shell inside a thread of java. 不,您不能在Java线程中运行Shell。

In general, if you have code that is separate from your Java program, such as code that is in a separate script, then there is no justification for why your code would execute an outside script when that code could be instead integrated into the program. 通常,如果您有与Java程序分离的代码(例如在单独脚本中的代码),则没有理由说明当该代码可以集成到程序中时,为什么您的代码将执行外部脚本。 It is insecure at best. 充其量是不安全的。 Your basically allowing arbitrary code to be executed by your program since the outside script is editable. 因为外部脚本是可编辑的,所以您基本上允许程序执行任意代码。 What you are doing sounds to me almost like it should be confined either a unit test or a build task. 您正在做的事情对我来说听起来几乎应该只限于单元测试或构建任务。

As a unit test task and you could use a threaded JUnit runner to run your outside script during the test phase of your project. 作为单元测试任务,您可以在项目的测试阶段使用线程化的JUnit运行程序运行外部脚本。

Also, separately from your program, you could also execute it using a Gradle task and by using the parallellforks option that Gradle has. 另外,与程序分开,您还可以使用Gradle任务和Gradle具有的parallellforks选项来执行它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM