给源码服务写启动脚本
给源码包httpd写启动脚本,可以启动、停止、查看状态
第一步:编辑脚本文件
[root@server2 ~]# cat apache.sh
start(){
`/usr/local/apache2/bin/apachectl start &> /dev/null`
if [ $?-eq 0 ];then
echo "start the httpd server ...... [ok]"
else
echo "start faild ... "
fi
}
stop(){ //停止服务
`/usr/local/apache2/bin/apachectl stop &> /dev/null`
if [ $?-eq 0 ];then
echo "stop the httpd server ...... [ok]"
else
echo "stop faild ... "
fi
}
status(){ //查看状态,根据是否开启80端口查看
j=`netstat-tunlp | grep :80 | wc -l`
if [ $j-eq 0 ];then
echo "httpd server is stoped ... "
else
echo "httpd server is running ..."
fi
}
case $1 in
start)
start;;
stop)
stop;;
status)
status;;
*)
echo"$0 values in { stop | start | status } , please again !"
esac
[root@server2 ~]#
第二步:将脚本移动到/etc/init.d目录下并重命名为apache,并添加x权限
[root@server2 ~]# mv apache.sh /etc/init.d/apache
[root@server2 ~]# chmod +x /etc/init.d/apache
第三步:为/etc/init.d/apache添加下列两行
# chkconfig: 2345 55 25 //指定运行级别、启动服务的优先级、关闭服务的优先级
# description: httpd server daemon //指定服务的描述信息
第四步:使用chkconfig --add将apache添加为系统服务
[root@server2 ~]# chkconfig --add /etc/init.d/apache
[root@server2 ~]# chkconfig --list | grep apache
apache 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
[root@server2 ~]# service apache stop
stop the httpd server ...... [ok]
[root@server2 ~]# service apache status
httpd server is stoped ...
[root@server2 ~]# service apache start
start the httpd server ...... [ok]
[root@server2 ~]# service apache status
httpd server is running ...
[root@server2 ~]#