シェルスクリプト勉強①〜httpdを読んでみる〜
シェルスクリプト勉強①
今回は、シェルスクリプトの勉強をしてみようと思う。ただ、どうやって勉強するかだが、今のところLinuxのOSにあるプロセスのシェルスクリプトを読みこんでみようと思う。
そもそも今回のシェルスクリプトの勉強の目的は、1.読めるようになる、2.書けるようになる なので、どちらの目的も達成するためには自分より知識のある人が書いたスクリプトを読み込むことが効果的だと思う。
※参考にしたサイト。参考というか答えが書いてあります。
起動スクリプト(httpd)を読んでみた - まどろみの思考空間
httpd
#!/bin/bash
言わずもがな。bashで起動するという意味。
# chkconfig: - 85 15
起動順序の指定。「スタートが85番」「ストップが15番」という意味。
# processname: httpd
# config: /etc/httpd/conf/httpd.conf
# config: /etc/sysconfig/httpd
設定ファイルの場所を記載。大体/etcの下にあるが、わからなくなったらスクリプトの中を見れば良いことがわかる。
### BEGIN INIT INFO
# Provides: httpd
# Required-Start: $local_fs $remote_fs $network $named
# Required-Stop: $local_fs $remote_fs $network
# Should-Start: distcache
# Short-Description: start and stop Apache HTTP Server
# Description: The Apache HTTP Server is an extensible server
# implementing the current HTTP standards.
### END INIT INFO
おまじないみたいなものらしい。
Required-Start:→httpdの起動前に起動しておかないと行けないスクリプト
Required-Stop:→httpdの停止前に停止しておかないと行けないスクリプト
# Source function library.
. /etc/rc.d/init.d/functions
functionsという共通のルーチンを呼び出してる。
if [ -f /etc/sysconfig/httpd ]; then
. /etc/sysconfig/httpd
fi
httpdのファイルが存在するかを確認している。もし存在しているのであれば、
. /etc/sysconfig/httpdを読み込む。
# Start httpd in the C locale by default.
自分で定義したHTTPD_LANGという変数に何も入っていなければ、アプリケーションにとってデフォルトの”C”を使用する。
# This will prevent initlog from swallowing up a pass-phrase prompt if
# mod_ssl needs a pass-phrase from the user.
INITLOG_ARGS=""
# Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server
# with the thread-based "worker" MPM; BE WARNED that some modules may not
# work correctly with a thread-based MPM; notably PHP will refuse to start.
# Path to the apachectl script, server binary, and short-form for messages.
apachectl=/usr/sbin/apachectl
httpd=${HTTPD-/usr/sbin/httpd}
prog=httpd
pidfile=${PIDFILE-/var/run/httpd/httpd.pid}
lockfile=${LOCKFILE-/var/lock/subsys/httpd}
RETVAL=0
STOP_TIMEOUT=${STOP_TIMEOUT-10}
とにかくいろんな変数を定義している。 自分で設定していなければ、デフォルトの設定を使用する。
start() {
echo -n $"Starting $prog: "
LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch ${lockfile}
return $RETVAL
}
Starting httpd を表示させ、LANGの設定(起動時の言語の設定)をしている。
その後、daemon関数を実行し、デーモンとしてhttpdを起動させている。(/etc/init.d/functions の中に記載されている)
実行結果をRETVALの中に格納し、正常終了していれば(0ならば)ロックファイルを作成する。
# When stopping httpd, a delay (of default 10 second) is required
# before SIGKILLing the httpd parent; this gives enough time for the
# httpd parent to SIGKILL any errant children.
stop() {
echo -n $"Stopping $prog: "
killproc -p ${pidfile} -d ${STOP_TIMEOUT} $httpd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}
killproc で対象のプロセスを停止している(はず)。functions の中身確認しないと、、、
その後、実行結果を評価して正常終了しているのであればロックファイルとPIDファイルを削除している。
reload() {
echo -n $"Reloading $prog: "
if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then
RETVAL=6
echo $"not reloading due to configuration syntax error"
failure $"not reloading $httpd due to configuration syntax error"
else
# Force LSB behaviour from killproc
LSB=1 killproc -p ${pidfile} $httpd -HUP
RETVAL=$?
if [ $RETVAL -eq 7 ]; then
failure $"httpd shutdown"
fi
fi
echo
}
最初の条件文で文法をチェックしている。もし文法に問題がなければ再読み込みを実施している。-HUPは親プロセスを殺さずに子プロセスを殺す。具体的な処理はやっぱりfunctionsを見ないとわからない。。
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status -p ${pidfile} $httpd
RETVAL=$?
;;
restart)
stop
start
;;
condrestart|try-restart)
if status -p ${pidfile} $httpd >&/dev/null; then
stop
start
fi
;;
force-reload|reload)
reload
;;
graceful|help|configtest|fullstatus)
$apachectl $@
RETVAL=$?
;;
*)
echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|reload|status|fullstatus|graceful|help|configtest}"
RETVAL=2
esac
第一引数をみて、どのコマンドが入力されたかを評価している。
わからなかったものは別さいとから説明を引用させていただきます。
condrestartはプログラムが実行中であるかどうかを判定して実行中である場合のみ再起動、gracefulはapachectlに丸投げしています。
また、それ以外の場合はマッチするものがなかった旨を表示します。
exit $RETVAL
exitで終了。