Java通过shell脚本监控重启服务( 二 )

查询所需的进程列表信息
/*** 查询进程列表* @return*/private List<ProcessBean> queryProcessList() {List<ProcessBean> processBeanList = new ArrayList<ProcessBean>();String osname = SystemUtils.osName().toLowerCase();if(osname.indexOf("window") >= 0) {return processBeanList;}final String iafCmd = "ps aux | grep iaf | grep -v grep";final String dimpleCmd = "ps aux | grep dimp | grep -v grep";final String tradeCmd = "ps aux | grep| grep -v grep";final String TomcatCmd = "ps aux | grep java | grep tomcat | grep -v grep";List<ProcessBean> iafProcessList = queryProcessByShellCmd(iafCmd);List<ProcessBean> dimpleProcessList = queryProcessByShellCmd(dimpleCmd);List<ProcessBean> tradeProcessList = queryProcessByShellCmd(tradeCmd);List<ProcessBean> tomcatProcessList = queryProcessByShellCmd(tomcatCmd);processBeanList.addAll(iafProcessList);processBeanList.addAll(dimpleProcessList);processBeanList.addAll(tradeProcessList);processBeanList.addAll(tomcatProcessList);return processBeanList;}

Java通过shell脚本监控重启服务

文章插图
 
重启业务进程为了解决在某个进程启动失败的时候,web端可以获取到该进程的信息, 需要通过shell返回一个int值, 每个进程启动结果占用1个bit位方式实现,web端获取结果后,解决返回的结果,然后判断是否有进程启动失败 。
首先准备好shell脚本,内容如下:
#!/bin/bashfileHome=/home/lehoon/interface#返回值,默认0retParam="0"if [ -n "$1" ] ;thenfileHome=$1fiinterface_home=$fileHome#查询gateway的守护shell是否存在,存在就结束掉pid=$(ps -fe | grep run_gateway.sh | grep -v grep | awk '{print $2}')for x in $pid; do kill -9 $x; donepkill -9 gateway#sleep 2s#echo Stop gateway#Nginx stop/usr/local/sbin/nginx -s stop#sleep 2s#echo Stop nginxpkill -9 interface_uapmm#sleep 2s#echo Stop interface_uapmm...cd $interface_home/interface_uapmm/binsh $interface_home/interface_uapmm/bin/startup.sh > startlup.log&#sleep 2s#echo Start interface_uapmm done.cd $interface_home/gateway/binsh $interface_home/gateway/bin/startup.sh > startup.log&#sleep 2s#echo Start gateway done.cd /usr/local/sbin/sh /usr/local/sbin/run_nginx.sh >> nginx.log &#sleep 1ssleep 1sOLD_IFS="$IFS"IFS=" "#query interface_uapmm program is exitsinterface_uapmm_pid=$(ps -fe | grep "./interface_uapmm" | grep -v grep | awk '{print $2}')interface_uapmm_pid_array=($interface_uapmm_pid)interface_uapmm_pid_len=${#interface_uapmm_pid_array[@]}if [ $interface_uapmm_pid_len -eq 1 ]; thenretParam=1fi#query gateway program is exitsgateway_shell_pid=$(ps -fe | grep "gateway" | grep -v grep | awk '{print $2}')gateway_shell_pid_array=($gateway_shell_pid)gateway_shell_pid_len=${#gateway_shell_pid_array[@]}if [ $gateway_shell_pid_len -eq 1 ]; thenretParam=$(($retParam + 2))fi#query nginx program is exitsnginx_pid=$(ps -fe | grep "nginx" | grep -v grep | awk '{print $2}')nginx_pid_array=($nginx_pid)nginx_pid_len=${#nginx_pid_array[@]}if [ $nginx_pid_len -eq 1 ]; thenretParam=$(($retParam + 4))fiIFS="$OLD_IFS"echo $retParamshell通过返回一个integer值,java获取到后,通过判断结果就可以知道哪些进程启动失败了 。
java代码如下:
/*** 重启接口脚本* @return*/@RequestMApping(value = https://www.isolves.com/it/cxkf/yy/JAVA/2020-08-12/"interface/restart")@RequiresPermissions(value = {"business:operation:maintenance:interface:restart"})public StringrestartInterface(HttpServletRequest request, HttpServletResponse response) throws BusinessException{String osname = SystemUtils.osName().toLowerCase();if(osname.indexOf("window") >= 0) {//增加日志记录LogUtils.saveLog(request, "系统运维", "手动重启接口系统失败, 不支持当前window系统",Log.TYPE_ACCESS, UserUtils.getUser().getId());throw new BusinessException("-1", "运维脚本不支持Window系统,重启接口失败.");}String shellDictName = SHELL_FILE_NAME_MAP.get("interface");String shellFile = DictUtils.getDictValue(shellDictName, "SYSTEM_MAINTENANCE", "");shellFile = StringUtils.trimToEmpty(shellFile);logger.info(String.format("======>>手动重启接口系统,接口启动shell脚本[%s]", shellFile));if(StringUtils.isEmpty(shellFile)) {//增加日志记录LogUtils.saveLog(request, "系统运维","手动重启接口系统失败, 接口启动shell脚本没有配置在字典表中",Log.TYPE_ACCESS, UserUtils.getUser().getId());logger.info("======>>手动重启接口系统失败,接口启动shell脚本没有配置在字典表中");throw new BusinessException("-1", "接口启动shell脚本没有配置在字典表中,启动失败.");}String shellResult = StringUtils.trimToEmpty(runShellFile(shellFile));logger.info(String.format("======>>执行shell脚本[%s],返回值[%s]", shellFile, shellResult));int shellResultCode = -1;try {shellResultCode = Integer.parseInt(shellResult);} catch (NumberFormatException e) {logger.error("============>>> 转换shell脚本返回结果失败{}", e);//增加日志记录LogUtils.saveLog(request, "系统运维",String.format("手动重启接口系统失败, 转换shell脚本返回结果失败,返回结果%s", shellResult),Log.TYPE_ACCESS, UserUtils.getUser().getId());throw new BusinessException("-1", "接口启动失败,请检查shell脚本是否有误.");}if(RESTART_INTERFACE_SUCCESS == shellResultCode) {//增加日志记录LogUtils.saveLog(request, "系统运维","交易接口重启成功",Log.TYPE_ACCESS, UserUtils.getUser().getId());AjaxSuccess success = new AjaxSuccess("交易接口重启成功");return renderJson(response, success);}StringBuilder sb = new StringBuilder("重启接口失败, 未启动成功的进程包括:");//查询错误进程信息//interface_uapmm进程if((shellResultCode & 0x1) == 0) {//interface_uapmm出错sb.append("结果共享缓存、");}if(((shellResultCode >> 1) & 0x1) == 0) {//dimphq run_dimp.sh脚本出错sb.append("行情启动Shell进程、");}//gateway进程if(((shellResultCode >> 2) & 0x1) == 0) {//gateway出错sb.append("接口网关、");}if(((shellResultCode >> 3) & 0x1) == 0) {//nginx脚本出错sb.append("nginx、");}sb.deleteCharAt(sb.length() - 1);String message = sb.toString();logger.info("=====>>>>管理员:{},本次重启接口进程失败, {}", UserUtils.getUser().getLoginName(), message);//增加日志记录LogUtils.saveLog(request, "系统运维", message, Log.TYPE_ACCESS, UserUtils.getUser().getId());throw new BusinessException("-1", message);}


推荐阅读