LoginSignup
752
722

More than 5 years have passed since last update.

systemctl コマンド

Last updated at Posted at 2017-12-08

systemctl

この記事は Linux コマンド 全部オレ Advent Calendar 2017 の8日目の記事です。

NAME
       systemctl - Control the systemd system and service manager

SYNOPSIS
       systemctl [OPTIONS...] COMMAND [NAME...]

DESCRIPTION
       systemctl may be used to introspect and control the state of the "systemd" system and service manager. Please refer to systemd(1) for an introduction into the basic concepts
       and functionality this tool manages.

これまでサービス起動デーモンとして、SysVinit/Upstart が使われてきたが Red Hat Enterprise Linux 7 や CentOS 7 からは systemd が使われるようになり、それをコントロールするためのコマンド。
サービスは Unit という単位で作られる。

コマンド群

操作 コマンド
サービス起動 systemctl start ${Unit}
サービス停止 systemctl stop ${Unit}
サービス再起動 systemctl restart ${Unit}
サービスリロード systemctl reload ${Unit}
サービスステータス表示 systemctl status ${Unit}
サービス自動起動有効 systemctl enable ${Unit}
サービス自動起動無効 systemctl disable ${Unit}
サービス自動起動設定確認 systemctl is-enabled ${Unit}
サービス一覧 systemctl list-unit-files --type=service
設定ファイルの再読込 systemctl daemon-reload

サービスを作る

  1. サービスのユニット定義ファイルを作る
  2. ユニット定義ファイルを再読込
  3. 起動
## 動かすサービスを作る
$ sudo vim /usr/local/bin/hello_world.sh
$ cat /usr/local/bin/hello_world.sh
#!/bin/sh

while :
do
    echo "Hello, World. (PID=$(echo $$), DATE=$(date))"
    sleep 30
done

## ユニット定義ファイルを作る
$ sudo vim /etc/systemd/system/hello_world.service
$ cat /etc/systemd/system/hello_world.service
[Unit]
Description=hello_world
After=network.service

[Service]
Type=simple
Restart=on-success
ExecStart=/usr/local/bin/hello_world.sh

[Install]
WantedBy=multi-user.target

## 起動
$ sudo systemctl start hello_world

## 自動起動の設定
$ sudo systemctl enable hello_world
Created symlink from /etc/systemd/system/multi-user.target.wants/hello_world.service to /etc/systemd/system/hello_world.service.

ログの確認

systemd によって起動された Unit のログは、/var/log/journal 以下にバイナリ形式で保存されるようになっています。そのため、ログの確認には journalctl コマンドを利用する。
-ftail -f 的な動きになる。

## tail -f 的な動きをさせる。
$ sudo journalctl -f -u hello_world
-- Logs begin at Fri 2017-11-24 12:20:20 JST. --
Dec 08 12:59:40 test hello_world.sh[27804]: Hello, World. (PID=27804, DATE=Fri Dec  8 12:59:40 JST 2017)
Dec 08 13:00:10 test hello_world.sh[27804]: Hello, World. (PID=27804, DATE=Fri Dec  8 13:00:10 JST 2017)
Dec 08 13:00:40 test hello_world.sh[27804]: Hello, World. (PID=27804, DATE=Fri Dec  8 13:00:40 JST 2017)
Dec 08 13:01:10 test hello_world.sh[27804]: Hello, World. (PID=27804, DATE=Fri Dec  8 13:01:10 JST 2017)
Dec 08 13:01:40 test hello_world.sh[27804]: Hello, World. (PID=27804, DATE=Fri Dec  8 13:01:40 JST 2017)
Dec 08 13:02:10 test hello_world.sh[27804]: Hello, World. (PID=27804, DATE=Fri Dec  8 13:02:10 JST 2017)

## pager(less) をやめる。
$ sudo journalctl --no-pager -u hello_world
752
722
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
752
722