利用系统函数模拟实现应用脚本启动的特殊颜色效果:
/server/scripts/start_nginx.sh {start|stop|restart},用if语句实现。
[root@vm1 scripts]# cat start_nginx.sh
#!/bin/bash
. /etc/init.d/functions
USAGE(){
echo "USAGE: $0 {start|stop|restart}"
exit 1
}
if [ $# -ne 1 ]
then
USAGE
fi
if [ "$1" == "start" ]
then
action "start nginx" /bin/true
elif [ "$1" == "stop" ]
then
action "stop nginx" /bin/true
elif [ "$1" == "restart" ]
then
action "restart nginx" /bin/true
else
USAGE
fi
执行结果:
[root@vm1 scripts]# sh start_nginx.sh start
start nginx [ OK ]
[root@vm1 scripts]# sh start_nginx.sh stop
stop nginx [ OK ]
[root@vm1 scripts]# sh start_nginx.sh restart
restart nginx [ OK ]
说明:
1)在脚本中有action这个系统函数,那这个应该是写在 /etc/init.d/functions文件中,所以在脚本的开头部分,加载了这个文件。
2)另外,我们也在脚本中使用了函数的语法创建了USAGE。
3)如果出现错误代码为"action: command not found",那这个原因就是没有加载/etc/init.d/functions。将这句话的注释取消即可。
案例2:
在脚本中添加启动、停止、重启nginx的功能。
[root@vm1 scripts]# cat start_nginx2.sh
#!/bin/bash
. /etc/init.d/functions
USAGE(){
echo "USAGE: $0 {start|stop|restart}"
exit 1
}
if [ $# -ne 1 ]
then
USAGE
fi
start_nginx=/usr/local/nginx/sbin/nginx
if [ "$1" == "start" ]
then
$start_nginx
action "start nginx" /bin/true
elif [ "$1" == "stop" ]
then
pkill nginx
action "stop nginx" /bin/true
elif [ "$1" == "restart" ]
then
pkill nginx
sleep 2
$start_nginx
action "restart nginx" /bin/true
else
USAGE
fi
测试结果:
[root@vm1 scripts]# sh start_nginx2.sh stop
stop nginx [ OK ]
[root@vm1 scripts]# sh start_nginx2.sh start
start nginx [ OK ]
[root@vm1 scripts]# sh start_nginx2.sh restart
restart nginx [ OK ]
[root@vm1 scripts]# sh start_nginx2.sh stop
stop nginx [ OK ]
[root@vm1 scripts]# netstat -atunlp |grep 80
[root@vm1 scripts]# ps -ef |grep nginx
root 10524 7516 0 17:30 pts/0 00:00:00 grep --color=auto nginx
[root@vm1 scripts]# sh start_nginx2.sh restart
restart nginx [ OK ]
[root@vm1 scripts]# ps -ef |grep nginx |grep -v grep
root 10537 1 0 17:30 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 10538 10537 0 17:30 ? 00:00:00 nginx: worker process
还是遇到一个问题:
如果nginx已经启动,再次执行start命令,会报错信息。那么,我们需要在脚本增加判断。
[root@vm1 scripts]# sh start_nginx2.sh start
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
start nginx [ OK ]
再次修改Shell脚本:
[root@vm1 scripts]# cat start_nginx3.sh
#!/bin/bash
. /etc/init.d/functions
USAGE(){
echo "USAGE: $0 {start|stop|restart}"
exit 1
}
if [ $# -ne 1 ]
then
USAGE
fi
start_nginx=/usr/local/nginx/sbin/nginx
if [ "$1" == "start" ]
then
if [ `netstat -atunlp |grep nginx | wc -l` -ge 1 ]
then
echo "nginx is started."
else
$start_nginx
action "start nginx" /bin/true
fi
elif [ "$1" == "stop" ]
then
pkill nginx
action "stop nginx" /bin/true
elif [ "$1" == "restart" ]
then
pkill nginx
sleep 2
$start_nginx
action "restart nginx" /bin/true
else
USAGE
fi
再次进行测试:
[root@vm1 scripts]# sh start_nginx3.sh start
start nginx [ OK ]
[root@vm1 scripts]#
[root@vm1 scripts]# sh start_nginx3.sh start
nginx is started.
[root@vm1 scripts]# sh start_nginx3.sh stop
stop nginx [ OK ]
[root@vm1 scripts]# sh start_nginx3.sh start
start nginx [ OK ]
[root@vm1 scripts]# sh start_nginx3.sh restart
restart nginx [ OK ]