홈/엔지니어/Linux / Shell/systemd 서비스 관리…
Linux / Shell초급ubuntucentosdebiansystemdsystemctljournalctl

systemd 서비스 관리

systemctl로 서비스를 시작·중지·활성화하고, 직접 서비스 유닛 파일을 작성하는 방법을 단계별로 설명합니다.

목차
  1. systemd란?
  2. 기본 서비스 조작
  3. 서비스 상태 전체 조회
  4. 직접 서비스 유닛 파일 작성
  5. 로그 확인 (journalctl)
  6. 타이머 (Cron 대체)

systemd란?

현대 리눅스 배포판(Ubuntu 22.04+, RHEL/Rocky Linux 8+, Debian 11+)의 표준 init 시스템. 부팅 시 서비스를 병렬 실행하고 의존성을 관리합니다.


기본 서비스 조작

Bash
# 상태 확인
systemctl status nginx

# 시작 / 중지 / 재시작
systemctl start  nginx
systemctl stop   nginx
systemctl restart nginx
systemctl reload nginx      # 설정만 재로드 (다운타임 없음)

# 부팅 시 자동 시작 설정/해제
systemctl enable  nginx
systemctl disable nginx

# 활성화 + 즉시 시작 (한 번에)
systemctl enable --now nginx

서비스 상태 전체 조회

Bash
# 실행 중인 서비스 목록
systemctl list-units --type=service --state=running

# 실패한 서비스 확인
systemctl --failed

# 부팅 시간 분석
systemd-analyze blame | head -20

직접 서비스 유닛 파일 작성

INI
# /etc/systemd/system/myapp.service

[Unit]
Description=My Node.js Application
After=network.target

[Service]
Type=simple
User=deploy
WorkingDirectory=/opt/myapp
ExecStart=/usr/bin/node /opt/myapp/server.js
Restart=on-failure
RestartSec=5
StandardOutput=journal
StandardError=journal
Environment=NODE_ENV=production PORT=3000

[Install]
WantedBy=multi-user.target

작성 후 적용:

Bash
systemctl daemon-reload          # 유닛 파일 변경 후 반드시 실행
systemctl enable --now myapp

로그 확인 (journalctl)

Bash
journalctl -u nginx                    # nginx 서비스 전체 로그
journalctl -u nginx -f                 # 실시간 팔로우
journalctl -u nginx --since "1 hour ago"
journalctl -u nginx -n 50             # 최근 50줄
journalctl -u nginx -p err            # 에러만 출력
journalctl --disk-usage               # 로그 디스크 사용량
journalctl --vacuum-time=7d           # 7일 이상 로그 삭제

타이머 (Cron 대체)

INI
# /etc/systemd/system/backup.timer
[Unit]
Description=Daily Backup Timer

[Timer]
OnCalendar=daily
Persistent=true

[Install]
WantedBy=timers.target
Bash
systemctl enable --now backup.timer
systemctl list-timers
#systemd#systemctl#journalctl#서비스#linux
편집 안내 · Editorial Note

AI 도구가 초안 작성에 사용될 수 있습니다. 명령어의 실행 여부와 검증 환경은 각 가이드에 기재된 근거를 확인하세요. 적용 전 사용 중인 버전의 공식 문서를 확인하고, 오류는 이메일로 알려주세요. 이메일로 제보

주제 관련 공식 문서(자동 연결 · 문장별 근거 아님)GNU/Linux man 페이지 ↗

질문 & 답변 (Q&A)

이 가이드에 대해 궁금한 점을 질문해보세요. 확인 후 답변드립니다.