Ubuntu 16.04.05自定义启动与停止脚本

1、start.sh(启动脚本)

#!/bin/sh
echo "Service: \033[40G[\033[49;32;5mStart\033[0m]\n";
# 启动Nginx
/usr/local/nginx/sbin/nginx
# 启动Php-fpm
/usr/local/php/sbin/php-fpm
# 启动mysql
/etc/init.d/mysql start
netstat -naptu | grep -E 'nginx|php-fpm|mysqld'
echo "Service: \033[40G[\033[49;32;5mDone\033[0m]\n";

Ubuntu 16.04.05自定义启动与停止脚本_第1张图片

2、stop.sh(停止脚本)


#!/bin/sh
Server_Name="nginx php-fpm mysql"
echo "Service: \033[40G[\033[49;32;5mStop\033[0m]\n";
# 停止Nginx
/usr/local/nginx/sbin/nginx -s stop
# 关闭Mysql
/etc/init.d/mysql stop
# 关闭PHP-FPM
ps -ef|grep php-fpm|awk -F ' ' '{print $2}'|xargs kill -9
# 显示进程信息
netstat -naptu
echo "Service: \033[40G[\033[49;32;5mDone\033[0m]\n";

Ubuntu 16.04.05自定义启动与停止脚本_第2张图片

1、未对进程是否启动做判断
2、必须给脚本赋权限,如:chmod 777 start.sh
3、执行脚本必须带”/”,如:./start.sh

3、参考脚本

停止服务时,判断进程是否存在,然后kill

#!/bin/sh
USR_NAME=`whoami`
Server_Name="nginx php-fpm mysql"
printf "\n正在停止服务,请等待\n"
for i in $Server_Name
do
pid=`ps -e | grep $i |awk '{print $1}'`
if [ "$pid" != "" ]; then
  kill -9 $pid
  sleep 3
fi
done

#./catalina.sh stop

ps -ef|grep ${USR_NAME} | grep -v bash | grep -v sshd

echo "Shutdown nginx php-fpm service done"
echo "Shutdown web ok ."

你可能感兴趣的:(Linux)