CentOS6 までは Linux 起動時の最初に動く "PID が 1 のプロセス"は init でしたが、
CentOS7 系からは systemd となっています。(systemctl コマンドは、"systemd" をコントロールするコマンドです。)
よって、Debian系、RedHat系ともに、systemctl コマンドを使うのが主流のようです)
サービス管理コマンドsystemctlを使います。
これは、/lib/systemdにある設定ファイルを指定して、コマンドを実行するもの
pi@raspberrypi:~ $ systemctl is-active vsftpd.service active pi@raspberrypi:~ $サービスは、Unit という単位のファイル(拡張子は.service)で管理され、以下はその例(network-select.service)です。
[Unit] Description=Network mode selector based on DIP switch DefaultDependencies=no After=local-fs.target Before=network.target dhcpcd.service hostapd.service dnsmasq.service [Service] Type=oneshot ExecStart=/usr/bin/python3 /usr/local/bin/network-select.py RemainAfterExit=yes [Install] WantedBy=multi-user.target上記は、systemd のユニットファイルで、Linux起動時に特定のPythonスクリプト(network-select.py)を実行して、 DIPスイッチの状態に応じてネットワークモードを選択するサービスを定義しています。
Unit という単位のサービスは、次のsystemctlコマンドで制御できます。
| 操作 | コマンド |
|---|---|
| サービス起動 | 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 |
| Description= | サービスの簡単な説明。 |
| Documentation= | 関連するドキュメントのURLなど。 |
| Requires= | 起動前に必ず起動している必要があるユニット。これが失敗するとこのユニットも失敗します。 |
| After= | 起動順序を指定。このユニットは、指定したユニットの後に起動されます。 |
| Before= | 起動順序を指定。このユニットは、指定したユニットの前に起動されます。 |
| Wants= | 推奨される依存関係。このユニットが起動されるときに指定したユニットも起動されますが、指定したユニットが失敗してもこのユニットの起動には影響しません。 |
| ExecStart= | サービスを起動するために実行するコマンド。有効化で必須の設定項目 |
| Type= | サービスの起動タイプ。一般に以下の設定があります。推奨の設定項目 simple: ExecStartで指定したプロセスがメインプロセスです。(最も一般的) forking: ExecStartが終了した後、子プロセスがバックグラウンドで実行され続けます。(古いデーモン形式) oneshot: コマンドを実行して終了したら、サービスも終了と見なされます。 |
| ExecStop= | サービスを停止するために実行するコマンド。(オプション) |
| ExecReload= | サービスを再読み込みするために実行するコマンド。(オプション) |
| Restart= | サービスが予期せず終了した場合の再起動関連の指定で、always, on-failure, no などを記述。 |
| User= | サービスを実行するユーザー名を記述するが、セキュリティ上、root以外を推奨します。 |
| Group= | サービスを実行するグループ名を記述するが、セキュリティ上、root以外を推奨します。 |
| WorkingDirectory= | サービスが実行される作業ディレクトリ。 |
| RemainAfterExit= | サービスが終了しても「アクティブな状態」として systemd に残るようにする場合にyesを指定します。 特に Type=oneshot のユニットと組み合わせて使われることが多いです。 Type=oneshot のサービスはコマンドを一度実行してすぐ終了します。 終了すると systemd はそのサービスを「inactive(停止)」とみなします。 これを「このサービスは一度実行すれば完了したとみなして、アクティブ扱いにしておきたい」という場合に指定します。 |
| WantedBy= | サービスが有効化されるときに、どのターゲット(実行レベルのようなもの)に組み込まれるかを指定。 multi-user.target が一般的です。(通常のサーバー環境) |
| network-pre.target | ネットワーク構成前に実行したいサービス用で、一般にネットワーク設定前のAfter=で指定する。 |
| network.target | ネットワーク関連ユニットの起動後のタイミングに指定する場合に使う。但し疎通保証がないタイミング |
| network-online.target | ネットワーク疎通が可能になった後(DHCP完了・リンク確立後)のタイミングの指定で使う。 |
| local-fs.target | ローカルファイルシステムのマウントが完了したことを示す同期ポイント。 |
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
network={
ssid="000740E60153"
psk=3b648a0a819f13f67b5bafd6366a00726fb364aae5415a76fd099bbef8596ad0
}
# A sample configuration for dhcpcd. # See dhcpcd.conf(5) for details. # Allow users of this group to interact with dhcpcd via the control socket. #controlgroup wheel # Inform the DHCP server of our hostname for DDNS. hostname # Use the hardware address of the interface for the Client ID. clientid # or # Use the same DUID + IAID as set in DHCPv6 for DHCPv4 ClientID as per RFC4361. # Some non-RFC compliant DHCP servers do not reply with this set. # In this case, comment out duid and enable clientid above. #duid # Persist interface configuration when dhcpcd exits. persistent # Rapid commit support. # Safe to enable by default because it requires the equivalent option set # on the server to actually work. option rapid_commit # A list of options to request from the DHCP server. option domain_name_servers, domain_name, domain_search, host_name option classless_static_routes # Respect the network MTU. This is applied to DHCP routes. option interface_mtu # Most distributions have NTP support. #option ntp_servers # A ServerID is required by RFC2131. require dhcp_server_identifier # Generate SLAAC address using the Hardware Address of the interface #slaac hwaddr # OR generate Stable Private IPv6 Addresses based from the DUID slaac private # Example static IP configuration: #interface eth0 #static ip_address=192.168.0.10/24 #static ip6_address=fd51:42f8:caae:d92e::ff/64 #static routers=192.168.0.1 #static domain_name_servers=192.168.0.1 8.8.8.8 fd51:42f8:caae:d92e::1 # It is possible to fall back to a static IP if DHCP fails:上記の解説
| #controlgroup wheel | wheel グループのユーザーに、dhcpcd の制御ソケットを通じて操作を許可する設定。コメントアウトされているので無効 |
| hostname | DHCP サーバーに対して、自分のホスト名を通知する。DDNS(動的DNS更新)に使われる。 |
| clientid | DHCP クライアントIDとして、インターフェイスのMACアドレスを使用する。 |
| #duid | ClientID には DHCPv6 で設定されているものと同じ DUID + IAID を使用します。(非RFC準拠のサーバーでは応答しない場合がある) |
| persistent | dhcpcd が終了しても、インターフェイスの設定(IPアドレスなど)を保持する。 |
| option rapid_commit | DHCPの「Rapid Commit」機能を有効化。サーバー側も対応している場合、通常の2段階ハンドシェイクを省略して高速にIPを取得できる。 |
| option domain_name_servers, domain_name, domain_search, host_name | DHCPサーバーから要求するオプションの一覧。DNSサーバー、ドメイン名、検索ドメイン、ホスト名など。 |
| option classless_static_routes | DHCPサーバーからクラスレス静的ルート情報を受け取る。 |
| option interface_mtu | ネットワークMTU(最大転送単位)を尊重する。 |
| #option ntp_servers | NTPサーバー情報を要求するオプション。コメントアウトされているので無効。 |
| require dhcp_server_identifier | RFC2131に従い、DHCPサーバー識別子(ServerID)が必須であることを指定。 |
| #slaac hwaddr | MACアドレスベースで生成される。無効 |
| slaac private | IPv6アドレスを生成する際、DUIDに基づいた「安定したプライベートアドレス」を使用する。 |
| #interface eth0 | 有線LANインターフェイス eth0 を対象にする 無効指定 |
| #static ip_address=192.168.0.10/24 | IPv4 の固定IPを指定 |
| #static ip6_address=fd51:42f8:caae:d92e::ff/64 | IPv6 の固定IPを指定 |
| #static routers=192.168.0.1 | デフォルトゲートウェイを指定 |
| #static domain_name_servers=192.168.0.1 8.8.8.8 fd51:42f8:caae:d92e::1 | DNSサーバーを指定(複数可) |
| # It is possible to fall back to a static IP if DHCP fails: | DHCPでIPが取得できなかった場合に、静的IPへフォールバックすることも可能する指定 無効 |
# --- WiFi AP mode static IP settings --- interface wlan0 static ip_address=192.168.4.1/24 nohook wpa_supplicant
# ========== HostAPD default configuration ========== interface=wlan0 driver=nl80211 # AP の SSID(任意に変更してください) ssid=RasPi3P-AP hw_mode=g channel=6 # 国コード(必ず設定) country_code=JP # 出力を抑えて省電力化 ieee80211n=1 ieee80211d=1 ignore_broadcast_ssid=0 # セキュリティ wpa=2 wpa_key_mgmt=WPA-PSK rsn_pairwise=CCMP # AP のパスワード(8文字以上) wpa_passphrase=abcd1234 # 自動再起動 logger_syslog=0 logger_syslog_level=4/etc/dnsmasq.confの設定ファイルを以下に示します。(個別設定は/etc/dnsmasq.d/に置きます)
# ---------------------------- # dnsmasq for Wi-Fi AP (wlan0) # ---------------------------- interface=wlan0 # AP が使うインターフェイス bind-interfaces # 他のインターフェイスにバインドしない # DHCP で配布する IP レンジ(例: 192.168.4.x) dhcp-range=192.168.4.50,192.168.4.150,255.255.255.0,24h # AP のゲートウェイは AP 自身 (固定IP) dhcp-option=3,192.168.4.1 # DNS サーバ(Raspberry Pi 自身) dhcp-option=6,192.168.4.1 # ホスト名を配布 dhcp-authoritative
一方、serviceは、/etc/init.dにあるシェルスクリプトのファイルを指定することで、シェルを実行してくれるものがある。
pi@raspberrypi:~ $ sudo service dnsmasq stopstop,start,status,restart などが使える。
どちらも使える状態であれば、systemctlを使うの方が簡単で、 systemctlは標準でenableと言うコマンドが入っており、systemctl enable mysqlとすることでOSが起動した時に自動でコマンドを実行してくれるようになる