본문 바로가기
강의 실습/비전공자도 이해할 수 있는 리눅스 입문 실전

백그라운드에서 실행되고 있는 프로세스의 로그 확인하는 법 (nohup)

by jint 2025. 11. 24.

1. 백그라운드에서 실행되고 있는 프로세스의 로그 확인하는 법
1) 이전에 실행시킨 Spring Boot 종료
- Ubuntu

ubuntu@ip-172-31-39-75:~$ ps aux | grep java
ubuntu    378982  0.1 14.9 2785484 140184 ?      Sl   Nov23   3:11 java -jar linux-springboot-0.0.1-SNAPSHOT.jar
ubuntu    386534  0.0  0.2   7076  2104 pts/0    S+   10:07   0:00 grep --color=auto java
ubuntu@ip-172-31-39-75:~$ kill 378982
ubuntu@ip-172-31-39-75:~$ ps aux | grep java
ubuntu    386542  0.0  0.2   7076  2204 pts/0    S+   10:09   0:00 grep --color=auto java


2) 포그라운드에서 Spring Boot 실행
- Ubuntu

ubuntu@ip-172-31-39-75:~$ cd linux-springboot/build/libs
ubuntu@ip-172-31-39-75:~/linux-springboot/build/libs$ ls
app.log  linux-springboot-0.0.1-SNAPSHOT-plain.jar  linux-springboot-0.0.1-SNAPSHOT.jar  nohup.out
ubuntu@ip-172-31-39-75:~/linux-springboot/build/libs$ java -jar linux-springboot-0.0.1-SNAPSHOT.jar

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/

 :: Spring Boot ::                (v3.5.0)

2025-11-24T10:10:57.740Z  INFO 386552 --- [linux-springboot] [           main] c.e.l.LinuxSpringbootApplication         : Starting LinuxSpringbootApplication v0.0.1-SNAPSHOT using Java 17.0.17 with PID 386552 (/home/ubuntu/linux-springboot/build/libs/linux-springboot-0.0.1-SNAPSHOT.jar started by ubuntu in /home/ubuntu/linux-springboot/build/libs)
2025-11-24T10:10:57.749Z  INFO 386552 --- [linux-springboot] [           main] c.e.l.LinuxSpringbootApplication         : No active profile set, falling back to 1 default profile: "default"
2025-11-24T10:10:59.711Z  INFO 386552 --- [linux-springboot] [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port 8080 (http)
2025-11-24T10:10:59.744Z  INFO 386552 --- [linux-springboot] [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2025-11-24T10:10:59.745Z  INFO 386552 --- [linux-springboot] [           main] o.apache.catalina.core.StandardEngine    : Starting Servlet engine: [Apache Tomcat/10.1.41]
2025-11-24T10:10:59.791Z  INFO 386552 --- [linux-springboot] [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2025-11-24T10:10:59.793Z  INFO 386552 --- [linux-springboot] [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1888 ms
2025-11-24T10:11:00.447Z  INFO 386552 --- [linux-springboot] [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port 8080 (http) with context path '/'
2025-11-24T10:11:00.492Z  INFO 386552 --- [linux-springboot] [           main] c.e.l.LinuxSpringbootApplication         : Started LinuxSpringbootApplication in 3.725 seconds (process running for 4.773)


포그라운드에서 Spring Boot 를 실행시키면 Spring Boot 가 뱉어내는 로그를 실시간으로 확인할 수 있다. 이 로그를 보면서 Spring Boot 가 정상적으로 실행된 건지 안 된 건지 직접 확인할 수 있다.

3) 백그라운드에서 Spring Boot 실행
- Ubuntu

^C2025-11-24T10:14:16.870Z  INFO 386552 --- [linux-springboot] [ionShutdownHook] o.s.b.w.e.tomcat.GracefulShutdown        : Commencing graceful shutdown. Waiting for active requests to complete
2025-11-24T10:14:16.885Z  INFO 386552 --- [linux-springboot] [tomcat-shutdown] o.s.b.w.e.tomcat.GracefulShutdown        : Graceful shutdown complete
ubuntu@ip-172-31-39-75:~/linux-springboot/build/libs$ nohup java -jar linux-springboot-0.0.1-SNAPSHOT.jar &
[1] 386593
ubuntu@ip-172-31-39-75:~/linux-springboot/build/libs$ nohup: ignoring input and appending output to 'nohup.out'


백그라운드에서 Spring Boot 실행시 실시간으로 로그가 찍히지 않는다.
Spring Boot 가 뱉어내는 로그를 확인하려면 어떻게 해야 할까?
nohup 명령어로 프로그램을 실행시키면 nohup.out 이라는 파일에 로그가 쌓이게끔 작동한다. nohup 명령어 입력한 뒤 찍히는 출력값은 프로세스에서 발생한 출력값을 nohup.out 이라는 파일에 추가하여 저장했다는 의미이다.

4) nohup.out 파일 확인
확실한 테스트를 위해 기존 실행시키던 Spring Boot 서버를 종료시키고 기존 nohup.out 파일을 삭제한 뒤에 확인한다.

- Ubuntu

ubuntu@ip-172-31-39-75:~/linux-springboot/build/libs$ ps aux | grep java
ubuntu    386593  2.4 14.1 2785592 132552 pts/0  Sl   10:14   0:09 java -jar linux-springboot-0.0.1-SNAPSHOT.jar
ubuntu    386639  0.0  0.2   7076  2204 pts/0    S+   10:21   0:00 grep --color=auto java
ubuntu@ip-172-31-39-75:~/linux-springboot/build/libs$ kill 386593
ubuntu@ip-172-31-39-75:~/linux-springboot/build/libs$ ps aux | grep java
ubuntu    386647  0.0  0.2   7076  2108 pts/0    S+   10:22   0:00 grep --color=auto java
[1]+  Exit 143                nohup java -jar linux-springboot-0.0.1-SNAPSHOT.jar
ubuntu@ip-172-31-39-75:~/linux-springboot/build/libs$ ps aux | grep java
ubuntu    386649  0.0  0.2   7076  2204 pts/0    S+   10:23   0:00 grep --color=auto java
ubuntu@ip-172-31-39-75:~/linux-springboot/build/libs$ ls
app.log  linux-springboot-0.0.1-SNAPSHOT-plain.jar  linux-springboot-0.0.1-SNAPSHOT.jar  nohup.out
ubuntu@ip-172-31-39-75:~/linux-springboot/build/libs$ rm -rf nohup.out
ubuntu@ip-172-31-39-75:~/linux-springboot/build/libs$ ls
app.log  linux-springboot-0.0.1-SNAPSHOT-plain.jar  linux-springboot-0.0.1-SNAPSHOT.jar
ubuntu@ip-172-31-39-75:~/linux-springboot/build/libs$ nohup java -jar linux-springboot-0.0.1-SNAPSHOT.jar &
[1] 386662
ubuntu@ip-172-31-39-75:~/linux-springboot/build/libs$ nohup: ignoring input and appending output to 'nohup.out'

ubuntu@ip-172-31-39-75:~/linux-springboot/build/libs$ ls
app.log  linux-springboot-0.0.1-SNAPSHOT-plain.jar  linux-springboot-0.0.1-SNAPSHOT.jar  nohup.out
ubuntu@ip-172-31-39-75:~/linux-springboot/build/libs$ cat nohup.out

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/

 :: Spring Boot ::                (v3.5.0)

2025-11-24T10:26:02.785Z  INFO 386662 --- [linux-springboot] [           main] c.e.l.LinuxSpringbootApplication         : Starting LinuxSpringbootApplication v0.0.1-SNAPSHOT using Java 17.0.17 with PID 386662 (/home/ubuntu/linux-springboot/build/libs/linux-springboot-0.0.1-SNAPSHOT.jar started by ubuntu in /home/ubuntu/linux-springboot/build/libs)
2025-11-24T10:26:02.794Z  INFO 386662 --- [linux-springboot] [           main] c.e.l.LinuxSpringbootApplication         : No active profile set, falling back to 1 default profile: "default"
2025-11-24T10:26:04.591Z  INFO 386662 --- [linux-springboot] [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port 8080 (http)
2025-11-24T10:26:04.618Z  INFO 386662 --- [linux-springboot] [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2025-11-24T10:26:04.618Z  INFO 386662 --- [linux-springboot] [           main] o.apache.catalina.core.StandardEngine    : Starting Servlet engine: [Apache Tomcat/10.1.41]
2025-11-24T10:26:04.675Z  INFO 386662 --- [linux-springboot] [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2025-11-24T10:26:04.679Z  INFO 386662 --- [linux-springboot] [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1706 ms
2025-11-24T10:26:05.339Z  INFO 386662 --- [linux-springboot] [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port 8080 (http) with context path '/'
2025-11-24T10:26:05.363Z  INFO 386662 --- [linux-springboot] [           main] c.e.l.LinuxSpringbootApplication         : Started LinuxSpringbootApplication in 3.66 seconds (process running for 4.459)


nohup.out 파일을 확인하면 백그라운드에서 실행된 프로세스의 로그가 파일에 잘 기록된 것을 확인할 수 있다. 


2. nohup.out 파일이 아닌 다른 파일에 로그가 남도록 만들기
- Ubuntu

ubuntu@ip-172-31-39-75:~/linux-springboot/build/libs$ rm -rf nohup.out
ubuntu@ip-172-31-39-75:~/linux-springboot/build/libs$ nohup java -jar linux-springboot-0.0.1-SNAPSHOT.jar >> result.log 2>&1 &
[2] 386710
ubuntu@ip-172-31-39-75:~/linux-springboot/build/libs$ ls
app.log  linux-springboot-0.0.1-SNAPSHOT-plain.jar  linux-springboot-0.0.1-SNAPSHOT.jar  result.log
[2]+  Exit 1                  nohup java -jar linux-springboot-0.0.1-SNAPSHOT.jar >> result.log 2>&1
ubuntu@ip-172-31-39-75:~/linux-springboot/build/libs$ ls
app.log  linux-springboot-0.0.1-SNAPSHOT-plain.jar  linux-springboot-0.0.1-SNAPSHOT.jar  result.log
ubuntu@ip-172-31-39-75:~/linux-springboot/build/libs$ cat result.log
nohup: ignoring input

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/

 :: Spring Boot ::                (v3.5.0)

2025-11-24T10:33:01.944Z  INFO 386710 --- [linux-springboot] [           main] c.e.l.LinuxSpringbootApplication         : Starting LinuxSpringbootApplication v0.0.1-SNAPSHOT using Java 17.0.17 with PID 386710 (/home/ubuntu/linux-springboot/build/libs/linux-springboot-0.0.1-SNAPSHOT.jar started by ubuntu in /home/ubuntu/linux-springboot/build/libs)
2025-11-24T10:33:01.976Z  INFO 386710 --- [linux-springboot] [           main] c.e.l.LinuxSpringbootApplication         : No active profile set, falling back to 1 default profile: "default"
2025-11-24T10:33:03.796Z  INFO 386710 --- [linux-springboot] [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port 8080 (http)
2025-11-24T10:33:03.824Z  INFO 386710 --- [linux-springboot] [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2025-11-24T10:33:03.824Z  INFO 386710 --- [linux-springboot] [           main] o.apache.catalina.core.StandardEngine    : Starting Servlet engine: [Apache Tomcat/10.1.41]
2025-11-24T10:33:03.876Z  INFO 386710 --- [linux-springboot] [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2025-11-24T10:33:03.881Z  INFO 386710 --- [linux-springboot] [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1737 ms
2025-11-24T10:33:04.525Z  WARN 386710 --- [linux-springboot] [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Failed to start bean 'webServerStartStop'
2025-11-24T10:33:04.552Z  INFO 386710 --- [linux-springboot] [           main] .s.b.a.l.ConditionEvaluationReportLogger : 

Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2025-11-24T10:33:04.582Z ERROR 386710 --- [linux-springboot] [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Web server failed to start. Port 8080 was already in use.

Action:

Identify and stop the process that's listening on port 8080 or configure this application to listen on another port.

ubuntu@ip-172-31-39-75:~/linux-springboot/build/libs$ ls
app.log  linux-springboot-0.0.1-SNAPSHOT-plain.jar  linux-springboot-0.0.1-SNAPSHOT.jar  result.log


확실한 테스트를 위해 기존 nohup.out 파일을 삭제한 뒤, Spring Boot 프로세스에서 발생한 로그(표준 출력과 표준 에러 출력)를 result.log 파일에 출력한다.
nohup java -jar linux-springboot-0.0.1-SNAPSHOT.jar >> result.log 2>&1 & 명령어에서 > 대신에 >> 을 쓴 이유는, 파일을 매번 새로 덮어쓰는 게 아닌 기존 파일에 이어서 로그를 쌓아가게끔 만들려고 하기 때문이다.

result.log 파일을 확인하면 정상적으로 로그가 저장된 것을 확인할 수 있다. 표준 출력과 표준 에러 출력의 리다이렉션 기능을 활용해 프로세스에서 발생하는 출력값(로그)을 파일로 저장하도록 구현했다.
nohup.out 파일도 생기지 않은 것을 보면 result.log 파일에 로그를 남기도록 명령어가 잘 실행된 것도 확인할 수 있다.


참고링크 : https://www.inflearn.com/course/%EB%B9%84%EC%A0%84%EA%B3%B5%EC%9E%90%EB%8F%84-%EC%9D%B4%ED%95%B4%ED%95%A0-%EC%88%98-%EC%9E%88%EB%8A%94-%EB%A6%AC%EB%88%85%EC%8A%A4-%EC%9E%85

 

비전공자도 이해할 수 있는 리눅스 입문/실전| JSCODE 박재성 - 인프런 강의

현재 평점 5.0점 수강생 295명인 강의를 만나보세요. 비전공자 입장에서도 쉽게 이해할 수 있고, 실전에서 바로 적용 가능한 '리눅스 입문' 강의를 만들어봤습니다! 리눅스를 처음 배우시는 분, Per

www.inflearn.com

댓글