| この記事の要点 |
|
Linux のサービス管理
Web サーバー (nginx/apache)、DB (MySQL/PostgreSQL)、SSH、Docker など、Linux 上で常駐するプログラムは「サービス (Service)」または「デーモン (Daemon)」と呼ばれます。Linux の世界では、これらを起動・停止・自動起動設定するための仕組みが歴史的に変遷してきました。
| 方式 | 主な採用ディストリ | 状態 |
|---|---|---|
| systemd | Ubuntu 16.04+, RHEL/CentOS 7+, Debian 8+ | 現在の主流 |
| SysVinit | RHEL/CentOS 5/6, 古い Debian | レガシー |
| Upstart | Ubuntu 9.10〜15.04 | 廃止 |
| OpenRC | Gentoo, 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.targetsudo 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.service | A の後に起動 (依存はしない、順序だけ) |
Before=B.service | B の前に起動 |
Requires=A.service | A が必須 (A 失敗時は自分も失敗) |
Wants=A.service | A を試みるが失敗してもよい |
Conflicts=A.service | A と同時起動不可 |
PartOf=A.service | A が止まれば自分も止まる |
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 を設定。
子ページ
子ページはありません
同階層のページ
- 起動・停止(Linux)
- 起動・停止
人気ページ
- 1 Eclipseで「サーバーに追加または除去できるリソースがありません。」の原因と対処法
- 2 tomcat の起動 / 停止ログと catalina.log・catalina.out の違い
- 3 JavaScript で base URL を取得する方法|window.location.origin
- 4 YouTube Data API v3 エラー一覧|403・400・404 の原因と対処
- 5 Laravel エラー一覧|500/Blade/DB 接続/ルーティングの代表エラー
- 6 3Dグラフィックスとは|モデリング/レンダリング/主要ソフトウェア (Blender / Maya)
- 7 Spring Frameworkのアノテーション一覧
- 8 【Spring】@Valueアノテーションとは
- 9 CATALINA_HOME の確認方法 (Linux / Mac)
- 10 【Spring】@Autowiredアノテーションとは
最近更新/作成されたページ
- Git pushした変更の取消し|revert・履歴変更と保護の注意点 2026-10-15 23:49:01
- Gitでローカルを戻す前に|reset・restoreの違いと退避範囲 2026-10-15 23:49:01
- Git rebaseの使い方|対象範囲・競合解決・pushの判断 2026-10-15 22:50:59
- Git pullの使い方|取込み先・ff-only・競合とstashの注意点 2026-10-15 22:50:59
- Git pushの使い方|送信先・タグ・force-with-leaseの条件 2026-10-15 22:12:47
- Git remoteの設定・確認・削除|SSH URLとfetch/pushの違い 2026-10-15 22:12:47
- Gitリモートリポジトリの作成|初回push・既存履歴・mirrorの注意 2026-10-15 15:48:16
- Gitのuser.name・user.email設定|local/global・確認と過去履歴の注意 2026-10-15 15:30:07
- LinuxにGitをインストール|配布元・初期設定・TLSと認証の確認 2026-10-15 15:30:06
- Git導入と初回push|Linuxサーバー・Windows/macOS・SSH設定 2026-10-15 14:51:54
- Db2のCREATE INDEX|索引定義・INCLUDEと実行計画の確認 2026-10-15 14:10:22
- Db2の自動採番|IDENTITY・ID取得とRESTARTの注意点 2026-10-15 14:10:22
- 【DB2】特定スキーマの全テーブルの全カラム情報を取得する方法 2026-10-15 13:53:15
- Db2のシーケンスとIDENTITYの違い|採番とカタログの確認SQL 2026-10-15 13:53:15
- Db2で複数カラムをUPDATEする方法|SET・WHEREと更新前の値 2026-10-15 13:53:15