记录笔记<java调用shell脚本,shell脚本调用python> 传参

package com.hansight.atom.custom.abilityPower.util;
import org.apache.commons.lang.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStreamReader;
import java.io.LineNumberReader;

public class JaveShellUtil {
    private static final Logger logger = LoggerFactory.getLogger(JaveShellUtil.class);
    public static int ExecCommand(String command) {
        int retCode = 0;
        try {
            ProcessBuilder builder1 = new ProcessBuilder("/bin/chmod", "755", AbilityPowerConfigReader.shellPath);
            Process process1 = builder1.start();
            process1.waitFor();
            Process process = Runtime.getRuntime().exec(new String[] { "/bin/sh", "-c", command+" 1234,5678"+" taskid"}, null, null);
            retCode = process.waitFor();
            ExecOutput(process);
            logger.info("能力上台调用告警补全shell脚本成功》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》");
        } catch (Exception e) {
            retCode = -1;
            e.printStackTrace();
            logger.error("能力上台调用告警补全shell脚本报错:::",e);
        }
        return retCode;
    }

    public static boolean ExecOutput(Process process) throws Exception {
        if (process == null) {
            return false;
        } else {
            InputStreamReader ir = new InputStreamReader(process.getInputStream());
            LineNumberReader input = new LineNumberReader(ir);
            String line;
            String output = "";
            while ((line = input.readLine()) != null) {
                output += line + "\n";
            }
            input.close();
            ir.close();
            if (output.length() > 0) {
                System.out.println(output);
            }
        }
        return true;
    }
}

shell脚本

#!/usr/bin/env bash
: '
 Date: 2019-06-09 14:11:00
 Description: 补全告警
'
indexs=$1
taskId=$2
source "$(
  cd $(dirname $0)
  pwd
)/env.sh"

PYTHON_COMMAND="${CONDA_ENV_HOME}/bin/python"
(
  cd ${PROGRAM_ENTRY_PATH}
  echo ${indexs}
  echo ${taskId}
  if [ `whoami` == "root" ]; then
    su soar -c "${PYTHON_COMMAND} main_domain_ability.py $indexs $taskId>> ${LOG_HOME}/alarm_completion_industry_ability.log"
  elif [ `whoami` == "soar" ]; then
    ${PYTHON_COMMAND} main_domain_ability.py $indexs $taskId>> ${LOG_HOME}/alarm_completion_industry_ability.log
  else
    echo `whoami` + "用户没有执行当前脚本权限,请切换至root或soar用户执行!"
  fi
)

python脚本

#!/usr/bin/env python3
# -*- encoding:utf-8 -*-
"""
@Filename: main.py
@Time: 5/11/19 12:12 AM
@Description: 补全告警
"""
import time
import sys
indexs = sys.argv[1]
taskId = sys.argv[2]
from zyxa.service.tip_completion_alarm_ability import TipCompletionAlarmByAbility
from zyxa.utils.logger import Logger

log = Logger("main_domain_ability").get_logger()
if __name__ == "__main__":
    try:
        log.info("shell传过来的参数》》》》》》》》》》》》》》》》》》》》》》::"+indexs+"::::::::::::::::::::taskId::::::::::::::::::::::"+taskId)
        t1 = str(round(time.time() * 1000))
        TipCompletionAlarmByAbility().query_alarm(indexs,taskId)
        t2 = str(round(time.time() * 1000))
    except Exception as e:
        log.error("能力上台补全告警异常,异常信息如下:", e)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值