systemdの使い方
シェルスクリプトを用意し、Unit定義ファイルを作成、自動起動設定をする。
CentOS 7, CentOS 8, Ubuntu 18, openSUSE 42 いずれも設定方法は同じだった。
# ls -l /usr/local/tmp/foo.sh
-rwxr-xr-x 1 root root 66 Mar 30 21:07 /usr/local/tmp/foo.sh
# cat /usr/local/tmp/foo.sh
# !/bin/sh
while true
do
date >> /tmp/date.txt
sleep 2000
done
# vi /etc/systemd/system/foo.service # Unit定義ファイルを作成
# ls -l /etc/systemd/system/foo.service
-rw-r--r-- 1 root root 244 Mar 30 21:15 /etc/systemd/system/foo.service
# cat /etc/systemd/system/foo.service # フォーマットやスペリングを間違えないように
[Unit]
Description = test systemd with date
After = network.target
[Service]
ExecStart = /usr/local/tmp/foo.sh
Restart = always
RestartSec = 16
Type = simple
StandardOutput = syslog
StandardError = syslog
[Install]
WantedBy = multi-user.target
# systemctl list-unit-files --type=service | grep foo # disabled 表示
foo.service disabled
# systemctl enable foo # 自動起動設定
Created symlink from /etc/systemd/system/multi-user.target.wants/foo.service to /etc/systemd/system/foo.service.
# systemctl is-enabled foo
enabled
# systemctl status foo
# systemctl start foo
# ls -l /tmp/date.txt
-rw-r--r-- 1 root root 29 Mar 30 23:28 /tmp/date.txt
# ps -ef | grep foo | grep sh
root 27352 1 0 23:28 ? 00:00:00 /bin/sh /usr/local/tmp/foo.sh
# systemctl stop foo
# ps -ef | grep foo | grep sh
# sync
# shutdown -r now
# ls -l /tmp/date.txt
-rw-r--r-- 1 root root 58 Mar 30 23:32 /tmp/date.txt
# ps -ef | grep foo | grep sh # 自動起動を確認
root 20644 1 0 23:32 ? 00:00:00 /bin/sh /usr/local/tmp/foo.sh
# kill 20644 # Restart = always なのでkillしても起動
# ls -l /tmp/date.txt
-rw-r--r-- 1 root root 87 Mar 30 23:34 /tmp/date.txt
# ps -ef | grep foo | grep sh
root 26807 1 0 23:34 ? 00:00:00 /bin/sh /usr/local/tmp/foo.sh
# systemctl stop foo # 停止
# ps -ef | grep foo | grep sh
# systemctl status foo
# less -S /var/log/messages # syslogに出力するように設定しているので確認
非推奨 /etc/rc.d/rc.local
ついでに、非推奨ではあるがCentOS 7では /etc/rc.d/rc.local
が利用できるので、動作確認を行ってみる。
# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
# ls -ld /etc/rc* # シンボリックリンクが張られている
drwxr-xr-x. 10 root root 118 Mar 11 15:32 /etc/rc.d
lrwxrwxrwx. 1 root root 13 Mar 11 15:32 /etc/rc.local -> rc.d/rc.local
lrwxrwxrwx. 1 root root 10 Mar 11 15:30 /etc/rc0.d -> rc.d/rc0.d
lrwxrwxrwx. 1 root root 10 Mar 11 15:30 /etc/rc1.d -> rc.d/rc1.d
lrwxrwxrwx. 1 root root 10 Mar 11 15:30 /etc/rc2.d -> rc.d/rc2.d
lrwxrwxrwx. 1 root root 10 Mar 11 15:30 /etc/rc3.d -> rc.d/rc3.d
lrwxrwxrwx. 1 root root 10 Mar 11 15:30 /etc/rc4.d -> rc.d/rc4.d
lrwxrwxrwx. 1 root root 10 Mar 11 15:30 /etc/rc5.d -> rc.d/rc5.d
lrwxrwxrwx. 1 root root 10 Mar 11 15:30 /etc/rc6.d -> rc.d/rc6.d
# cd /etc/rc.d/ # 実際にはこのディレクトリの中に
# ls -l
total 8
drwxr-xr-x. 2 root root 4096 Mar 12 16:06 init.d
-rw-r--r--. 1 root root 473 Oct 31 08:31 rc.local
drwxr-xr-x. 2 root root 74 Mar 12 16:07 rc0.d
drwxr-xr-x. 2 root root 74 Mar 12 16:07 rc1.d
drwxr-xr-x. 2 root root 74 Mar 13 10:25 rc2.d
drwxr-xr-x. 2 root root 74 Mar 13 10:25 rc3.d
drwxr-xr-x. 2 root root 74 Mar 13 10:25 rc4.d
drwxr-xr-x. 2 root root 74 Mar 13 10:25 rc5.d
drwxr-xr-x. 2 root root 74 Mar 12 16:07 rc6.d
# cat rc.local # /etc/rc.d/rc.local ではなくsystemdを使うようにと書かれている
# !/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.
touch /var/lock/subsys/local
# vi rc.local # 最終行にコマンドを書き込んでみる
# tail -n1 rc.local
date > /tmp/date.txt
# chmod +x /etc/rc.d/rc.local
# sync
# shutdown -r now # 再起動