OS 운영체제/LINUX
nohup (터미널 끊겨도 proc 진행 시키는 명령어)
by yororing
2024. 4. 29.
01 nohub이란
1. 정의
리눅스에서 프로세스 (process, proc)를 실행한 터미널 (terminal, tty)의 세션 연결이 끊어지더라도 프로세스를 계속해서 동작시키는 명령어
원래 리눅스에서는 터미널에서 ssh 세션 로그아웃(logout) 발생 시 해당 터미널에서 실행한 프로세스들에게 HUP signal 을 전달 하여 종료시키게 되는데 이 HUP signal을 프로세스가 무시(ignore) 하도록 하고 프로세스들을 마치 데몬(daemon)인 것처럼 동작시키는 명령어라서 nohup 이라고 명명
또한 nohup 명령어로 인해 백그라운드에서 실행된 프로세스들의 표준 출력(standard output)은 nohup.out 파일로 재지향(redirection)됨
터미널이 종료되어도 표준 출력은 nohup.out 파일 에 계속해서 기록되기에 프로세스의 상태를 확인하는데 유용
그러나 필요 이상의 로그를 화면에 계속해서 출력하면 nohup.out 파일의 용량이 매우 커지기에 디스크 공간을 낭비 가능
꼭 필요한 로그만 출력하거나 로그가 불필요한 경우 재지향을 통해 nohup.out 파일을 생성하지 않는 것 권장
2. 문법
nohup [Process_NAME] &
nohup ./start.sh &
nohup python3 device-agent.py &
Process_NAME : 실행할 프로그랩/스크립트 이름
실행 대상이 스크립트인 경우, 스크립트의 권한은 755 이상 필수
&
nohup 명령어 사용 시 백그라운드 작업으로 실행하는 경우가 많기에 & 를 붙여 백그라운드 실행이라는 것을 명시
3. 설명
1) 일반적인 리눅스 프로세스 (nohup 미사용)
nohup 미사용 시 , 일반적인 리눅스 프로세스는 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
2) nohup 사용한 리눅스 프로세스
nohup 사용 시 , ssh 세션이 종료되더라도 삭제되지 않고 표준 출력(standard output)은 nohup.out 파일로 재지향(redirection)됨
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.out 파일을 생성하지 않으려면 표준출력과 표준에러를 /dev/null로 재지향 해주면 됨\
nohup [프로세스] 1>/dev/null 2>&1 &
3) nohup 사용 후 프로세스 종료하기
nohup으로 실행된 프로세스를 종료 시키려면 다음의 명령어를 사용
ps -aux | grep [Process_NAME]
kill -9 [PID]
ps -ef | grep [Process_NAME]
kill -15 [PID]
kill -9 [PID]
ps -aux | grep [Process_NAME]
kill -9 [PID]
명령어를 사용하여 해당 프로세스에 종료 시그널을 보내서 종료
참조
https://ingnoh.tistory.com/42
https://gracefulprograming.tistory.com/128
공유하기
URL 복사 카카오톡 공유 페이스북 공유 엑스 공유