环境是这样的,前端发起一个post请求,里面的信息带有要启动的进程,java后端接收到这个进程名后,使用linux的shell调用指定的程序。
相关代码如下:
- package xx.xx.xx.service;
-
- import org.springframework.stereotype.Service;
-
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.util.concurrent.Semaphore;
-
- @Service
- public class ProcessService {
-
- static Boolean isSomeProcessRun = false;
- Semaphore semaphore = new Semaphore(1);
-
- public String runProcess(String processName){
-
- System.out.println(isSomeProcessRun);
- if(isSomeProcessRun){
-
- return "有程序正在计算," + processName + " 无法启动";
- }
-
- try {
-
- isSomeProcessRun = true;
- semaphore.acquire();
- run(processName);
- }
- catch (InterruptedException e) {
-
- e.printStackTrace();
- }
-
- return processName + "后台已调用";
- }
-
- protected void run(String processName){
-
- Thread thread = new Thread(){
-
- @Override
- public void run() {
-
- ProcessBuilder processBuilder = new ProcessBuilder();
- processBuilder.command("/root/program/" + processName);
- processBuilder.redirectErrorStream(true);
- try {
-
- Process start = processBuilder.start();
- InputStream inputStream = start.getInputStream();
- InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "gbk");
-
- char[] chs = new char[1024];
- int len = -1;
- while ((len = inputStreamReader.read(chs)) != -1){
-
- String str = new String(chs, 0, len);
- System.out.println(str);
- }
-
- inputStreamReader.close();
- inputStream.close();
- }
- catch (IOException e) {
-
- e.printStackTrace();
- }
-
- semaphore.release();
- isSomeProcessRun = false;
- }
- };
- thread.start();
- }
- }
这里有几点要注意的:
调用shell的关键代码:
- Thread thread = new Thread(){
-
- @Override
- public void run() {
-
- ProcessBuilder processBuilder = new ProcessBuilder();
- processBuilder.command("/root/program/" + processName);
- processBuilder.redirectErrorStream(true);
- try {
-
- Process start = processBuilder.start();
- InputStream inputStream = start.getInputStream();
- InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "gbk");
-
- char[] chs = new char[1024];
- int len = -1;
- while ((len = inputStreamReader.read(chs)) != -1){
-
- String str = new String(chs, 0, len);
- System.out.println(str);
- }
-
- inputStreamReader.close();
- inputStream.close();
- }
- catch (IOException e) {
-
- e.printStackTrace();
- }
-
- semaphore.release();
- isSomeProcessRun = false;
- }
- };
启动一个进程,让这个进程调用shell,用这种方式来做,比较合理。