티스토리 뷰

Linux.

[Linux] Nohup이란?

인쥭 2021. 2. 15. 20:19
반응형

참고 : 다음의 링크에서 더 자세한 내용을 확인하실 수 있습니다.

 

[Linux] nohup 세션이 끊겨도 계속 실행되도록 해보자

개요 nohup 명령어는 리눅스에서 프로세스를 실행한 터미널의 세션 연결이 끊어지더라도 지속적으로 동작 할 수 있게 해주는 명령어입니다. 기본적으로 터미널에서 세션 로그아웃(logout)이 발생

gracefulprograming.tistory.com

  • nohup은 프로세스를 실행한 터미널의 세션 연결이 끊기더라도 프로세스를 계속해서 동작시키는 명령어이다.
  • 기본적으로 터미널에서 세션의 logout이 발생하면, 해당 터미널에서 실행된 프로세스들에게 HUP 신호를 전달하여 종료시킨다.
  • nohup은 프로세스들을 마치 데몬인 것처럼 동작시켜 이러한 HUP 시그널을 무시하도록 하는 명령어가 된다.

 

요약. nohup은 다음과 같이 사용할 수 있다.

[~] nohup [Process_NAME] &
  • process name에는 실행할 프로그램 또는 스크립트 이름을 넣어준다.
    • 실행할 대상이 스크립트인 경우, 스크립트의 권한은 755 이상이어야 한다고 함.
  • 예시 : 
    • nohup ./start.sh &
    • nohup python3 device-agent.py &

 

상세. 일반적인 리눅스 프로세스는 SSH 세션이 종료될 경우 백그라운드 여부와 관계 없이 종료된다.

ubuntu@ip-10-0-0-21:~/tunneling-poc$ python3 device-agent.py &
[1] 9780
ubuntu@ip-10-0-0-21:~/tunneling-poc$ ps -aux | grep python3
root       751  0.0  2.5 170832 12308 ?        Ssl  10:43   0:00 /usr/bin/python3 /usr/bin/networkd-dispatcher --run-startup-triggers
root       801  0.0  3.1 187680 15200 ?        Ssl  10:43   0:00 /usr/bin/python3 /usr/share/unattended-upgrades/unattended-upgrade-shutdown --wait-for-signal
ubuntu    9781  2.5  3.8 290976 18504 pts/0    Sl   10:56   0:00 python3 device-agent.py
ubuntu    9789  0.0  0.2  14860  1112 pts/0    S+   10:56   0:00 grep --color=auto python3
ubuntu@ip-10-0-0-21:~/tunneling-poc$ exit
logout
Connection to 0.0.0.0 closed.
[~] ssh ubuntu@0.0.0.0

ubuntu@ip-10-0-0-21:~$ ps -aux | grep python3
root       751  0.0  2.5 170832 12308 ?        Ssl  10:43   0:00 /usr/bin/python3 /usr/bin/networkd-dispatcher --run-startup-triggers
root       801  0.0  3.1 187680 15200 ?        Ssl  10:43   0:00 /usr/bin/python3 /usr/share/unattended-upgrades/unattended-upgrade-shutdown --wait-for-signal
ubuntu    9875  0.0  0.2  14860  1080 pts/1    S+   10:56   0:00 grep --color=auto python3

 

nohup을 사용할 경우 ssh 세션이 종료되더라도 삭제되지 않도록할 수 있다.

  • 백그라운드로 실행한 nohup 프로세스의 output은 nohup.out으로 리다이렉션된다.
ubuntu@ip-10-0-0-21:~/tunneling-poc$ nohup python3 device-agent.py &
[1] 9882
ubuntu@ip-10-0-0-21:~/tunneling-poc$ nohup: ignoring input and appending output to 'nohup.out'

ubuntu@ip-10-0-0-21:~/tunneling-poc$ ps -aux | grep python3
root       751  0.0  2.5 170832 12308 ?        Ssl  10:43   0:00 /usr/bin/python3 /usr/bin/networkd-dispatcher --run-startup-triggers
root       801  0.0  3.1 187680 15200 ?        Ssl  10:43   0:00 /usr/bin/python3 /usr/share/unattended-upgrades/unattended-upgrade-shutdown --wait-for-signal
ubuntu    9883  1.4  3.7 290980 18416 pts/1    Sl   10:57   0:00 python3 device-agent.py
ubuntu    9891  0.0  0.2  14860  1044 pts/1    S+   10:58   0:00 grep --color=auto python3
ubuntu@ip-10-0-0-21:~/tunneling-poc$ exit
logout
Connection to 0.0.0.0 closed.
[~] ssh ubuntu@0.0.0.0
ubuntu@ip-10-0-0-21:~$ ps -aux | grep python3
root       751  0.0  2.5 170832 12308 ?        Ssl  10:43   0:00 /usr/bin/python3 /usr/bin/networkd-dispatcher --run-startup-triggers
root       801  0.0  3.1 187680 15200 ?        Ssl  10:43   0:00 /usr/bin/python3 /usr/share/unattended-upgrades/unattended-upgrade-shutdown --wait-for-signal
ubuntu    9883  0.8  3.7 290980 18416 ?        Sl   10:57   0:00 python3 device-agent.py
ubuntu    9977  0.0  0.2  14860  1112 pts/0    S+   10:58   0:00 grep --color=auto python3
ubuntu@ip-10-0-0-21:~$ cd tunneling-poc
ubuntu@ip-10-0-0-21:~/tunneling-poc$ cat nohup.out
2021-02-15 10:52:46,763 - MqttCore initialized
2021-02-15 10:52:46,763 - Client id: iot_tunnel_ingnoh
2021-02-15 10:52:46,763 - Protocol version: MQTTv3.1.1
2021-02-15 10:52:46,763 - Authentication type: TLSv1.2 certificate based Mutual Auth.
2021-02-15 10:52:46,763 - Configuring endpoint...
2021-02-15 10:52:46,763 - Configuring alpn protocols...
2021-02-15 10:52:46,763 - Configuring certificates...
2021-02-15 10:52:46,764 - Performing sync connect...
2021-02-15 10:52:46,764 - Performing async connect...
2021-02-15 10:52:46,764 - Keep-alive: 600.000000 sec
2021-02-15 10:52:46,838 - Performing sync subscribe...
2021-02-15 10:52:56,880 - Performing sync subscribe...
2021-02-15 10:53:06,926 - Performing sync subscribe...

 

nohup으로 실행된 프로세스를 종료시키려면 다음의 명령어를 사용한다.

  • ps -aux | grep [Process_NAME]
  • kill -9 [PID]
댓글
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함