C.W.K.
Stream
Lesson 05 of 10 · published

Background 실행과 nohup

~8 min · nohup, disown, background

Level 0창 구경꾼
0 XP0/95 lessons0/14 achievements
0/100 XP to next level100 XP to go0% complete

터미널 닫아도 살아남기

일반 백그라운드 잡 (./script &) 은 부모 shell 종료 시 죽음 — shell 이 SIGHUP 보냄. shell 종료 후에도 살리는 세 방법:

  • nohup ./script & — no-hangup 으로 시작. SIGHUP 무시. 출력은 현재 디렉터리의 nohup.out.
  • ./script & disown — 시작 후 shell 의 job 테이블에서 최근 잡 detach.
  • setsid ./script & — 새 세션에서 시작 (controlling terminal 과 완전 분리).

스트림 잊지 말기

스크립트가 원래 stdout/stderr (터미널로 가는 파이프) 에 계속 쓰면 그 터미널이 닫힐 때 SIGPIPE 받음. 항상 redirect:

nohup ./script > out.log 2>&1 &

장기 실행은 다른 도구로

다시 돌아올 거면: tmux new -s build './script.sh' + 나중에 tmux attach -t build. 세션이 pseudo-terminal 을 유지.

진짜 서비스는 macOS launchd / Linux systemd. Pippa 스택도 launchd 로 pippa serve 유지.

백그라운드 잡 살아 있는지

kill -0 PID && echo alive
ps -p PID

kill -0 는 시그널 0 — '실제 실행 말고 가능한지만 확인'. 프로세스 존재 시 0 반환.

Code

안정적 detached 실행·bash
nohup ./long-job.sh > job.log 2>&1 &
echo $!         # save the PID
disown          # remove from this shell's table
# now exit the terminal — job lives on

External links

Exercise

가짜 장기 잡: nohup sh -c 'for i in $(seq 1 30); do echo tick $i; sleep 1; done' > /tmp/tick.log 2>&1 &. $! 로 PID. 터미널 닫고 새 거 열어 cat /tmp/tick.log — 계속 돌고 있었던 거 확인.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.