unix - コマンドリファレンス - システムサービスの制御 service

クラウディア 
1. service システムサービスの制御
2.  一覧表示
3. -R 再起動
4.  ちょっとだけ応用

1. service システムサービスの制御

 Linux 系に service というコマンドがありますが、ここではそれとよく似た FreeBSD の service コマンドについて述べます。  FreeBSD には service コマンドはないと思い込んでいたのですが、Webmin をアップデートしたときに

after installing Webmin for the first time you should perform the following
steps as root:

* Configure Webmin by running /usr/local/lib/webmin/setup.sh

* Add webmin_enable="YES" to your /etc/rc.conf

* Start Webmin for the first time by running "service webmin start"

The parameters requested by setup.sh may then be changed from within Webmin
itself.
 というメッセージを読んで「はて・・・?」と思ったのです。  man で見てみると FreeBSD 7.3 より登場したようです。  基本形は、以下の形です。

> service スクリプト コマンド
 スクリプトは、下記のディレクトリにあるものが対象になります。

/etc/rc.d/
/usr/local/etc/rc.d/
 とても納得なのでありまして、いままで

> /usr/local/etc/rc.d/apache24 configtest
> /usr/local/etc/rc.d/apache24 restart
 としていたのが

> service apache24 configtest
> service apache24 restart
 になるわけです。  apache24 はありがたみが少ないですが、/etc/rc.d/ と /usr/local/etc/rc.d/ を使い分けなくて済むのがとてもありがたい。

2. 一覧表示

 スクリプトなし、-vl オプションで、どこにどういうスクリプトがあるかを一覧表示します。

> service -vl
From /etc/rc.d:
abi
accounting

	・・・

zfsd
zvol
From /usr/local/etc/rc.d:
apache24
clamav-clamd

	・・・
unbound
webmin
 スクリプトなし、-e オプションで

/etc/defaults/rc.conf
/etc/rc.conf
 で enable を設定されることにより有効となっているスクリプトを一覧表示します。

> service -e
/etc/rc.d/hostid
/etc/rc.d/hostid_save
/etc/rc.d/cleanvar
/etc/rc.d/ip6addrctl
/etc/rc.d/netif
/etc/rc.d/devd
/etc/rc.d/newsyslog
/etc/rc.d/syslogd
/etc/rc.d/savecore
/etc/rc.d/dmesg
/etc/rc.d/virecover
/etc/rc.d/motd
/etc/rc.d/ntpd
/etc/rc.d/rctl
/usr/local/etc/rc.d/webmin
/usr/local/etc/rc.d/unbound
/etc/rc.d/sshd
/usr/local/etc/rc.d/postgresql
/usr/local/etc/rc.d/nsd
/usr/local/etc/rc.d/mysql-server
/usr/local/etc/rc.d/clamav-clamd
/usr/local/etc/rc.d/clamav-freshclam
/usr/local/etc/rc.d/apache24
/etc/rc.d/sendmail
/etc/rc.d/cron
/etc/rc.d/mixer
/etc/rc.d/gptboot
/etc/rc.d/bgfsck

3. -R 再起動

 スクリプトなし、-R オプションで、/etc/rc.conf で現在有効となっているサービスのすべてを再起動します。

> service -R
Stopping apache24.
Waiting for PIDS: 2022.
Stopping clamav_freshclam.
Waiting for PIDS: 2017.
Stopping clamav_clamd.
Waiting for PIDS: 2014.
Stopping mysql.
Waiting for PIDS: 2008.
nsd not running? (check /var/nsd/var/run/nsd/nsd.pid).
Stopping unbound.
Waiting for PIDS: 92868.
webmin not running? (check /var/log/webmin/miniserv.pid).
Starting webmin.
Pre-loaded WebminCore
Obtaining a trust anchor:.
Starting unbound.
2018-01-24 01:52:24 JST LOG:  database system was shut down at 2018-01-24 01:52:16 JST
2018-01-24 01:52:24 JST LOG:  MultiXact member wraparound protections are now enabled
2018-01-24 01:52:24 JST LOG:  database system is ready to accept connections
2018-01-24 01:52:24 JST LOG:  autovacuum launcher started
Starting nsd.
/usr/sbin/service: WARNING: failed to start nsd
Starting mysql.
Starting clamav_clamd.
Starting clamav_freshclam.
Performing sanity check on apache24 configuration:
Syntax OK
Starting apache24.

4. ちょっとだけ応用

 ntpd を除けば、わたしがさわるのは /usr/local/etc/rc.d の配下ばかりです。  /usr/local/etc/rc.d の下の有効なスクリプトを表示するには grep と組み合わせて

> service -e | grep /usr
/usr/local/etc/rc.d/webmin
/usr/local/etc/rc.d/unbound
/usr/local/etc/rc.d/postgresql
/usr/local/etc/rc.d/nsd
/usr/local/etc/rc.d/mysql-server
/usr/local/etc/rc.d/clamav-clamd
/usr/local/etc/rc.d/clamav-freshclam
/usr/local/etc/rc.d/apache24
ハイスピードプラン5G CONNECT