Kick ShellScript on the server from Java

Preface

I got a dump of the table by kicking the ShellScript on the server with an additional function I've never done it in Java, so I'll write it down I used this person as a reference Here, execute the ShellScript of RenumberData.sh in / home / dev / sample / work / sh /

Execution environment

java version "1.8.0_181"
Java(TM) SE Runtime Environment (build 1.8.0_181-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)

call

yobidashi.java


CallProcess sh = new CallProcess();
sh.doBackupTable("RenumberData");

CallProcess

CallProcess.java


package mr.yappy;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import sample.core.log.Logger;

/**
 *Shell script execution processing class
 * @param shName Execution shell name
 * @author yappy
 * @version 1.0.0
 */
public class CallProcess {

    private Logger log = Logger.getLogger("aaa");

    public void doBackupTable(String shName) {
        Process process = null;
        InputStream inputS = null;
        InputStreamReader isReader = null;
        BufferedReader bReader = null;
        String text = "";

        try {
            //Specify Path in the server
            String shPath = "/home/dev/sample/work/sh/";
            ProcessBuilder pb = new ProcessBuilder("sh", shPath + shName + ".sh");
            // /home/dev/sample/work/sh/RenumberData.run sh
            process = pb.start();

            //inputS = process.getInputStream();
            inputS = process.getErrorStream();
            //isr = new InputStreamReader(inputS, "Shift-JIS");
            isReader = new InputStreamReader(inputS, "UTF-8");
            bReader = new BufferedReader(isReader);

            StringBuilder builder = new StringBuilder();
            String c = "";

            while ((c = bReader.readLine()) != null) {
                //Writing execution results
                builder.append(c);
            }
            //Execution result
            text = builder.toString();
            //End
            process.waitFor();
            //Get processing status
            int ret = process.exitValue();
            if (ret == 0) {
                //Successful completion
            } else {
                //Abnormal termination
                log.error("error result : " + text);
            }

        } catch (IOException | InterruptedException e) {
            log.error(e,e);
        } finally {
            //Various closes
            if (process != null) {
                process.destroy();
            }
            if (inputS != null) {
                try {
                    inputS.close();
                } catch (IOException e) {
                    log.error(e,e);
                }
            }
            if (isReader != null) {
                try {
                    isReader.close();
                } catch (IOException e) {
                    log.error(e,e);
                }
            }
            if (bReader != null) {
                try {
                    bReader.close();
                } catch (IOException e) {
                    log.error(e,e);
                }
            }
        }
    }
}

Recommended Posts

Kick ShellScript on the server from Java
[Java] How to retrieve the parameters passed from html on the server side
Verify the ID token obtained from Firebase on the server side (Java + SpringBoot)
Using the database (SQL Server 2014) from a Java program 2018/01/04
JSON in Java and Jackson Part 1 Return JSON from the server
Consideration on the 2017 Java Persistence Framework (1)
The road from JavaScript to Java
Install the memcached plugin on MySQL and access it from Java
My thoughts on the equals method (Java)
Calling java from C ++ on Android NDK
Install OpenJDK (Java) on the latest Ubuntu
Hit the Salesforce REST API from Java
Looking back on the basics of Java
Get history from Zabbix server in Java
Execute Java code stored on the clipboard.
Building Java Web Applications on Windows Server 2016
Build Web Application Server (Java) on VPS
Using JUnit from the command line on Ubuntu
Place Java project using database on the server created in VPS so far
[Java] Set the time from the browser with jsoup
Migrate from Java to Server Side Kotlin + Spring-boot
Try calling the CORBA service from Spring (Java)
Ssh login to the app server on heroku
Try accessing the dataset from Java using JZOS
Compile and run Java on the command line
For Java engineers starting Kotlin from now on
I touched on the new features of Java 15
Try communication using gRPC on Android + Java server
Minecraft BE server development from PHP to Java
[Java] Memo on how to write the source
[Android development] Get an image from the server in Java and set it in ImageView! !!
[Java] I want to calculate the difference from the date
How to write Scala from the perspective of Java
[Java] How to extract the file name from the path
To connect from Spring to MySQL on virtual server (unsolved)
Deploy Java apps on the IBM Cloud Kubernetes service
Java language from the perspective of Kotlin and C #
Install the latest hardware drivers from ELRepo on CentOS
Try calling IBM Watson Assistant 2018-07-10 from the Java SDK.
I tried using Log4j2 on a Java EE server
HTTPS connection with Java to the self-signed certificate server
[Android] Uploading images from your device to the server
Notes on updating Ubuntu from the command line (16.04 → 18.04, 18.04 → 20.04 both)
(Ruby on Rails6) Reflecting the posted content from the form
New topics from the Java SE 11 Programmer II exam
Pessimistic lock on NW ABAP from Java via JCo
Call Java from JRuby
Let's touch on Java
Changes from Java 8 to Java 11
Sum from Java_1 to 100
Eval Java source from Java
Run PostgreSQL on Java
Access API.AI from Java
From Java to Ruby !!
JPS (Java Server pages)
Try switching from the RHEL environment to the Ubuntu environment Server installation
The key to running Docker on Raspberry Pi 4 (Ubuntu server 20.04)
[JDBC] I tried to access the SQLite3 database from Java.
20190803_Java & k8s on Azure The story of going to the festival
Catch up on new features from Java 7 to Java 9 at once
Apache Geode-Easy way to execute logic on the server side