/엔지니어/Linux / Shell/systemd 서비스 관리 완전 가이드
Linux / Shell초급ubuntucentosdebiansystemdsystemctljournalctl

systemd 서비스 관리 완전 가이드

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

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)

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