DevShivmohan / Learning-everything

Learning for developer only
0 stars 1 forks source link

Remote server common commands #8

Open DevShivmohan opened 1 year ago

DevShivmohan commented 1 year ago
DevShivmohan commented 1 year ago

If server gives an error like this

image

then run command with sudo like this sudo mvn spring-boot:run & then this issue has been fixed.

DevShivmohan commented 1 year ago

Kill process with indentified port 8080 [Linux system]

private void killProcessViaPort(){
        try {
            String pid=null;
            Process process=Runtime.getRuntime().exec("netstat -ltnup | grep ':8080'");
            var scanner=new Scanner(process.getInputStream());
            while (scanner.hasNext()){
                var readline=scanner.nextLine();
                System.out.println(readline);
                if(readline.contains(":8080")){
                    StringBuilder stringBuilder=new StringBuilder();
                    for(char ch:readline.substring(readline.indexOf("LISTEN      "),readline.lastIndexOf("/java")).toCharArray())
                        if((""+ch).matches("[0-9]+"))
                            stringBuilder.append(ch);
                    pid=stringBuilder.toString();
                    scanner.close();
                    break;
                }
            }
            log.info("killing process with PID "+pid);
            if(pid!=null && !pid.isBlank()){
                process=Runtime.getRuntime().exec("kill -SIGKILL "+pid);
                process.waitFor();
                log.info("Process killed");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }