SysV启动方式最大的特点就是服务管理脚本是分开的,可以接收start,stop等的文件。一般可以用如下命令来控制服务的开启和关闭:
/etc/init.d/xxx {start|stop|restart}
下面就来说说这种脚本是如何书写的。
其中${1}传入的参数。
case "${1}" in start) ... ;; stop) ... ;; restart) ... ;; status) ... ;; *) ... ;; esac
下面用lfs中的swap脚本文件为示例说明一下。用swap是因为这个脚本只用到一个命令swapon,控制参数也很全。
其中boot_mesg和evaluate_retval为两个函数,不相关,可以不看。
#!/bin/sh . /etc/sysconfig/rc . ${rc_functions} case "${1}" in start) boot_mesg "Activating all swap files/partitions..." swapon -a evaluate_retval ;; stop) boot_mesg "Deactivating all swap files/partitions..." swapoff -a evaluate_retval ;; restart) ${0} stop sleep 1 ${0} start ;; status) boot_mesg "Retrieving swap status." ${INFO} echo_ok echo swapon -s ;; *) echo "Usage: ${0} {start|stop|restart|status}" exit 1 ;; esac # End $rc_base/init.d/swap
从上面的例子可以看出,服务脚本也是不难写的,只要在指定位置写上代码就可以了,前提是你知道命令是什么。