Spring boot project execution script automatically pull the latest code and restart the instance content

Time:2021-1-14

gitPullThenRestart.sh

#Date: 20191230
#Author: he Pengju
#Note: the project is deployed on Alibaba cloud, so it's troublesome to compile, package and upload every time. Therefore, this script can be used to restart the application automatically by clicking the button
#Note: 1) the plug-ins of alicloud also need to be uploaded to OSS and then started every time. The uploaded jar packages are relatively large (most of them are dependent on third parties), which makes it feel bad
#2) it is not necessary to use Jenkins to build a set of Jenkins for a project

#In the following code, / root / hekele is the project path of GIT clone, and / root / app is the deployment path


#Refresh environment variables
source ~/.bashrc

#Switch to git project directory, pull the latest code, and package MVN
cd /root/hekele
git pull

cd /root/hekele/java
mvn clean install


#Kill the progress of the current project
ps -ef|grep hekele.jar | awk '{print $2}' | while read pid
do
 kill -9 $pid
done

#Copy the jar package and force it to be covered (ignore the reminder). Since the - I option of CP is opened in. Bashrc of CentOS, you can use - F, so you'd better delete it first and then copy it
rm -f /root/app/hekele.jar
cp /root/hekele/java/target/hekele.jar /root/app/

#Background startup project
nohup java -jar /root/app/hekele.jar >> /root/app/hekele.log 2>&1 &

#Check the log (Note: after the kill, the jar package was copied and started or executed. The reason is unknown for the moment... This sentence is available for manual startup, but it has no effect on automatic restart)
tail -f /root/app/hekele.log

JavaController

@Apioperation ("pull the latest code and restart the application")
  @GetMapping("gitPullThenRestart")
  public R gitPullThenRestart() {
    //A new thread must be opened to return to the browser, otherwise the browser will not receive a response and will send it again
    new Thread(() -> ExecUtil.execCommand(restartShell)).start();
    Return r.ok(). Seterrmsg ("restarting, please wait a moment");
  }

JavaExecUtil

/**
 *Execute system commands
 * 
 * 
 *< br > pay attention to deadlock
 *< br > solution: as long as the main process can continuously process the data in the buffer before waitfor
 * 
 * @see <a href=" https://blog.csdn.net/seapeak007/article/details/69668600 "Rel =" external nofollow "> call Process.waitfor Causes the process to hang</a>
 * 
 * @author he_pe
 *
 */
@Slf4j
public class ExecUtil {

  public static void execCommand(String command) {
    log.info("begin exec os command: " + command);
    Process process;
    try {
      process = Runtime.getRuntime().exec(command);
      
      //Open a new thread to handle normal output and error output
      newThreadLogInputStream(process.getInputStream());
      newThreadLogInputStream(process.getErrorStream());

      //Block the current process until the end of the command
      process.waitFor();

      //Will not block the process, but if the call is not completed, an error will be reported
      if (process.exitValue() != 0) {
        log.error("exec os command failure: " + command);
      } else {
        log.info("exec os command success: " + command);
      }
    } catch (Exception e) {
      log.error(e.getMessage(), e);
    }
  }

  private static void newThreadLogInputStream(InputStream is){
    new Thread(() -> {
      try(InputStream inputStream = is;
        BufferedReader bufr = new BufferedReader(new InputStreamReader(inputStream))){
        String out = null;
        while ((out = bufr.readLine()) != null) {
          log.info(out);
        }
      } catch (IOException e) {
      }
    }).start();
  }

}

The above is all the code content of this knowledge point. Thank you for your learning and support for developer.