テックブログ

技術ネタ

自動起動設定について

お久しぶりです。mnakamuraです。
前回はSwapファイルの作成についての記事でしたが、
今回のブログのテーマはサービスの自動起動設定についてです。
CentOS6系で利用されているchkconfigコマンドでサービスやアプリケーションの起動を
簡略化する事が出来るのです。サーバを再起動するたびに手動でサービスを立ち上げるの、
面倒ですよね?
最近のクラウドベンダーでもスペック変更を実施する際にサーバ再起動が必要になる事はありますし、
不意の障害でサーバを再起動しなければならない事もままあります。
まずは現在のランレベルを確認しましょう。

-----------------------------------------------------------------------------------------------------
[root@mnakamura ~]# runlevel
N 3

-----------------------------------------------------------------------------------------------------
現在のランレベルは3ですね。
因みに各ランレベルの状態は下記の通りです。

-----------------------------------------------------------------------------------------------------
0:システムの停止
1:シングルユーザモード
2:マルチユーザモード
3:マルチユーザモード(コンソールログイン)
4:未使用
5:マルチユーザモード(ディスプレイマネージャ使用)
6:システム再起動

-----------------------------------------------------------------------------------------------------

chkconfig --listで各サービスのランレベル毎の設定を確認出来ますが、
ここではhttpdのみをgrepで指定して確認します。
-----------------------------------------------------------------------------------------------------
[root@mnakamura ~]# chkconfig --list | grep httpd
httpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off

-----------------------------------------------------------------------------------------------------
2、3、4、5のレベルでonになっていますね。
この設定であれば、現在と同じ3でサーバを立ち上げた際、
手動でサービスを起動しなくても、自動的にhttpdが起動するのです。
この設定をoffにしたいときは下記のコマンドを実行します。

-----------------------------------------------------------------------------------------------------
[root@mnakamura ~]# chkconfig httpd off
-----------------------------------------------------------------------------------------------------
すると…

-----------------------------------------------------------------------------------------------------
[root@mnakamura ~]# chkconfig --list | grep httpd
httpd 0:off 1:off 2:off 3:off 4:off 5:off 6:off


-----------------------------------------------------------------------------------------------------
全面的にoffになりました。今度はonにしてみましょう。
-----------------------------------------------------------------------------------------------------
[root@mnakamura ~]# chkconfig httpd on
[root@mnakamura ~]# chkconfig --list | grep httpd
httpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off

-----------------------------------------------------------------------------------------------------
元に戻りましたね。
因みに、さっき紹介した通り、レベル4は未使用です。
止めても問題が無いので、下記のようにレベルを指定してピンポイントにoffにしてみましょう。

-----------------------------------------------------------------------------------------------------
[root@mnakamura ~]# chkconfig --level 4 httpd off
-----------------------------------------------------------------------------------------------------
すると…

-----------------------------------------------------------------------------------------------------
[root@mnakamura ~]# chkconfig --list | grep httpd
httpd 0:off 1:off 2:on 3:on 4:off 5:on 6:off

-----------------------------------------------------------------------------------------------------

御覧の通り、レベル4だけoffに出来ました。
grepしなければ一覧を一気に表記出来ます。

-----------------------------------------------------------------------------------------------------
[root@mnakamura ~]# chkconfig --list
auditd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
blk-availability 0:off 1:off 2:off 3:off 4:off 5:off 6:off
crond 0:off 1:off 2:on 3:on 4:on 5:on 6:off
dovecot 0:off 1:off 2:on 3:on 4:on 5:on 6:off
htcacheclean 0:off 1:off 2:off 3:off 4:off 5:off 6:off
httpd 0:off 1:off 2:on 3:on 4:off 5:on 6:off
ip6tables 0:off 1:off 2:off 3:off 4:off 5:off 6:off
iptables 0:off 1:off 2:on 3:on 4:on 5:on 6:off
lvm2-monitor 0:off 1:off 2:off 3:off 4:off 5:off 6:off
messagebus 0:off 1:off 2:on 3:on 4:on 5:on 6:off
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
named 0:off 1:off 2:on 3:on 4:on 5:on 6:off
netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
netfs 0:off 1:off 2:off 3:off 4:off 5:off 6:off
network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
nginx 0:off 1:off 2:on 3:on 4:on 5:on 6:off
nginx-debug 0:off 1:off 2:off 3:off 4:off 5:off 6:off
nrpe 0:off 1:off 2:on 3:on 4:on 5:on 6:off
ntpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
ntpdate 0:off 1:off 2:off 3:off 4:off 5:off 6:off
portreserve 0:off 1:off 2:off 3:off 4:off 5:off 6:off
postfix 0:off 1:off 2:on 3:on 4:on 5:on 6:off
rdisc 0:off 1:off 2:off 3:off 4:off 5:off 6:off
restorecond 0:off 1:off 2:off 3:off 4:off 5:off 6:off
rsyslog 0:off 1:off 2:on 3:on 4:on 5:on 6:off
saslauthd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
snmpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
snmptrapd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
sshd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
sysstat 0:off 1:on 2:on 3:on 4:on 5:on 6:off
udev-post 0:off 1:off 2:off 3:off 4:off 5:off 6:off
vnstat 0:off 1:off 2:off 3:off 4:off 5:off 6:off
vsftpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
xinetd 0:off 1:off 2:off 3:on 4:on 5:on 6:off
zabbix-agent 0:off 1:off 2:on 3:on 4:on 5:on 6:off
xinetd ベースのサービス:
chargen-dgram: off
chargen-stream: off
daytime-dgram: off
daytime-stream: off
discard-dgram: off
discard-stream: off
echo-dgram: off
echo-stream: off
nrpe: off
rsync: off
tcpmux-server: off
telnet: off
time-dgram: off
time-stream: off

-----------------------------------------------------------------------------------------------------
更に、不要なサービスはそもそもリストから除外する事も出来ます。
上記の内、この検証サーバではnetfsを利用していません。

-----------------------------------------------------------------------------------------------------
[root@mnakamura ~]# chkconfig --list | grep netfs
netfs 0:off 1:off 2:off 3:off 4:off 5:off 6:off

-----------------------------------------------------------------------------------------------------
こちらのサービスの実体は/etc/init.d/の配下にあります。

-----------------------------------------------------------------------------------------------------
[root@mnakamura ~]# ll -A /etc/init.d/
合計 232
-rwxr-xr-x 1 root root 3580 3月 22 19:05 2017 auditd
-r-xr-xr-x 1 root root 1362 11月 16 00:36 2017 blk-availability
-rwxr-xr-x. 1 root root 2826 8月 24 03:36 2016 crond
-rwxr-xr-x 1 root root 2015 1月 9 03:14 2016 dovecot
-rw-r--r-- 1 root root 25592 4月 27 19:58 2018 functions
-rwxr-xr-x 1 root root 5985 4月 27 19:58 2018 halt
-rwxr-xr-x 1 root root 2001 6月 20 00:45 2018 htcacheclean
-rwxr-xr-x 1 root root 3488 6月 20 00:45 2018 httpd
-rwxr-xr-x 1 root root 11244 6月 20 01:08 2018 ip6tables
-rwxr-xr-x 1 root root 11123 6月 20 01:08 2018 iptables
-rwxr-xr-x 1 root root 652 4月 27 19:58 2018 killall
-r-xr-xr-x 1 root root 2137 11月 16 00:36 2017 lvm2-lvmetad
-r-xr-xr-x 1 root root 3045 11月 16 00:36 2017 lvm2-monitor
-rwxr-xr-x 1 root root 2200 6月 20 00:59 2018 messagebus
-rwxr-xr-x 1 root root 7026 1月 27 07:25 2017 mysqld
-rwxr-xr-x 1 root root 7992 8月 28 00:39 2018 named
-rwxr-xr-x 1 root root 4334 4月 27 19:58 2018 netconsole
-rwxr-xr-x 1 root root 5309 4月 27 19:58 2018 netfs
-rwxr-xr-x 1 root root 6742 4月 27 19:58 2018 network
-rwxr-xr-x 1 root root 3615 12月 5 00:02 2018 nginx
-rwxr-xr-x 1 root root 3646 12月 5 00:02 2018 nginx-debug
-rwxr-xr-x 1 root root 1178 3月 20 23:11 2013 nrpe
-rwxr-xr-x 1 root root 1923 12月 11 18:22 2018 ntpd
-rwxr-xr-x 1 root root 2043 12月 11 18:22 2018 ntpdate
-rwxr-xr-x 1 root root 2023 5月 11 08:01 2016 portreserve
-rwxr-xr-x 1 root root 3912 3月 24 01:43 2017 postfix
-rwxr-xr-x 1 root root 1513 12月 7 22:11 2016 rdisc
-rwxr-xr-x. 1 root root 1822 11月 19 00:30 2016 restorecond
-rwxr-xr-x 1 root root 2011 12月 1 23:14 2017 rsyslog
-rwxr-xr-x. 1 root root 1698 11月 19 00:30 2016 sandbox
-rwxr-xr-x. 1 root root 2056 2月 28 00:57 2015 saslauthd
-rwxr-xr-x 1 root root 647 4月 27 19:58 2018 single
-rwxr-xr-x 1 root root 2162 3月 22 20:45 2017 snmpd
-rwxr-xr-x 1 root root 1738 3月 22 20:45 2017 snmptrapd
-rwxr-xr-x 1 root root 4621 9月 1 02:40 2017 sshd
-rwxr-xr-x 1 root root 1144 1月 26 16:43 2018 sysstat
-rwxr-xr-x. 1 root root 2294 9月 6 23:51 2016 udev-post
-rwxr-xr-x 1 root root 1439 5月 19 04:34 2011 vnstat
-rwxr-xr-x 1 root root 3478 3月 22 21:14 2017 vsftpd
-rwxr-xr-x 1 root root 3555 5月 11 03:36 2016 xinetd
-rwxr-xr-x 1 root root 1722 11月 8 03:10 2017 zabbix-agent
[root@mnakamura ~]# ll -A /etc/init.d/ | grep netfs
-rwxr-xr-x 1 root root 5309 4月 27 19:58 2018 netfs

-----------------------------------------------------------------------------------------------------
実体さえあれば、追加や削除は下記のように可能なのです。
-----------------------------------------------------------------------------------------------------
[root@mnakamura ~]# chkconfig --del netfs
[root@mnakamura ~]# chkconfig --list | grep netfs
[root@mnakamura ~]#

-----------------------------------------------------------------------------------------------------
これで削除。追加する際は…

-----------------------------------------------------------------------------------------------------
[root@mnakamura ~]# chkconfig --add netfs
[root@mnakamura ~]# chkconfig --list | grep netfs
netfs 0:off 1:off 2:off 3:on 4:on 5:on 6:off

-----------------------------------------------------------------------------------------------------
この通り。ただし、--addするとonの状態になってしまうようなので、
こちらも適切な状態に設定しましょう。

-----------------------------------------------------------------------------------------------------
[root@mnakamura ~]# chkconfig netfs off
[root@mnakamura ~]# chkconfig --list | grep netfs
netfs 0:off 1:off 2:off 3:off 4:off 5:off 6:off

-----------------------------------------------------------------------------------------------------
また、CentOS7系のsystemctlでも同じようなコマンドが用意されています。
systemctl list-unit-filesというコマンドになりますが、
ページャが邪魔な場合はオプションを付ければ全て一括出力可能です。

-----------------------------------------------------------------------------------------------------
[root@mnakamuraOS7 ~]# systemctl list-unit-files --no-pager
UNIT FILE STATE
proc-sys-fs-binfmt_misc.automount static
dev-hugepages.mount static
dev-mqueue.mount static
proc-sys-fs-binfmt_misc.mount static
sys-fs-fuse-connections.mount static
sys-kernel-config.mount static
sys-kernel-debug.mount static
tmp.mount disabled
brandbot.path disabled
systemd-ask-password-console.path static
systemd-ask-password-plymouth.path static
systemd-ask-password-wall.path static
session-102389.scope static
session-102392.scope static
auditd.service enabled
autovt@.service disabled
blk-availability.service disabled
brandbot.service static
chrony-dnssrv@.service static
chrony-wait.service disabled
chronyd.service enabled
console-getty.service disabled
console-shell.service disabled
container-getty@.service static
cpupower.service disabled
crond.service enabled
dbus-org.fedoraproject.FirewallD1.service enabled
dbus-org.freedesktop.hostname1.service static
dbus-org.freedesktop.import1.service static
dbus-org.freedesktop.locale1.service static
dbus-org.freedesktop.login1.service static
dbus-org.freedesktop.machine1.service static
dbus-org.freedesktop.network1.service bad
dbus-org.freedesktop.NetworkManager.service enabled
dbus-org.freedesktop.nm-dispatcher.service enabled
dbus-org.freedesktop.timedate1.service static
dbus.service static
debug-shell.service disabled
dm-event.service disabled
dnsmasq.service disabled
dovecot.service enabled
dracut-cmdline.service static
dracut-initqueue.service static
dracut-mount.service static
dracut-pre-mount.service static
dracut-pre-pivot.service static
dracut-pre-trigger.service static
dracut-pre-udev.service static
dracut-shutdown.service static
ebtables.service disabled
emergency.service static
fail2ban.service disabled
fancontrol.service disabled
firewalld.service enabled
fstrim.service static
getty@.service disabled
halt-local.service static
htcacheclean.service static
httpd.service enabled
initrd-cleanup.service static
initrd-parse-etc.service static
initrd-switch-root.service static
initrd-udevadm-cleanup-db.service static
iprdump.service disabled
iprinit.service disabled
iprupdate.service disabled
irqbalance.service enabled
kdump.service enabled
kmod-static-nodes.service static
lm_sensors.service enabled
lvm2-lvmetad.service disabled
lvm2-lvmpolld.service disabled
lvm2-monitor.service disabled
lvm2-pvscan@.service static
mariadb.service enabled
messagebus.service static
microcode.service disabled
named-setup-rndc.service static
named.service disabled
NetworkManager-dispatcher.service enabled
NetworkManager-wait-online.service disabled
NetworkManager.service enabled
nrpe.service disabled
ntpd.service disabled
ntpdate.service disabled
plymouth-halt.service disabled
plymouth-kexec.service disabled
plymouth-poweroff.service disabled
plymouth-quit-wait.service disabled
plymouth-quit.service disabled
plymouth-read-write.service disabled
plymouth-reboot.service disabled
plymouth-start.service disabled
plymouth-switch-root.service static
polkit.service static
postfix.service enabled
quotaon.service static
rc-local.service static
rdisc.service disabled
rdma.service disabled
rescue.service static
rhel-autorelabel-mark.service static
rhel-autorelabel.service static
rhel-configure.service static
rhel-dmesg.service disabled
rhel-domainname.service disabled
rhel-import-state.service static
rhel-loadmodules.service static
rhel-readonly.service static
rpcbind.service indirect
rsyncd.service disabled
rsyncd@.service static
rsyslog.service enabled
saslauthd.service enabled
selinux-policy-migrate-local-changes@.service static
serial-getty@.service disabled
snmpd.service enabled
snmptrapd.service disabled
sshd-keygen.service static
sshd.service enabled
sshd@.service static
sysstat.service enabled
systemd-ask-password-console.service static
systemd-ask-password-plymouth.service static
systemd-ask-password-wall.service static
systemd-backlight@.service static
systemd-binfmt.service static
systemd-bootchart.service disabled
systemd-firstboot.service static
systemd-fsck-root.service static
systemd-fsck@.service static
systemd-halt.service static
systemd-hibernate-resume@.service static
systemd-hibernate.service static
systemd-hostnamed.service static
systemd-hwdb-update.service static
systemd-hybrid-sleep.service static
systemd-importd.service static
systemd-initctl.service static
systemd-journal-catalog-update.service static
systemd-journal-flush.service static
systemd-journald.service static
systemd-kexec.service static
systemd-localed.service static
systemd-logind.service static
systemd-machine-id-commit.service static
systemd-machined.service static
systemd-modules-load.service static
systemd-nspawn@.service disabled
systemd-poweroff.service static
systemd-quotacheck.service static
systemd-random-seed.service static
systemd-readahead-collect.service disabled
systemd-readahead-done.service indirect
systemd-readahead-drop.service disabled
systemd-readahead-replay.service disabled
systemd-reboot.service static
systemd-remount-fs.service static
systemd-rfkill@.service static
systemd-shutdownd.service static
systemd-suspend.service static
systemd-sysctl.service static
systemd-timedated.service static
systemd-tmpfiles-clean.service static
systemd-tmpfiles-setup-dev.service static
systemd-tmpfiles-setup.service static
systemd-udev-settle.service static
systemd-udev-trigger.service static
systemd-udevd.service static
systemd-update-done.service static
systemd-update-utmp-runlevel.service static
systemd-update-utmp.service static
systemd-user-sessions.service static
systemd-vconsole-setup.service static
tcsd.service disabled
teamd@.service static
telnet@.service static
tuned.service disabled
vsftpd.service enabled
vsftpd@.service disabled
wpa_supplicant.service disabled
-.slice static
machine.slice static
system.slice static
user.slice static
dbus.socket static
dm-event.socket enabled
dovecot.socket disabled
lvm2-lvmetad.socket enabled
lvm2-lvmpolld.socket enabled
rpcbind.socket enabled
rsyncd.socket disabled
sshd.socket disabled
syslog.socket static
systemd-initctl.socket static
systemd-journald.socket static
systemd-networkd.socket disabled
systemd-shutdownd.socket static
systemd-udevd-control.socket static
systemd-udevd-kernel.socket static
telnet.socket disabled
basic.target static
bluetooth.target static
cryptsetup-pre.target static
cryptsetup.target static
ctrl-alt-del.target disabled
default.target enabled
emergency.target static
final.target static
getty.target static
graphical.target static
halt.target disabled
hibernate.target static
hybrid-sleep.target static
initrd-fs.target static
initrd-root-fs.target static
initrd-switch-root.target static
initrd.target static
iprutils.target disabled
kexec.target disabled
local-fs-pre.target static
local-fs.target static
machines.target disabled
multi-user.target enabled
network-online.target static
network-pre.target static
network.target static
nss-lookup.target static
nss-user-lookup.target static
paths.target static
poweroff.target disabled
printer.target static
reboot.target disabled
remote-fs-pre.target static
remote-fs.target enabled
rescue.target disabled
rpcbind.target static
runlevel0.target disabled
runlevel1.target disabled
runlevel2.target enabled
runlevel3.target enabled
runlevel4.target enabled
runlevel5.target static
runlevel6.target disabled
shutdown.target static
sigpwr.target static
sleep.target static
slices.target static
smartcard.target static
sockets.target static
sound.target static
suspend.target static
swap.target static
sysinit.target static
system-update.target static
time-sync.target static
timers.target static
umount.target static
vsftpd.target disabled
chrony-dnssrv@.timer disabled
fstrim.timer disabled
systemd-readahead-done.timer indirect
systemd-tmpfiles-clean.timer static
263 unit files listed.

-----------------------------------------------------------------------------------------------------

皆様もサービスを上手く管理して、是非運用負荷を軽減してみてください。
では、今回はこの辺りで。またお会いしましょう。

この記事をシェアする

  • facebook
  • twitter
  • hatena
  • line
URLとタイトルをコピーする

実績数30,000件!
サーバーやネットワークなど
ITインフラのことならネットアシストへ、
お気軽にご相談ください