• Java笔记-使用processBuilder调用shell(Linux)


    背景

    环境是这样的,前端发起一个post请求,里面的信息带有要启动的进程,java后端接收到这个进程名后,使用linux的shell调用指定的程序。

    代码

    相关代码如下:

    1. package xx.xx.xx.service;
    2. import org.springframework.stereotype.Service;
    3. import java.io.IOException;
    4. import java.io.InputStream;
    5. import java.io.InputStreamReader;
    6. import java.util.concurrent.Semaphore;
    7. @Service
    8. public class ProcessService {
    9. static Boolean isSomeProcessRun = false;
    10. Semaphore semaphore = new Semaphore(1);
    11. public String runProcess(String processName){
    12. System.out.println(isSomeProcessRun);
    13. if(isSomeProcessRun){
    14. return "有程序正在计算," + processName + " 无法启动";
    15. }
    16. try {
    17. isSomeProcessRun = true;
    18. semaphore.acquire();
    19. run(processName);
    20. }
    21. catch (InterruptedException e) {
    22. e.printStackTrace();
    23. }
    24. return processName + "后台已调用";
    25. }
    26. protected void run(String processName){
    27. Thread thread = new Thread(){
    28. @Override
    29. public void run() {
    30. ProcessBuilder processBuilder = new ProcessBuilder();
    31. processBuilder.command("/root/program/" + processName);
    32. processBuilder.redirectErrorStream(true);
    33. try {
    34. Process start = processBuilder.start();
    35. InputStream inputStream = start.getInputStream();
    36. InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "gbk");
    37. char[] chs = new char[1024];
    38. int len = -1;
    39. while ((len = inputStreamReader.read(chs)) != -1){
    40. String str = new String(chs, 0, len);
    41. System.out.println(str);
    42. }
    43. inputStreamReader.close();
    44. inputStream.close();
    45. }
    46. catch (IOException e) {
    47. e.printStackTrace();
    48. }
    49. semaphore.release();
    50. isSomeProcessRun = false;
    51. }
    52. };
    53. thread.start();
    54. }
    55. }

    这里有几点要注意的:

    调用shell的关键代码:

    1. Thread thread = new Thread(){
    2. @Override
    3. public void run() {
    4. ProcessBuilder processBuilder = new ProcessBuilder();
    5. processBuilder.command("/root/program/" + processName);
    6. processBuilder.redirectErrorStream(true);
    7. try {
    8. Process start = processBuilder.start();
    9. InputStream inputStream = start.getInputStream();
    10. InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "gbk");
    11. char[] chs = new char[1024];
    12. int len = -1;
    13. while ((len = inputStreamReader.read(chs)) != -1){
    14. String str = new String(chs, 0, len);
    15. System.out.println(str);
    16. }
    17. inputStreamReader.close();
    18. inputStream.close();
    19. }
    20. catch (IOException e) {
    21. e.printStackTrace();
    22. }
    23. semaphore.release();
    24. isSomeProcessRun = false;
    25. }
    26. };

    启动一个进程,让这个进程调用shell,用这种方式来做,比较合理。

  • 相关阅读:
    VMware虚拟机安装ubuntu-16.04.5详细教程
    仪表盘自定义标题和数值样式
    python random模块随机抽样专题
    主流超融合多副本机制缺陷与 SmartX 的临时副本策略
    信息安全工程师软考笔记汇总目录
    Linux 创建的用户无法登录
    【Java】国密SM3/SM4(附工具类)
    javascript基本语法(持续补充)
    【ML】K-Means 聚类
    Java进阶API第二章
  • 原文地址:https://blog.csdn.net/qq78442761/article/details/127841135