公司的7台服务器,每台都有十几个左右的服务,手动启动太浪费生命,于是费力气写了个脚本,
在哪个服务器运行这个脚本,就启动对应服务器上的服务。
源代码如下,
#!/bin/bash
# Author : Attaboy
# Date : 2018-08-13
# Date : 2018-10-18
# Date : 2018-11-06
# Date : 2018-11-09
# Date : 2018-11-26
# Date : 2018-12-03
# Date : 2018-12-06
# Date : 2019-08-07 修改成只是启动,不停止服务,去除所有参数
# Date : 2019-08-08 更新每个服务器上的服务
# Date : 2019-08-15 启动后打印启动成功的服务
swt=$1
ip_no="$(ifconfig | awk -F '[ \t:.]+' '/inet add/{print $7}' | head -1)"
pt_ip=(103 104 105 106)
hq_ip=(113 114 115)
log="./log"
#sp="" ; ps aux | grep -iEo "tomcat8_[^/]{2,}/" | awk -F'([ \t/]+)|(tomcat8_)' '{if(!a[$2]++) {if ($0 ~ /tomcat8_[a-zA-Z0-9].*/ ) print $2}}' | sort
#wget -q -O - http://owemeawe.com:8888/download/test/hstong/batchStart_Serv.sh | sh
#使用如下命令获取各个服务器的服务名
#sp="" ; ps aux | grep -iEo "^.+/usr/java/default/bin/java -Djava.+tomcat8_[^/]{2,}/" | awk -F'([ \t/]+)|(tomcat8_)' '{if(!a[$2]++) {if ($0 ~ /tomcat8_[a-zA-Z0-9].*/ ) printf "%-10s%s\n",$2,$(NF-1)}}' | sort -k2 | awk '{printf "%s ",$2}'
S_103="account_executive_admin account_executive_h5 account_executive_server account_executive_task account_executive_web activity-server admin admin_server aml_admin aml_server aml_task cms_admin cms_iisnews_server cms_service cms_task cms_web cps_admin cps_server cps_web crm_admin open_api_community open_api_community service"
S_104="crm_server crm_task csc_admin csc_server dict_server forum_admin forum_interface forum_open_api forum_server forum_task fund_open_api h5_cms h5_embed h5_hd h5_hq h5_hstong h5_kh h5_kh3 h5_live h5_passport h5_simulation_trade h5_trade h5_uc h5_vip hkstock image_server web_kh"
S_105="basical_server bss_admin_server crm_basic_server crm_proccess_admin crm_proccess_server forum_v2_admin forum_v2_interface forum_v2_job forum_v2_server interface ipo_server kh_server kh_witness_pad miscserver msgcenter_admin msgcenter_job msgcenter_server news-crawler open_api open_api_task open_server platform_service push real_trade_server report_admin report_server risk_control_admin simulated_admin simulated_server simulated_task valuable_passport"
S_106="admin_platform_server admin_system_server cms_interface crash_admin crash_api crash_server crm_ext_server crm_money_server h5_kh_valuable hq_subscriber kh_admin socket_interface socket_open_api stock_msg_subscriber stock_server task transfer_server transfer_task vip_admin vip_server vip_task web web_hstong web_valuable web_valuable_hk web_vbkr_hk web_vbkr_www wgp_h5 zx_crawler"
##########################
S_113="hq_admin hq_a_shares_task_v2 hq_asynTask hq_calculator hq_futures_task_v2 hq_mem_calculator hq_open_api hs_hq_push_pc hq_mem_calculator_us"
S_114="hq_interface hq_monitor hq_socket_open hq_task_delay hq_task_push hq_task_v2 hq_us_task_client hq_socket_interface hq_mem_calculator_ashare"
S_115="hq_coreserver hq_service_async hs_hq_push us_stream_task us_stream_task_push usahq2 hq_ice_task ICE-socket-proxy"
usage() {
echo -e "\nSyntax Error! Only ZERO arguments is allowed!"
echo -e "Usage: sh script.sh\n" # >>${log} 2>&1
exit -1
}
pre_chk() {
echo $#
if [ $# -ne 0 ] ; then
usage
fi
#判断有问题,代码暂时不用
}
bpt() {
for i in $1 ; do
sh /data0/opt/hs/tomcat8_$i/bin/startup.sh 2>/dev/null | grep -iE "Tomcat.started"
[ $? -eq 0 ] && echo -e "started service is : $i\n"
#echo "$i"
done
#if [ ${swt} == "s" ] ; then
# for i in $1 ; do
# #sh /data0/opt/hs/tomcat8_$i/bin/startup.sh 2>/dev/null
# echo "XXXX"
# done
#elif [ ${swt} == "t" ] ; then
# for i in $1 ; do
# #sh /data0/opt/hs/tomcat8_$i/bin/shutdown.sh 2>/dev/null
# echo "XXXX"
# done
#fi
}
bhq() {
for i in $1 ; do
sh /data0/opt/hq/tomcat8_$i/bin/startup.sh 2>/dev/null | grep -iE "Tomcat.started"
[ $? -eq 0 ] && echo -e "started service is : $i\n"
#echo "$i"
done
#if [ ${swt} == "s" ] ; then
# for i in $1 ; do
# #sh /data0/opt/hq/tomcat8_$i/bin/startup.sh 2>/dev/null
# echo "XXXX"
# done
#
#elif [ ${swt} == "t" ] ; then
# for i in $1 ; do
# #sh /data0/opt/hq/tomcat8_$i/bin/shutdown.sh 2>/dev/null
# echo "XXXX"
# done
#fi
}
#pre_chk
echo -e "\n\nip_no is : ${ip_no}\n"
case ${ip_no} in
103)
bpt "${S_103}"
;;
104)
bpt "${S_104}"
;;
105)
bpt "${S_105}"
;;
106)
bpt "${S_106}"
;;
113)
bhq "${S_113}"
;;
114)
bhq "${S_114}"
;;
115)
bhq "${S_115}"
;;
*)
echo -e "\nYour IP is not correct.\n"
;;
esac
exit 0