IT한 것/unix

gradle ssh execute가 종료되지 않을 때

lovian 2021. 4. 14. 12:23

gradle ssh plugin은 아주 많은 기능을 가진 플러그인이다.

단순 ssh client라고 생각했는데,

gradle-ssh-plugin.github.io/docs/

 

Gradle SSH Plugin Document

Groovy SSH is an automation tool which provides SSH facilities such as command execution or file transfer. It is provided as the executable JAR gssh.jar and the library groovy-ssh-x.y.z.jar.

gradle-ssh-plugin.github.io

참고 링크를 따라가면.. 아주 장난 아니다

 

어쨋든 이것을 이용하여 간단하게 개발 모듈을 deploy 할 수 있도록 작업중이다.

ssh remote에 접속해서 서버를 실행시켜야하는데, gradle task가 종료가 되지 않는 문제가 있다.

 

java -jar target.jar

식의 명령을 수행하는데, 서버 프로그램이라 바로 종료가 되지 않는 형태이다.

 

java -jar target.jar &
nohup java -jar target.jar &

아주 다양하게 널리 알려진 방법을 사용했지만, gradle task는 종료되지 않았다.

 

조금 더 알아보니 원론적인 방법이 있었다.

nohup java -jar target.jar > console 2> error < /dev/null &

이 방식을 사용하면 standard output, standard error, standard input 모두를 파일로 처리하게되어 ssh 세션이 문제 없이 종료되어 원하는 결과를 얻을 수 있다.

 

 

원인

아래의 참조 링크에 가면 자세하게 설명되어 있다.

 

백그라운드 프로세스가 남아있는 상태에서 SSH 세션이 종료하면, standard ouput, error가 닫히게 된다.

그렇게 되면 백그라운드 프로세스가 남기는 정보를 아무도 받을 수 없는 상태이므로 정보의 손실이 생긴다.

그 손실을 막기 위해 SSH 세션 종료를 억지로 막고 있는 상황이다.

standard output, error를 파일로 처리하도록 redirection을 걸어주면 정보의 손실이 생기지 않으므로 SSH 세션은 잘 종료된다. 

 

ko.wikipedia.org/wiki/Nohup

 

nohup - 위키백과, 우리 모두의 백과사전

 

ko.wikipedia.org