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