java代码控制启停服务,shell远程登录并执行脚本

1、ssh配置免密登录
例如服务器A,远程访问服务器B
服务器A执行`

ssh-keygen -t rsa

`
会生成两个文件把其中的id_rsa.pub文件复制粘贴进入服务器B的文件夹/root/.ssh/authorized_keys中
2、在服务器A执行

ssh root@服务器A的ip

3、后端代码执行脚本文件,控制服务的启/停

application 脚本文件名
status 状态参数

@ApiOperation(value = "操作服务")
    @RequestMapping(value="/operation",method= RequestMethod.GET)
    public ResponseEntity<JsonResult> start(
            @RequestParam(value = "application", required = true) String application,
            @RequestParam(value = "operation", required = true) String operation) {
        logger.info("-----------------------开始"+operation+"服务:" + application
                + "-----------------------------");

        String serverPath = System.getProperty("user.dir");
        logger.info("jar包路径" + serverPath);
        String shellPath = serverPath + "/" + application + ".sh";
        logger.info("shell路径" + shellPath);
        String[] cmd = new String[]{"sh",shellPath,operation};
        logger.info("shell脚本路径和状态" + "------" + application + operation + cmd.toString());

        JsonResult r = new JsonResult();
        try {
            Runtime runtime = Runtime.getRuntime();
            Process process = runtime.exec(cmd);
            int res = process.waitFor();
            if(res != 0){
                logger.info("Failed to call shell command");
            }
            BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
            StringBuffer strbr = new StringBuffer();
            String line;
            while((line = br.readLine()) != null){
                strbr.append(line).append("\n");
            }

            String result = strbr.toString();
            logger.info(result);

            r.setResult("服务状态" + result);
            r.setStatus("ok");
        } catch (Exception e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setStatus("error");
            e.printStackTrace();
        }
        return ResponseEntity.ok(r);
    }

4、脚本文件,再执行服务器B的脚本文件

#!/bin/bash
#echo hello world!
SERVER_IP=服务器B的ip
SH=GATEWAY-SECURITY.sh
APP_NAME=jar包名
APP_PATH=jar包路径
APP_LOG=jar包路径/gateway.log
usage(){
echo "Usage: sh GATEWAY-SECURITY.sh [start|stop|restart|status]"
exit 1
}

start(){
ssh root@$SERVER_IP > /dev/null 2>&1 << d
cd $APP_PATH
sh $SH start
exit
d
echo start ok
}
stop(){
ssh root@$SERVER_IP > /dev/null 2>&1 << f
cd $APP_PATH
sh $SH stop
exit
f
echo stop ok
}
status(){
ssh root@$SERVER_IP > /dev/null 2>&1 << remote
cd $APP_PATH
sh $SH status
exit
remote
echo ok
}
restart(){
stop

sleep 5

start
}
case "$1" in
"start")
start
;;
"stop")
stop
;;
"status")
status
;;
"restart")
restart
;;
*)
usage
;;
esac

5、服务器B的脚本

#!/bin/bash
#echo hello world!
APP_NAME=jar包名
APP_PATH=jar包路径
APP_LOG=jar包路径/gateway.log
usage(){
echo "Usage: sh GATEWAY-SECURITY.sh [start|stop|restart|status]"
exit 1
}

is_exit(){
echo is_exit
pid=`ps -ef | grep $APP_NAME | grep -v grep | awk '{print $2}'`
echo $pid
if [ -z "$pid" ]; then
  return 1
else
  return 0
fi
}
start(){
is_exit
if [ $? -eq "0" ]; then
  echo "$APP_NAME is already running.pid = ${pid}"
else
  cd $APP_PATH
  nohup java -jar ${APP_NAME} >$APP_LOG 2>&1 &
  echo start ok
fi
}
stop(){
is_exit
if [ $? -eq "0" ]; then
  kill -9 $pid
  echo stop ok
else
  echo "${APP_NAME} is not running"
fi
}
status(){
is_exit
touch test.log
if [ $? -eq "0" ]; then
  echo "${APP_NAME} is running"
  exit
else
  echo "${APP_NAME} is not running"
fi
}
restart(){
stop

sleep 5

start
}
case "$1" in
"start")
start
;;
"stop")
stop
;;
"status")
status
;;
"restart")
restart
;;
*)
usage
;;
esac

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值