1.

Linux サービスの起動・停止完全ガイド — systemd と SysVinit

編集
この記事の要点
  • 現在の Linux は systemd が主流: systemctl start/stop/restart/status nginx
  • 自動起動の有効化/無効化: systemctl enable nginx / systemctl disable nginx
  • 稼働状態確認: is-active / is-enabled、ログは journalctl -u nginx -f
  • 古いシステム (CentOS 6 等) は SysVinit: service nginx start / /etc/init.d/nginx start
  • 定期実行は systemd timer (cron の置換) / 起動順序は After= / Requires= で制御

Linux のサービス管理

Web サーバー (nginx/apache)、DB (MySQL/PostgreSQL)、SSH、Docker など、Linux 上で常駐するプログラムは「サービス (Service)」または「デーモン (Daemon)」と呼ばれます。Linux の世界では、これらを起動・停止・自動起動設定するための仕組みが歴史的に変遷してきました。

方式主な採用ディストリ状態
systemdUbuntu 16.04+, RHEL/CentOS 7+, Debian 8+現在の主流
SysVinitRHEL/CentOS 5/6, 古い Debianレガシー
UpstartUbuntu 9.10〜15.04廃止
OpenRCGentoo, Alpine Linux軽量代替

systemd: 基本操作

# 起動
sudo systemctl start nginx

# 停止
sudo systemctl stop nginx

# 再起動 (停止 → 起動)
sudo systemctl restart nginx

# 再読み込み (PID を変えずに設定リロード、対応しているサービスのみ)
sudo systemctl reload nginx

# 状態確認
systemctl status nginx
# ● nginx.service - A high performance web server
#      Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset)
#      Active: active (running) since Mon 2026-05-15 10:00:00 JST; 2h ago
#    Main PID: 1234 (nginx)
#       Tasks: 5
#      Memory: 12.3M
#         CPU: 1.234s

# 設定リロード後に再起動
sudo systemctl reload-or-restart nginx

自動起動 (enable / disable)

# 起動時に自動起動するよう設定
sudo systemctl enable nginx
# → /etc/systemd/system/multi-user.target.wants/nginx.service へのリンクが作成

# enable と同時に start
sudo systemctl enable --now nginx

# 自動起動を解除
sudo systemctl disable nginx
sudo systemctl disable --now nginx    # stop も同時に

# 完全に無効化 (他サービスから依存されても起動しない)
sudo systemctl mask nginx
# 解除
sudo systemctl unmask nginx

状態を機械可読に取る

# 稼働中か (active / inactive / failed)
systemctl is-active nginx
# active

# 自動起動が有効か
systemctl is-enabled nginx
# enabled / disabled / masked / static

# 失敗したか
systemctl is-failed nginx

# シェルスクリプトでの分岐
if ! systemctl is-active --quiet nginx; then
    echo "nginx is down!"
    sudo systemctl restart nginx
fi

サービス一覧と検索

# 稼働中のサービス一覧
systemctl list-units --type=service

# 全 unit 一覧
systemctl list-units --all

# 自動起動が有効なもの
systemctl list-unit-files --state=enabled

# サービスを名前で検索
systemctl list-units --type=service | grep nginx

# 失敗しているサービスだけ
systemctl --failed

journalctl: systemd のログ

# nginx のログ
journalctl -u nginx

# リアルタイム監視 (tail -f 相当)
journalctl -u nginx -f

# 直近 1 時間
journalctl -u nginx --since '1 hour ago'

# 期間指定
journalctl -u nginx --since '2026-05-15 10:00' --until '2026-05-15 12:00'

# 直近 100 行を逆順
journalctl -u nginx -n 100 --reverse

# エラーレベル以上
journalctl -u nginx -p err

# 起動以降 (再起動後の動作確認)
journalctl -u nginx -b

# JSON 出力
journalctl -u nginx -o json

SysVinit (古い CentOS 6 / Ubuntu 14.04 以前)

# service コマンド (推奨インタフェース)
sudo service nginx start
sudo service nginx stop
sudo service nginx restart
sudo service nginx status

# /etc/init.d/ 直接実行
sudo /etc/init.d/nginx start
sudo /etc/init.d/nginx restart

# 自動起動の制御
sudo chkconfig nginx on     # RHEL系
sudo chkconfig nginx off
sudo update-rc.d nginx defaults     # Debian系
sudo update-rc.d -f nginx remove

# 現在の自動起動状態
chkconfig --list nginx

systemd unit ファイルの基本

# /etc/systemd/system/myapp.service
[Unit]
Description=My Application
After=network.target mysql.service        # この後に起動
Requires=mysql.service                    # mysql 起動失敗時は自分も失敗
Wants=redis.service                       # redis 起動を試みるが失敗しても続行

[Service]
Type=simple                # simple / forking / oneshot / notify
User=www-data
Group=www-data
WorkingDirectory=/var/www/myapp
ExecStart=/usr/bin/python3 /var/www/myapp/app.py
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure
RestartSec=5s
StandardOutput=journal
StandardError=journal

[Install]
WantedBy=multi-user.target

反映方法:

# unit ファイル変更後
sudo systemctl daemon-reload

# 起動 + 自動起動
sudo systemctl enable --now myapp

# 状態確認
systemctl status myapp

systemd timer (cron の代替)

# /etc/systemd/system/backup.service
[Unit]
Description=Daily backup
[Service]
Type=oneshot
ExecStart=/usr/local/bin/backup.sh

# /etc/systemd/system/backup.timer
[Unit]
Description=Run backup daily at 3:00
[Timer]
OnCalendar=*-*-* 03:00:00
Persistent=true
[Install]
WantedBy=timers.target
sudo systemctl enable --now backup.timer
systemctl list-timers
# NEXT                        LEFT          LAST PASSED  UNIT          ACTIVATES
# Tue 2026-05-20 03:00:00 JST 10h left      n/a  n/a     backup.timer  backup.service

起動順序の制御

ディレクティブ意味
After=A.serviceA の後に起動 (依存はしない、順序だけ)
Before=B.serviceB の前に起動
Requires=A.serviceA が必須 (A 失敗時は自分も失敗)
Wants=A.serviceA を試みるが失敗してもよい
Conflicts=A.serviceA と同時起動不可
PartOf=A.serviceA が止まれば自分も止まる

FAQ

Q: nginx の設定変更後、restart と reload どちらを使う?
A: 軽い設定変更なら reload (worker を入れ替えるだけ、無停止)。バイナリ更新時は restart

Q: systemctl が動かない (古い OS)
A: cat /proc/1/comm で init なら SysVinit。service / chkconfig を使う。

Q: クラッシュしたサービスを自動復旧したい
A: unit ファイルに Restart=on-failure + RestartSec=5s を設定。

編集
Post Share
子ページ

子ページはありません

同階層のページ
  1. 起動・停止(Linux)
  2. 起動・停止(macOS)