본문 바로가기
C.W.K.
Stream
Lesson 08 of 10 · published

시스템 서비스: brew services, launchctl

~10 min · services, launchctl, brew

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

Mac과 함께 시작되는 장기 프로세스

웹 서버와 데이터베이스처럼 오래 살아야 하는 프로세스는 시작 시점, 재시작 조건, 로그, 권한을 관리할 주체가 필요해. macOS에서는 launchd가 그 생명주기를 소유해.

  • brew services: Homebrew로 설치한 도구를 launchd에 등록하기 쉽게 감싸.
  • launchctl: 임의의 plist를 직접 다루는 명령이야.

brew services 사용하기

brew install postgresql@16
brew services start postgresql@16
brew services list
brew services stop postgresql@16
brew services restart postgresql@16
# 로그인 자동 시작 없이 현재 실행
brew services run postgresql@16

Homebrew는 보통 사용자 LaunchAgent plist를 ~/Library/LaunchAgents/에 만들고 로드해.

launchctl로 직접 제어하기

# 현재 사용자에게 로드된 작업 확인
launchctl list | grep -v com.apple
# plist 로드
launchctl bootstrap gui/$UID ~/Library/LaunchAgents/com.me.svc.plist
# 작업 언로드
launchctl bootout gui/$UID/com.me.svc
# 로드된 작업 재시작
launchctl kickstart -k gui/$UID/com.me.svc

LaunchAgent와 LaunchDaemon

  • LaunchAgent: 사용자가 로그인한 뒤 그 사용자 권한으로 실행해. ~/Library/LaunchAgents//Library/LaunchAgents/에 선언해.
  • LaunchDaemon: 사용자 로그인 전부터 시스템 권한으로 실행할 수 있어. /Library/LaunchDaemons/에 선언해.

관리자 권한과 로그인 전 시작이 정말 필요한 경우가 아니라면 사용자 Agent가 영향 범위와 복구가 더 단순해.

로그와 종료 사유 확인하기

plist의 StandardOutPathStandardErrorPath로 파일 로그를 정할 수 있어. 통합 로깅을 쓰는 앱도 있으므로 파일뿐 아니라 log show, 로드 상태, 최근 종료 사유를 함께 봐.

서비스 선언이 생명주기를 소유해

plist에는 실행 인자, 작업 디렉터리, 환경, 로그, 재시작 정책, 중지 동작이 드러나야 해. 선언을 바꾼 뒤에는 해당 실행 영역의 bootout·bootstrap 또는 launchctl kickstart -k gui/$UID/com.me.svc를 사용하고, 손으로 중복 프로세스를 띄우지 마. 로드된 작업, PID, 로그, 실제 health를 확인해야 배포가 끝나.

Code

brew services 흐름·bash
brew install redis
brew services start redis
brew services list
redis-cli ping     # 출력: PONG
brew services stop redis

External links

Exercise

Homebrew로 설치한 시험 서비스가 있다면 brew services list로 현재 상태를 읽어. 하나를 시작한 뒤 전용 클라이언트로 응답을 확인하고 정상적으로 중지해. launchctl list에서는 시스템 서비스와 사용자 서비스를 구분해 봐.

Progress

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

댓글 0

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

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